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<>();
37 * Gets file content as stream.
39 * @param fileName the file name
40 * @return if the file was found, its content as stream, otherwise {@code null}.
42 public InputStream getFileContentAsStream(final String fileName) {
43 byte[] content = files.get(fileName);
44 if (content == null || content.length == 0) {
48 return new ByteArrayInputStream(content);
51 public byte[] getFileContent(final String fileName) {
52 return files.get(fileName);
55 public boolean isFolder(final String fileName) {
56 return files.get(fileName) == null;
59 public boolean isFile(final String fileName) {
60 return files.get(fileName) != null;
63 public void addFolder(final String folder) {
64 files.put(folder, null);
67 public void addFile(final String fileName, final byte[] content) {
68 files.put(fileName, content == null ? new byte[0] : content);
71 public void addFile(final String fileName, final InputStream is) {
72 files.put(fileName, FileUtils.toByteArray(is));
75 public Map<String, byte[]> getFiles() {
76 return files.entrySet().stream().filter(entry -> entry.getValue() != null)
77 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
80 public void setFiles(final Map<String, byte[]> files) {
84 public Set<String> getFileList() {
85 return files.keySet().stream().filter(this::isFile).collect(Collectors.toSet());
88 public Set<String> getFolderList() {
89 return files.keySet().stream().filter(this::isFolder).collect(Collectors.toSet());
92 public void addAll(final FileContentHandler fileContentHandlerOther) {
93 if (CollectionUtils.isNotEmpty(fileContentHandlerOther.getFolderList())) {
94 fileContentHandlerOther.getFolderList().forEach(this::addFolder);
96 addAll(fileContentHandlerOther.getFiles());
99 private void addAll(final Map<String, byte[]> files) {
100 if (!MapUtils.isEmpty(files)) {
101 files.forEach(this::addFile);
105 public boolean isEmpty() {
106 return MapUtils.isEmpty(this.files);
109 public byte[] remove(final String fileName) {
110 return files.remove(fileName);
113 public boolean containsFile(final String fileName) {
114 return files.containsKey(fileName);