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;
33 * Stores the content of files in a path:byte[] structure
35 public class FileContentHandler {
37 private final Map<String, byte[]> files = new HashMap<>();
39 public FileContentHandler() {
42 public FileContentHandler(final FileContentHandler other) {
47 * Gets file content as stream.
49 * @param fileName the file name
50 * @return if the file was found, its content as stream, otherwise {@code null}.
52 public InputStream getFileContentAsStream(final String fileName) {
53 byte[] content = files.get(fileName);
54 if (content == null || content.length == 0) {
58 return new ByteArrayInputStream(content);
62 * Gets the content of a file.
64 * @param filePath the file path
65 * @return the content of the file
67 public byte[] getFileContent(final String filePath) {
68 return files.get(filePath);
72 * Checks if the path is a folder.
74 * @param filePath the file path to verify
75 * @return {@code true} if the path is a folder, {@code false} otherwise
77 public boolean isFolder(final String filePath) {
78 return files.get(filePath) == null;
82 * Checks if the path is a file.
84 * @param filePath the file path to verify
85 * @return {@code true} if the path is a file, {@code false} otherwise
87 public boolean isFile(final String filePath) {
88 return files.get(filePath) != null;
94 * @param folderPath the folder path to add
96 public void addFolder(final String folderPath) {
97 files.put(folderPath, null);
103 * @param filePath the file path
104 * @param content the file content
106 public void addFile(final String filePath, final byte[] content) {
107 files.put(filePath, content == null ? new byte[0] : content);
113 * @param filePath the file path
114 * @param fileInputStream the file input stream
116 public void addFile(final String filePath, final InputStream fileInputStream) {
117 files.put(filePath, FileUtils.toByteArray(fileInputStream));
121 * Gets only the files, ignoring directories from the structure.
123 * @return a file path:content map
125 public Map<String, byte[]> getFiles() {
126 return files.entrySet().stream().filter(entry -> entry.getValue() != null)
127 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
130 public void setFiles(final Map<String, byte[]> files) {
135 * Gets only the file paths, ignoring directories from the structure.
137 * @return a set of the file paths
139 public Set<String> getFileList() {
140 return files.keySet().stream().filter(this::isFile).collect(Collectors.toSet());
144 * Gets only the folder paths, ignoring files from the structure.
146 * @return a set of the folder paths
148 public Set<String> getFolderList() {
149 return files.keySet().stream().filter(this::isFolder).collect(Collectors.toSet());
152 public void addAll(final FileContentHandler fileContentHandlerOther) {
153 if (CollectionUtils.isNotEmpty(fileContentHandlerOther.getFolderList())) {
154 fileContentHandlerOther.getFolderList().forEach(this::addFolder);
156 addAll(fileContentHandlerOther.getFiles());
159 private void addAll(final Map<String, byte[]> files) {
160 if (!MapUtils.isEmpty(files)) {
161 files.forEach(this::addFile);
166 * Checks if the file structure is empty.
168 * @return {@code true} if the file structure is empty, {@code false} otherwise
170 public boolean isEmpty() {
171 return MapUtils.isEmpty(this.files);
175 * Removes a file or folder from the file structure.
177 * @param filePath the file path to remove
178 * @return the removed file content
180 public byte[] remove(final String filePath) {
181 return files.remove(filePath);
185 * Checks if the file structure contains the provided file.
187 * @param filePath the file path to search
188 * @return {@code true} if the file exists, {@code false} otherwise
190 public boolean containsFile(final String filePath) {
191 return files.containsKey(filePath);