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.logging.api.Logger;
27 import org.openecomp.sdc.logging.api.LoggerFactory;
28 import org.openecomp.sdc.tosca.services.YamlUtil;
30 import java.io.ByteArrayInputStream;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.io.InputStream;
35 import java.util.Collections;
36 import java.util.Enumeration;
37 import java.util.LinkedList;
38 import java.util.List;
39 import java.util.Objects;
40 import java.util.function.Function;
41 import java.util.zip.ZipEntry;
42 import java.util.zip.ZipInputStream;
45 * The type File utils.
47 public class FileUtils {
49 //private final static Logger log = (Logger) LoggerFactory.getLogger(FileUtils.class.getName());
52 * Allows to consume an input stream open against a resource with a given file name.
54 * @param fileName the file name
55 * @param function logic to be applied to the input stream
57 public static <T> T readViaInputStream(String fileName, Function<InputStream, T> function) {
59 Objects.requireNonNull(fileName);
61 // the leading slash doesn't make sense and doesn't work when used with a class loader
62 URL resource = FileUtils.class.getClassLoader().getResource(fileName.startsWith("/") ? fileName.substring(1) : fileName);
63 if (resource == null) {
64 throw new IllegalArgumentException("Resource not found: " + fileName);
67 return readViaInputStream(resource, function);
71 * Allows to consume an input stream open against a resource with a given URL.
73 * @param urlFile the url file
74 * @param function logic to be applied to the input stream
76 public static <T> T readViaInputStream(URL urlFile, Function<InputStream, T> function) {
78 Objects.requireNonNull(urlFile);
79 try (InputStream is = urlFile.openStream()) {
80 return function.apply(is);
81 } catch (IOException exception) {
82 throw new RuntimeException(exception);
87 * Gets file input streams.
89 * @param fileName the file name
90 * @return the file input streams
92 public static List<URL> getAllLocations(String fileName) {
94 List<URL> urls = new LinkedList<>();
95 Enumeration<URL> urlFiles;
98 urlFiles = FileUtils.class.getClassLoader().getResources(fileName);
99 while (urlFiles.hasMoreElements()) {
100 urls.add(urlFiles.nextElement());
104 } catch (IOException exception) {
105 throw new RuntimeException(exception);
108 return urls.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(urls);
112 * Convert to bytes byte [ ].
114 * @param object the object
115 * @param extension the extension
116 * @return the byte [ ]
118 public static byte[] convertToBytes(Object object, FileExtension extension) {
119 if (object != null) {
120 if (extension.equals(FileExtension.YAML) || extension.equals(FileExtension.YML)) {
121 return new YamlUtil().objectToYaml(object).getBytes();
123 return JsonUtil.object2Json(object).getBytes();
131 * Convert to input stream input stream.
133 * @param object the object
134 * @param extension the extension
135 * @return the input stream
137 public static InputStream convertToInputStream(Object object, FileExtension extension) {
138 if (object != null) {
142 if (extension.equals(FileExtension.YAML) || extension.equals(FileExtension.YML)) {
143 content = new YamlUtil().objectToYaml(object).getBytes();
145 content = JsonUtil.object2Json(object).getBytes();
148 return new ByteArrayInputStream(content);
155 * Load file to input stream input stream.
157 * @param fileName the file name
158 * @return the input stream
160 public static InputStream loadFileToInputStream(String fileName) {
161 URL urlFile = Thread.currentThread().getContextClassLoader().getResource(fileName);
163 Enumeration<URL> en = Thread.currentThread().getContextClassLoader().getResources(fileName);
164 while (en.hasMoreElements()) {
165 urlFile = en.nextElement();
167 } catch (IOException | NullPointerException exception) {
168 throw new RuntimeException(exception);
171 if (urlFile != null) {
172 return urlFile.openStream();
174 throw new RuntimeException();
176 } catch (IOException | NullPointerException exception) {
177 throw new RuntimeException(exception);
183 * To byte array byte [ ].
185 * @param input the input
186 * @return the byte [ ]
188 public static byte[] toByteArray(InputStream input) {
193 return IOUtils.toByteArray(input);
194 } catch (IOException exception) {
195 throw new RuntimeException(
196 "error will convertion input stream to byte array:" + exception.getMessage());
201 * Gets file without extention.
203 * @param fileName the file name
204 * @return the file without extention
206 public static String getFileWithoutExtention(String fileName) {
207 if (!fileName.contains(".")) {
210 return fileName.substring(0, fileName.lastIndexOf("."));
213 public static String getFileExtension(String filename) {
214 return FilenameUtils.getExtension(filename);
217 public static String getNetworkPackageName(String filename){
218 String[] split = filename.split("\\.");
220 if (split != null && split.length > 1){
227 * Gets file content map from zip.
229 * @param zipData the zip data
230 * @return the file content map from zip
231 * @throws IOException the io exception
233 public static FileContentHandler getFileContentMapFromZip(byte[] zipData) throws IOException {
235 try (ZipInputStream inputZipStream = new ZipInputStream(new ByteArrayInputStream(zipData))) {
237 FileContentHandler mapFileContent = new FileContentHandler();
241 while ((zipEntry = inputZipStream.getNextEntry()) != null) {
242 mapFileContent.addFile(zipEntry.getName(), FileUtils.toByteArray(inputZipStream));
245 return mapFileContent;
247 } catch (RuntimeException exception) {
248 throw new IOException(exception);
254 * The enum File extension.
256 public enum FileExtension {
259 * Json file extension.
263 * Yaml file extension.
267 * Yml file extension.
271 private String displayName;
273 FileExtension(String displayName) {
274 this.displayName = displayName;
280 * @return the display name
282 public String getDisplayName() {