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.core.utilities.yaml.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 streams.
64 * @param fileName the file name
65 * @return the file input streams
67 public static List<InputStream> getFileInputStreams(String fileName) {
68 Enumeration<URL> urlFiles;
69 List<InputStream> streams = new ArrayList<>();
73 urlFiles = FileUtils.class.getClassLoader().getResources(fileName);
74 while (urlFiles.hasMoreElements()) {
75 url = urlFiles.nextElement();
76 is = url.openStream();
81 } catch (IOException exception) {
82 throw new RuntimeException(exception);
88 * Convert to bytes byte [ ].
90 * @param object the object
91 * @param extension the extension
92 * @return the byte [ ]
94 public static byte[] convertToBytes(Object object, FileExtension extension) {
96 if (extension.equals(FileExtension.YAML) || extension.equals(FileExtension.YML)) {
97 return new YamlUtil().objectToYaml(object).getBytes();
99 return JsonUtil.object2Json(object).getBytes();
107 * Convert to input stream input stream.
109 * @param object the object
110 * @param extension the extension
111 * @return the input stream
113 public static InputStream convertToInputStream(Object object, FileExtension extension) {
114 if (object != null) {
118 if (extension.equals(FileExtension.YAML) || extension.equals(FileExtension.YML)) {
119 content = new YamlUtil().objectToYaml(object).getBytes();
121 content = JsonUtil.object2Json(object).getBytes();
124 return new ByteArrayInputStream(content);
131 * Load file to input stream input stream.
133 * @param fileName the file name
134 * @return the input stream
136 public static InputStream loadFileToInputStream(String fileName) {
137 URL urlFile = FileUtils.class.getClassLoader().getResource(fileName);
139 Enumeration<URL> en = FileUtils.class.getClassLoader().getResources(fileName);
140 while (en.hasMoreElements()) {
141 urlFile = en.nextElement();
143 } catch (IOException | NullPointerException exception) {
144 throw new RuntimeException(exception);
147 if (urlFile != null) {
148 return urlFile.openStream();
150 throw new RuntimeException();
152 } catch (IOException | NullPointerException exception) {
153 throw new RuntimeException(exception);
159 * To byte array byte [ ].
161 * @param input the input
162 * @return the byte [ ]
164 public static byte[] toByteArray(InputStream input) {
165 ByteArrayOutputStream output = new ByteArrayOutputStream();
168 } catch (IOException exception) {
169 throw new RuntimeException(
170 "error will convertion input stream to byte array:" + exception.getMessage());
172 return output.toByteArray();
178 * @param input the input
179 * @param output the output
181 * @throws IOException the io exception
183 public static int copy(InputStream input, OutputStream output) throws IOException {
184 long count = copyLarge(input, output);
185 return count > 2147483647L ? -1 : (int) count;
188 private static long copyLarge(InputStream input, OutputStream output) throws IOException {
189 return copyLarge(input, output, new byte[4096]);
192 private static long copyLarge(InputStream input, OutputStream output, byte[] buffer)
200 for (; -1 != (n1 = input.read(buffer)); count += (long) n1) {
201 output.write(buffer, 0, n1);
210 * Gets file without extention.
212 * @param fileName the file name
213 * @return the file without extention
215 public static String getFileWithoutExtention(String fileName) {
216 if (!fileName.contains(".")) {
219 return fileName.substring(0, fileName.lastIndexOf("."));
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 FileContentHandler mapFileContent = new FileContentHandler();
233 ZipInputStream inputZipStream;
235 byte[] fileByteContent;
236 String currentEntryName;
237 inputZipStream = new ZipInputStream(new ByteArrayInputStream(zipData));
239 while ((zipEntry = inputZipStream.getNextEntry()) != null) {
240 currentEntryName = zipEntry.getName();
241 fileByteContent = FileUtils.toByteArray(inputZipStream);
242 mapFileContent.addFile(currentEntryName, fileByteContent);
245 } catch (RuntimeException exception) {
246 throw new IOException(exception);
248 return mapFileContent;
252 * The enum File extension.
254 public enum FileExtension {
257 * Json file extension.
261 * Yaml file extension.
265 * Yml file extension.
269 private String displayName;
271 FileExtension(String displayName) {
272 this.displayName = displayName;
278 * @return the display name
280 public String getDisplayName() {