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.FilenameUtils;
24 import org.apache.commons.io.IOUtils;
25 import org.openecomp.core.utilities.json.JsonUtil;
26 import org.openecomp.sdc.tosca.services.YamlUtil;
28 import java.io.ByteArrayInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
32 import java.util.Collections;
33 import java.util.Enumeration;
34 import java.util.LinkedList;
35 import java.util.List;
36 import java.util.Objects;
37 import java.util.function.Function;
38 import java.util.zip.ZipEntry;
39 import java.util.zip.ZipInputStream;
42 * The type File utils.
44 public class FileUtils {
47 * Allows to consume an input stream open against a resource with a given file name.
49 * @param fileName the file name
50 * @param function logic to be applied to the input stream
52 public static <T> T readViaInputStream(String fileName, Function<InputStream, T> function) {
54 Objects.requireNonNull(fileName);
56 // the leading slash doesn't make sense and doesn't work when used with a class loader
57 URL resource = FileUtils.class.getClassLoader().getResource(fileName.startsWith("/")
58 ? fileName.substring(1) : fileName);
59 if (resource == null) {
60 throw new IllegalArgumentException("Resource not found: " + fileName);
63 return readViaInputStream(resource, function);
67 * Allows to consume an input stream open against a resource with a given URL.
69 * @param urlFile the url file
70 * @param function logic to be applied to the input stream
72 public static <T> T readViaInputStream(URL urlFile, Function<InputStream, T> function) {
74 Objects.requireNonNull(urlFile);
75 try (InputStream is = urlFile.openStream()) {
76 return function.apply(is);
77 } catch (IOException exception) {
78 throw new RuntimeException(exception);
83 * Gets file input streams.
85 * @param fileName the file name
86 * @return the file input streams
88 public static List<URL> getAllLocations(String fileName) {
90 List<URL> urls = new LinkedList<>();
91 Enumeration<URL> urlFiles;
94 urlFiles = FileUtils.class.getClassLoader().getResources(fileName);
95 while (urlFiles.hasMoreElements()) {
96 urls.add(urlFiles.nextElement());
100 } catch (IOException exception) {
101 throw new RuntimeException(exception);
104 return urls.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(urls);
108 * Convert to bytes byte [ ].
110 * @param object the object
111 * @param extension the extension
112 * @return the byte [ ]
114 public static byte[] convertToBytes(Object object, FileExtension extension) {
115 if (object != null) {
116 if (extension.equals(FileExtension.YAML) || extension.equals(FileExtension.YML)) {
117 return new YamlUtil().objectToYaml(object).getBytes();
119 return JsonUtil.object2Json(object).getBytes();
127 * Convert to input stream input stream.
129 * @param object the object
130 * @param extension the extension
131 * @return the input stream
133 public static InputStream convertToInputStream(Object object, FileExtension extension) {
134 if (object != null) {
138 if (extension.equals(FileExtension.YAML) || extension.equals(FileExtension.YML)) {
139 content = new YamlUtil().objectToYaml(object).getBytes();
141 content = JsonUtil.object2Json(object).getBytes();
144 return new ByteArrayInputStream(content);
151 * Load file to input stream input stream.
153 * @param fileName the file name
154 * @return the input stream
156 public static InputStream loadFileToInputStream(String fileName) {
157 URL urlFile = Thread.currentThread().getContextClassLoader().getResource(fileName);
159 Enumeration<URL> en = Thread.currentThread().getContextClassLoader().getResources(fileName);
160 while (en.hasMoreElements()) {
161 urlFile = en.nextElement();
163 } catch (IOException | NullPointerException exception) {
164 throw new RuntimeException(exception);
167 if (urlFile != null) {
168 return urlFile.openStream();
170 throw new RuntimeException();
172 } catch (IOException | NullPointerException exception) {
173 throw new RuntimeException(exception);
179 * To byte array byte [ ].
181 * @param input the input
182 * @return the byte [ ]
184 public static byte[] toByteArray(InputStream input) {
189 return IOUtils.toByteArray(input);
190 } catch (IOException exception) {
191 throw new RuntimeException(
192 "error will convertion input stream to byte array:" + exception.getMessage());
197 * Gets file without extention.
199 * @param fileName the file name
200 * @return the file without extention
202 public static String getFileWithoutExtention(String fileName) {
203 if (!fileName.contains(".")) {
206 return fileName.substring(0, fileName.lastIndexOf("."));
209 public static String getFileExtension(String filename) {
210 return FilenameUtils.getExtension(filename);
213 public static String getNetworkPackageName(String filename) {
214 String[] split = filename.split("\\.");
216 if (split.length > 1) {
223 * Gets file content map from zip.
225 * @param zipData the zip data
226 * @return the file content map from zip
227 * @throws IOException the io exception
229 public static FileContentHandler getFileContentMapFromZip(byte[] zipData) throws IOException {
231 try (ZipInputStream inputZipStream = new ZipInputStream(new ByteArrayInputStream(zipData))) {
233 FileContentHandler mapFileContent = new FileContentHandler();
237 while ((zipEntry = inputZipStream.getNextEntry()) != null) {
238 mapFileContent.addFile(zipEntry.getName(), FileUtils.toByteArray(inputZipStream));
241 return mapFileContent;
243 } catch (RuntimeException exception) {
244 throw new IOException(exception);
250 * The enum File extension.
252 public enum FileExtension {
255 * Json file extension.
259 * Yaml file extension.
263 * Yml file extension.
267 private String displayName;
269 FileExtension(String displayName) {
270 this.displayName = displayName;
276 * @return the display name
278 public String getDisplayName() {