2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.core.utilities.file;
23 import java.io.ByteArrayInputStream;
24 import java.io.InputStream;
25 import java.util.HashMap;
28 import java.util.stream.Collectors;
29 import org.apache.commons.collections4.CollectionUtils;
30 import org.apache.commons.collections4.MapUtils;
32 public class FileContentHandler {
34 private Map<String, byte[]> files = new HashMap<>();
36 public FileContentHandler() {
39 public FileContentHandler(final FileContentHandler other) {
44 * Gets file content as stream.
46 * @param fileName the file name
47 * @return if the file was found, its content as stream, otherwise {@code null}.
49 public InputStream getFileContentAsStream(final String fileName) {
50 byte[] content = files.get(fileName);
51 if (content == null || content.length == 0) {
55 return new ByteArrayInputStream(content);
58 public byte[] getFileContent(final String fileName) {
59 return files.get(fileName);
62 public boolean isFolder(final String fileName) {
63 return files.get(fileName) == null;
66 public boolean isFile(final String fileName) {
67 return files.get(fileName) != null;
70 public void addFolder(final String folder) {
71 files.put(folder, null);
74 public void addFile(final String fileName, final byte[] content) {
75 files.put(fileName, content == null ? new byte[0] : content);
78 public void addFile(final String fileName, final InputStream is) {
79 files.put(fileName, FileUtils.toByteArray(is));
82 public Map<String, byte[]> getFiles() {
83 return files.entrySet().stream().filter(entry -> entry.getValue() != null)
84 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
87 public void setFiles(final Map<String, byte[]> files) {
91 public Set<String> getFileList() {
92 return files.keySet().stream().filter(this::isFile).collect(Collectors.toSet());
95 public Set<String> getFolderList() {
96 return files.keySet().stream().filter(this::isFolder).collect(Collectors.toSet());
99 public void addAll(final FileContentHandler fileContentHandlerOther) {
100 if (CollectionUtils.isNotEmpty(fileContentHandlerOther.getFolderList())) {
101 fileContentHandlerOther.getFolderList().forEach(this::addFolder);
103 addAll(fileContentHandlerOther.getFiles());
106 private void addAll(final Map<String, byte[]> files) {
107 if (!MapUtils.isEmpty(files)) {
108 files.forEach(this::addFile);
112 public boolean isEmpty() {
113 return MapUtils.isEmpty(this.files);
116 public byte[] remove(final String fileName) {
117 return files.remove(fileName);
120 public boolean containsFile(final String fileName) {
121 return files.containsKey(fileName);