cc13879b968b65ed3cd959483c0a74f82a4414db
[sdc.git] /
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     /**
37      * Gets file content as stream.
38      *
39      * @param fileName the file name
40      * @return if the file was found, its content as stream, otherwise {@code null}.
41      */
42     public InputStream getFileContentAsStream(final String fileName) {
43         byte[] content = files.get(fileName);
44         if (content == null || content.length == 0) {
45             return null;
46         }
47
48         return new ByteArrayInputStream(content);
49     }
50
51     public byte[] getFileContent(final String fileName) {
52         return files.get(fileName);
53     }
54
55     public boolean isFolder(final String fileName) {
56         return files.get(fileName) == null;
57     }
58
59     public boolean isFile(final String fileName) {
60         return files.get(fileName) != null;
61     }
62
63     public void addFolder(final String folder) {
64         files.put(folder, null);
65     }
66
67     public void addFile(final String fileName, final byte[] content) {
68         files.put(fileName, content == null ? new byte[0] : content);
69     }
70
71     public void addFile(final String fileName, final InputStream is) {
72         files.put(fileName, FileUtils.toByteArray(is));
73     }
74
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));
78     }
79
80     public void setFiles(final Map<String, byte[]> files) {
81         addAll(files);
82     }
83
84     public Set<String> getFileList() {
85         return files.keySet().stream().filter(this::isFile).collect(Collectors.toSet());
86     }
87
88     public Set<String> getFolderList() {
89         return files.keySet().stream().filter(this::isFolder).collect(Collectors.toSet());
90     }
91
92     public void addAll(final FileContentHandler fileContentHandlerOther) {
93         if (CollectionUtils.isNotEmpty(fileContentHandlerOther.getFolderList())) {
94             fileContentHandlerOther.getFolderList().forEach(this::addFolder);
95         }
96         addAll(fileContentHandlerOther.getFiles());
97     }
98
99     private void addAll(final Map<String, byte[]> files) {
100         if (!MapUtils.isEmpty(files)) {
101             files.forEach(this::addFile);
102         }
103     }
104
105     public boolean isEmpty() {
106         return MapUtils.isEmpty(this.files);
107     }
108
109     public byte[] remove(final String fileName) {
110         return files.remove(fileName);
111     }
112
113     public boolean containsFile(final String fileName) {
114         return files.containsKey(fileName);
115     }
116
117 }