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.ArrayList;
32 import java.util.Enumeration;
33 import java.util.List;
34 import java.util.zip.ZipEntry;
35 import java.util.zip.ZipInputStream;
38 * The type File utils.
40 public class FileUtils {
43 * Gets file input stream.
45 * @param fileName the file name
46 * @return the file input stream
48 public static InputStream getFileInputStream(String fileName) {
49 URL urlFile = FileUtils.class.getClassLoader().getResource(fileName);
52 assert urlFile != null;
53 is = urlFile.openStream();
54 } catch (IOException exception) {
55 throw new RuntimeException(exception);
61 * Gets file input stream.
63 * @param urlFile the url file
64 * @return the file input stream
66 public static InputStream getFileInputStream(URL urlFile) {
69 assert urlFile != null;
70 is = urlFile.openStream();
71 } catch (IOException exception) {
72 throw new RuntimeException(exception);
78 * Gets file input streams.
80 * @param fileName the file name
81 * @return the file input streams
83 public static List<InputStream> getFileInputStreams(String fileName) {
84 Enumeration<URL> urlFiles;
85 List<InputStream> streams = new ArrayList<>();
89 urlFiles = FileUtils.class.getClassLoader().getResources(fileName);
90 while (urlFiles.hasMoreElements()) {
91 url = urlFiles.nextElement();
92 is = url.openStream();
97 } catch (IOException exception) {
98 throw new RuntimeException(exception);
104 * Convert to bytes byte [ ].
106 * @param object the object
107 * @param extension the extension
108 * @return the byte [ ]
110 public static byte[] convertToBytes(Object object, FileExtension extension) {
111 if (object != null) {
112 if (extension.equals(FileExtension.YAML) || extension.equals(FileExtension.YML)) {
113 return new YamlUtil().objectToYaml(object).getBytes();
115 return JsonUtil.object2Json(object).getBytes();
123 * Convert to input stream input stream.
125 * @param object the object
126 * @param extension the extension
127 * @return the input stream
129 public static InputStream convertToInputStream(Object object, FileExtension extension) {
130 if (object != null) {
134 if (extension.equals(FileExtension.YAML) || extension.equals(FileExtension.YML)) {
135 content = new YamlUtil().objectToYaml(object).getBytes();
137 content = JsonUtil.object2Json(object).getBytes();
140 return new ByteArrayInputStream(content);
147 * Load file to input stream input stream.
149 * @param fileName the file name
150 * @return the input stream
152 public static InputStream loadFileToInputStream(String fileName) {
153 URL urlFile = Thread.currentThread().getContextClassLoader().getResource(fileName);
155 Enumeration<URL> en = Thread.currentThread().getContextClassLoader().getResources(fileName);
156 while (en.hasMoreElements()) {
157 urlFile = en.nextElement();
159 } catch (IOException | NullPointerException exception) {
160 throw new RuntimeException(exception);
163 if (urlFile != null) {
164 return urlFile.openStream();
166 throw new RuntimeException();
168 } catch (IOException | NullPointerException exception) {
169 throw new RuntimeException(exception);
175 * To byte array byte [ ].
177 * @param input the input
178 * @return the byte [ ]
180 public static byte[] toByteArray(InputStream input) {
185 return IOUtils.toByteArray(input);
186 } catch (IOException exception) {
187 throw new RuntimeException(
188 "error will convertion input stream to byte array:" + exception.getMessage());
193 * Gets file without extention.
195 * @param fileName the file name
196 * @return the file without extention
198 public static String getFileWithoutExtention(String fileName) {
199 if (!fileName.contains(".")) {
202 return fileName.substring(0, fileName.lastIndexOf("."));
206 * Gets file content map from zip.
208 * @param zipData the zip data
209 * @return the file content map from zip
210 * @throws IOException the io exception
212 public static FileContentHandler getFileContentMapFromZip(byte[] zipData) throws IOException {
214 FileContentHandler mapFileContent = new FileContentHandler();
216 ZipInputStream inputZipStream;
218 byte[] fileByteContent;
219 String currentEntryName;
220 inputZipStream = new ZipInputStream(new ByteArrayInputStream(zipData));
222 while ((zipEntry = inputZipStream.getNextEntry()) != null) {
223 currentEntryName = zipEntry.getName();
224 fileByteContent = FileUtils.toByteArray(inputZipStream);
225 mapFileContent.addFile(currentEntryName, fileByteContent);
228 } catch (RuntimeException exception) {
229 throw new IOException(exception);
231 return mapFileContent;
236 * The enum File extension.
238 public enum FileExtension {
241 * Json file extension.
245 * Yaml file extension.
249 * Yml file extension.
253 private String displayName;
255 FileExtension(String displayName) {
256 this.displayName = displayName;
262 * @return the display name
264 public String getDisplayName() {