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.IOException;
32 import java.io.InputStream;
34 import java.util.Collections;
35 import java.util.Enumeration;
36 import java.util.LinkedList;
37 import java.util.List;
38 import java.util.Objects;
39 import java.util.function.Function;
40 import java.util.zip.ZipEntry;
41 import java.util.zip.ZipInputStream;
44 * The type File utils.
46 public class FileUtils {
48 //private final static Logger log = (Logger) LoggerFactory.getLogger(FileUtils.class.getName());
51 * Allows to consume an input stream open against a resource with a given file name.
53 * @param fileName the file name
54 * @param function logic to be applied to the input stream
56 public static <T> T readViaInputStream(String fileName, Function<InputStream, T> function) {
57 return readViaInputStream(FileUtils.class.getClassLoader().getResource(fileName), function);
61 * Allows to consume an input stream open against a resource with a given URL.
63 * @param urlFile the url file
64 * @param function logic to be applied to the input stream
66 public static <T> T readViaInputStream(URL urlFile, Function<InputStream, T> function) {
68 Objects.requireNonNull(urlFile);
69 try (InputStream is = urlFile.openStream()) {
70 return function.apply(is);
71 } catch (IOException exception) {
72 throw new RuntimeException(exception);
77 * Gets file input streams.
79 * @param fileName the file name
80 * @return the file input streams
82 public static List<URL> getAllLocations(String fileName) {
84 List<URL> urls = new LinkedList<>();
85 Enumeration<URL> urlFiles;
88 urlFiles = FileUtils.class.getClassLoader().getResources(fileName);
89 while (urlFiles.hasMoreElements()) {
90 urls.add(urlFiles.nextElement());
94 } catch (IOException exception) {
95 throw new RuntimeException(exception);
98 return urls.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(urls);
102 * Convert to bytes byte [ ].
104 * @param object the object
105 * @param extension the extension
106 * @return the byte [ ]
108 public static byte[] convertToBytes(Object object, FileExtension extension) {
109 if (object != null) {
110 if (extension.equals(FileExtension.YAML) || extension.equals(FileExtension.YML)) {
111 return new YamlUtil().objectToYaml(object).getBytes();
113 return JsonUtil.object2Json(object).getBytes();
121 * Convert to input stream input stream.
123 * @param object the object
124 * @param extension the extension
125 * @return the input stream
127 public static InputStream convertToInputStream(Object object, FileExtension extension) {
128 if (object != null) {
132 if (extension.equals(FileExtension.YAML) || extension.equals(FileExtension.YML)) {
133 content = new YamlUtil().objectToYaml(object).getBytes();
135 content = JsonUtil.object2Json(object).getBytes();
138 return new ByteArrayInputStream(content);
145 * Load file to input stream input stream.
147 * @param fileName the file name
148 * @return the input stream
150 public static InputStream loadFileToInputStream(String fileName) {
151 URL urlFile = Thread.currentThread().getContextClassLoader().getResource(fileName);
153 Enumeration<URL> en = Thread.currentThread().getContextClassLoader().getResources(fileName);
154 while (en.hasMoreElements()) {
155 urlFile = en.nextElement();
157 } catch (IOException | NullPointerException exception) {
158 throw new RuntimeException(exception);
161 if (urlFile != null) {
162 return urlFile.openStream();
164 throw new RuntimeException();
166 } catch (IOException | NullPointerException exception) {
167 throw new RuntimeException(exception);
173 * To byte array byte [ ].
175 * @param input the input
176 * @return the byte [ ]
178 public static byte[] toByteArray(InputStream input) {
183 return IOUtils.toByteArray(input);
184 } catch (IOException exception) {
185 throw new RuntimeException(
186 "error will convertion input stream to byte array:" + exception.getMessage());
191 * Gets file without extention.
193 * @param fileName the file name
194 * @return the file without extention
196 public static String getFileWithoutExtention(String fileName) {
197 if (!fileName.contains(".")) {
200 return fileName.substring(0, fileName.lastIndexOf("."));
203 public static String getFileExtension(String filename) {
204 return FilenameUtils.getExtension(filename);
207 public static String getNetworkPackageName(String filename){
208 String[] split = filename.split("\\.");
210 if (split != null && split.length > 1){
217 * Gets file content map from zip.
219 * @param zipData the zip data
220 * @return the file content map from zip
221 * @throws IOException the io exception
223 public static FileContentHandler getFileContentMapFromZip(byte[] zipData) throws IOException {
225 try (ZipInputStream inputZipStream = new ZipInputStream(new ByteArrayInputStream(zipData))) {
227 FileContentHandler mapFileContent = new FileContentHandler();
231 while ((zipEntry = inputZipStream.getNextEntry()) != null) {
232 mapFileContent.addFile(zipEntry.getName(), FileUtils.toByteArray(inputZipStream));
235 return mapFileContent;
237 } catch (RuntimeException exception) {
238 throw new IOException(exception);
244 * The enum File extension.
246 public enum FileExtension {
249 * Json file extension.
253 * Yaml file extension.
257 * Yml file extension.
261 private String displayName;
263 FileExtension(String displayName) {
264 this.displayName = displayName;
270 * @return the display name
272 public String getDisplayName() {