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.openecomp.core.utilities.json.JsonUtil;
24 import org.openecomp.sdc.tosca.services.YamlUtil;
26 import java.io.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.OutputStream;
32 import java.util.ArrayList;
33 import java.util.Enumeration;
34 import java.util.List;
35 import java.util.zip.ZipEntry;
36 import java.util.zip.ZipInputStream;
39 * The type File utils.
41 public class FileUtils {
44 * Gets file input stream.
46 * @param fileName the file name
47 * @return the file input stream
49 public static InputStream getFileInputStream(String fileName) {
50 URL urlFile = FileUtils.class.getClassLoader().getResource(fileName);
53 assert urlFile != null;
54 is = urlFile.openStream();
55 } catch (IOException exception) {
56 throw new RuntimeException(exception);
62 * Gets file input stream.
64 * @param urlFile the url file
65 * @return the file input stream
67 public static InputStream getFileInputStream(URL urlFile) {
70 assert urlFile != null;
71 is = urlFile.openStream();
72 } catch (IOException exception) {
73 throw new RuntimeException(exception);
79 * Gets file input streams.
81 * @param fileName the file name
82 * @return the file input streams
84 public static List<InputStream> getFileInputStreams(String fileName) {
85 Enumeration<URL> urlFiles;
86 List<InputStream> streams = new ArrayList<>();
90 urlFiles = FileUtils.class.getClassLoader().getResources(fileName);
91 while (urlFiles.hasMoreElements()) {
92 url = urlFiles.nextElement();
93 is = url.openStream();
98 } catch (IOException exception) {
99 throw new RuntimeException(exception);
105 * Convert to bytes byte [ ].
107 * @param object the object
108 * @param extension the extension
109 * @return the byte [ ]
111 public static byte[] convertToBytes(Object object, FileExtension extension) {
112 if (object != null) {
113 if (extension.equals(FileExtension.YAML) || extension.equals(FileExtension.YML)) {
114 return new YamlUtil().objectToYaml(object).getBytes();
116 return JsonUtil.object2Json(object).getBytes();
124 * Convert to input stream input stream.
126 * @param object the object
127 * @param extension the extension
128 * @return the input stream
130 public static InputStream convertToInputStream(Object object, FileExtension extension) {
131 if (object != null) {
135 if (extension.equals(FileExtension.YAML) || extension.equals(FileExtension.YML)) {
136 content = new YamlUtil().objectToYaml(object).getBytes();
138 content = JsonUtil.object2Json(object).getBytes();
141 return new ByteArrayInputStream(content);
148 * Load file to input stream input stream.
150 * @param fileName the file name
151 * @return the input stream
153 public static InputStream loadFileToInputStream(String fileName) {
154 URL urlFile = Thread.currentThread().getContextClassLoader().getResource(fileName);
156 Enumeration<URL> en = Thread.currentThread().getContextClassLoader().getResources(fileName);
157 while (en.hasMoreElements()) {
158 urlFile = en.nextElement();
160 } catch (IOException | NullPointerException exception) {
161 throw new RuntimeException(exception);
164 if (urlFile != null) {
165 return urlFile.openStream();
167 throw new RuntimeException();
169 } catch (IOException | NullPointerException exception) {
170 throw new RuntimeException(exception);
176 * To byte array byte [ ].
178 * @param input the input
179 * @return the byte [ ]
181 public static byte[] toByteArray(InputStream input) {
182 ByteArrayOutputStream output = new ByteArrayOutputStream();
185 } catch (IOException exception) {
186 throw new RuntimeException(
187 "error will convertion input stream to byte array:" + exception.getMessage());
189 return output.toByteArray();
195 * @param input the input
196 * @param output the output
198 * @throws IOException the io exception
200 public static int copy(InputStream input, OutputStream output) throws IOException {
201 long count = copyLarge(input, output);
202 return count > 2147483647L ? -1 : (int) count;
205 private static long copyLarge(InputStream input, OutputStream output) throws IOException {
206 return copyLarge(input, output, new byte[4096]);
209 private static long copyLarge(InputStream input, OutputStream output, byte[] buffer)
217 for (; -1 != (n1 = input.read(buffer)); count += (long) n1) {
218 output.write(buffer, 0, n1);
227 * Gets file without extention.
229 * @param fileName the file name
230 * @return the file without extention
232 public static String getFileWithoutExtention(String fileName) {
233 if (!fileName.contains(".")) {
236 return fileName.substring(0, fileName.lastIndexOf("."));
240 * Gets file content map from zip.
242 * @param zipData the zip data
243 * @return the file content map from zip
244 * @throws IOException the io exception
246 public static FileContentHandler getFileContentMapFromZip(byte[] zipData) throws IOException {
248 FileContentHandler mapFileContent = new FileContentHandler();
250 ZipInputStream inputZipStream;
252 byte[] fileByteContent;
253 String currentEntryName;
254 inputZipStream = new ZipInputStream(new ByteArrayInputStream(zipData));
256 while ((zipEntry = inputZipStream.getNextEntry()) != null) {
257 currentEntryName = zipEntry.getName();
258 fileByteContent = FileUtils.toByteArray(inputZipStream);
259 mapFileContent.addFile(currentEntryName, fileByteContent);
262 } catch (RuntimeException exception) {
263 throw new IOException(exception);
265 return mapFileContent;
270 * The enum File extension.
272 public enum FileExtension {
275 * Json file extension.
279 * Yaml file extension.
283 * Yml file extension.
287 private String displayName;
289 FileExtension(String displayName) {
290 this.displayName = displayName;
296 * @return the display name
298 public String getDisplayName() {