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 org.apache.commons.io.IOUtils;
24 import org.openecomp.core.utilities.json.JsonUtil;
25 import org.openecomp.sdc.tosca.services.YamlUtil;
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
31 import java.util.Collections;
32 import java.util.Enumeration;
33 import java.util.LinkedList;
34 import java.util.List;
35 import java.util.Objects;
36 import java.util.function.Function;
37 import java.util.zip.ZipEntry;
38 import java.util.zip.ZipInputStream;
41 * The type File utils.
43 public class FileUtils {
46 * Allows to consume an input stream open against a resource with a given file name.
48 * @param fileName the file name
49 * @param function logic to be applied to the input stream
51 public static <T> T readViaInputStream(String fileName, Function<InputStream, T> function) {
52 return readViaInputStream(FileUtils.class.getClassLoader().getResource(fileName), function);
56 * Allows to consume an input stream open against a resource with a given URL.
58 * @param urlFile the url file
59 * @param function logic to be applied to the input stream
61 public static <T> T readViaInputStream(URL urlFile, Function<InputStream, T> function) {
63 Objects.requireNonNull(urlFile);
64 try (InputStream is = urlFile.openStream()) {
65 return function.apply(is);
66 } catch (IOException exception) {
67 throw new RuntimeException(exception);
72 * Gets file input streams.
74 * @param fileName the file name
75 * @return the file input streams
77 public static List<URL> getAllLocations(String fileName) {
79 List<URL> urls = new LinkedList<>();
80 Enumeration<URL> urlFiles;
83 urlFiles = FileUtils.class.getClassLoader().getResources(fileName);
84 while (urlFiles.hasMoreElements()) {
85 urls.add(urlFiles.nextElement());
89 } catch (IOException exception) {
90 throw new RuntimeException(exception);
93 return urls.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(urls);
97 * Convert to bytes byte [ ].
99 * @param object the object
100 * @param extension the extension
101 * @return the byte [ ]
103 public static byte[] convertToBytes(Object object, FileExtension extension) {
104 if (object != null) {
105 if (extension.equals(FileExtension.YAML) || extension.equals(FileExtension.YML)) {
106 return new YamlUtil().objectToYaml(object).getBytes();
108 return JsonUtil.object2Json(object).getBytes();
116 * Convert to input stream input stream.
118 * @param object the object
119 * @param extension the extension
120 * @return the input stream
122 public static InputStream convertToInputStream(Object object, FileExtension extension) {
123 if (object != null) {
127 if (extension.equals(FileExtension.YAML) || extension.equals(FileExtension.YML)) {
128 content = new YamlUtil().objectToYaml(object).getBytes();
130 content = JsonUtil.object2Json(object).getBytes();
133 return new ByteArrayInputStream(content);
140 * Load file to input stream input stream.
142 * @param fileName the file name
143 * @return the input stream
145 public static InputStream loadFileToInputStream(String fileName) {
146 URL urlFile = Thread.currentThread().getContextClassLoader().getResource(fileName);
148 Enumeration<URL> en = Thread.currentThread().getContextClassLoader().getResources(fileName);
149 while (en.hasMoreElements()) {
150 urlFile = en.nextElement();
152 } catch (IOException | NullPointerException exception) {
153 throw new RuntimeException(exception);
156 if (urlFile != null) {
157 return urlFile.openStream();
159 throw new RuntimeException();
161 } catch (IOException | NullPointerException exception) {
162 throw new RuntimeException(exception);
168 * To byte array byte [ ].
170 * @param input the input
171 * @return the byte [ ]
173 public static byte[] toByteArray(InputStream input) {
178 return IOUtils.toByteArray(input);
179 } catch (IOException exception) {
180 throw new RuntimeException(
181 "error will convertion input stream to byte array:" + exception.getMessage());
186 * Gets file without extention.
188 * @param fileName the file name
189 * @return the file without extention
191 public static String getFileWithoutExtention(String fileName) {
192 if (!fileName.contains(".")) {
195 return fileName.substring(0, fileName.lastIndexOf("."));
199 * Gets file content map from zip.
201 * @param zipData the zip data
202 * @return the file content map from zip
203 * @throws IOException the io exception
205 public static FileContentHandler getFileContentMapFromZip(byte[] zipData) throws IOException {
207 try (ZipInputStream inputZipStream = new ZipInputStream(new ByteArrayInputStream(zipData))) {
209 FileContentHandler mapFileContent = new FileContentHandler();
213 while ((zipEntry = inputZipStream.getNextEntry()) != null) {
214 mapFileContent.addFile(zipEntry.getName(), FileUtils.toByteArray(inputZipStream));
217 return mapFileContent;
219 } catch (RuntimeException exception) {
220 throw new IOException(exception);
226 * The enum File extension.
228 public enum FileExtension {
231 * Json file extension.
235 * Yaml file extension.
239 * Yml file extension.
243 private String displayName;
245 FileExtension(String displayName) {
246 this.displayName = displayName;
252 * @return the display name
254 public String getDisplayName() {