db5be601499aa570777497d6d580bf257b08ca0a
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-utilities-lib / src / main / java / org / openecomp / core / utilities / file / FileContentHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.core.utilities.file;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.InputStream;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.stream.Collectors;
29 import org.apache.commons.collections4.CollectionUtils;
30 import org.apache.commons.collections4.MapUtils;
31
32 public class FileContentHandler {
33
34     private Map<String, byte[]> files = new HashMap<>();
35
36     public FileContentHandler() {
37     }
38
39     public FileContentHandler(final FileContentHandler other) {
40         addAll(other);
41     }
42
43     /**
44      * Gets file content as stream.
45      *
46      * @param fileName the file name
47      * @return if the file was found, its content as stream, otherwise {@code null}.
48      */
49     public InputStream getFileContentAsStream(final String fileName) {
50         byte[] content = files.get(fileName);
51         if (content == null || content.length == 0) {
52             return null;
53         }
54
55         return new ByteArrayInputStream(content);
56     }
57
58     public byte[] getFileContent(final String fileName) {
59         return files.get(fileName);
60     }
61
62     public boolean isFolder(final String fileName) {
63         return files.get(fileName) == null;
64     }
65
66     public boolean isFile(final String fileName) {
67         return files.get(fileName) != null;
68     }
69
70     public void addFolder(final String folder) {
71         files.put(folder, null);
72     }
73
74     public void addFile(final String fileName, final byte[] content) {
75         files.put(fileName, content == null ? new byte[0] : content);
76     }
77
78     public void addFile(final String fileName, final InputStream is) {
79         files.put(fileName, FileUtils.toByteArray(is));
80     }
81
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));
85     }
86
87     public void setFiles(final Map<String, byte[]> files) {
88         addAll(files);
89     }
90
91     public Set<String> getFileList() {
92         return files.keySet().stream().filter(this::isFile).collect(Collectors.toSet());
93     }
94
95     public Set<String> getFolderList() {
96         return files.keySet().stream().filter(this::isFolder).collect(Collectors.toSet());
97     }
98
99     public void addAll(final FileContentHandler fileContentHandlerOther) {
100         if (CollectionUtils.isNotEmpty(fileContentHandlerOther.getFolderList())) {
101             fileContentHandlerOther.getFolderList().forEach(this::addFolder);
102         }
103         addAll(fileContentHandlerOther.getFiles());
104     }
105
106     private void addAll(final Map<String, byte[]> files) {
107         if (!MapUtils.isEmpty(files)) {
108             files.forEach(this::addFile);
109         }
110     }
111
112     public boolean isEmpty() {
113         return MapUtils.isEmpty(this.files);
114     }
115
116     public byte[] remove(final String fileName) {
117         return files.remove(fileName);
118     }
119
120     public boolean containsFile(final String fileName) {
121         return files.containsKey(fileName);
122     }
123
124 }