push addional code
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-utilities-lib / src / main / java / org / openecomp / core / utilities / file / FileUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.core.utilities.file;
22
23 import org.openecomp.core.utilities.json.JsonUtil;
24 import org.openecomp.core.utilities.yaml.YamlUtil;
25
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;
31 import java.net.URL;
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;
37
38 /**
39  * The type File utils.
40  */
41 public class FileUtils {
42
43   /**
44    * Gets file input stream.
45    *
46    * @param fileName the file name
47    * @return the file input stream
48    */
49   public static InputStream getFileInputStream(String fileName) {
50     URL urlFile = FileUtils.class.getClassLoader().getResource(fileName);
51     InputStream is;
52     try {
53       assert urlFile != null;
54       is = urlFile.openStream();
55     } catch (IOException exception) {
56       throw new RuntimeException(exception);
57     }
58     return is;
59   }
60
61   /**
62    * Gets file input streams.
63    *
64    * @param fileName the file name
65    * @return the file input streams
66    */
67   public static List<InputStream> getFileInputStreams(String fileName) {
68     Enumeration<URL> urlFiles;
69     List<InputStream> streams = new ArrayList<>();
70     InputStream is;
71     URL url;
72     try {
73       urlFiles = FileUtils.class.getClassLoader().getResources(fileName);
74       while (urlFiles.hasMoreElements()) {
75         url = urlFiles.nextElement();
76         is = url.openStream();
77         streams.add(is);
78       }
79
80
81     } catch (IOException exception) {
82       throw new RuntimeException(exception);
83     }
84     return streams;
85   }
86
87   /**
88    * Convert to bytes byte [ ].
89    *
90    * @param object    the object
91    * @param extension the extension
92    * @return the byte [ ]
93    */
94   public static byte[] convertToBytes(Object object, FileExtension extension) {
95     if (object != null) {
96       if (extension.equals(FileExtension.YAML) || extension.equals(FileExtension.YML)) {
97         return new YamlUtil().objectToYaml(object).getBytes();
98       } else {
99         return JsonUtil.object2Json(object).getBytes();
100       }
101     } else {
102       return new byte[]{};
103     }
104   }
105
106   /**
107    * Convert to input stream input stream.
108    *
109    * @param object    the object
110    * @param extension the extension
111    * @return the input stream
112    */
113   public static InputStream convertToInputStream(Object object, FileExtension extension) {
114     if (object != null) {
115
116       byte[] content;
117
118       if (extension.equals(FileExtension.YAML) || extension.equals(FileExtension.YML)) {
119         content = new YamlUtil().objectToYaml(object).getBytes();
120       } else {
121         content = JsonUtil.object2Json(object).getBytes();
122
123       }
124       return new ByteArrayInputStream(content);
125     } else {
126       return null;
127     }
128   }
129
130   /**
131    * Load file to input stream input stream.
132    *
133    * @param fileName the file name
134    * @return the input stream
135    */
136   public static InputStream loadFileToInputStream(String fileName) {
137     URL urlFile = FileUtils.class.getClassLoader().getResource(fileName);
138     try {
139       Enumeration<URL> en = FileUtils.class.getClassLoader().getResources(fileName);
140       while (en.hasMoreElements()) {
141         urlFile = en.nextElement();
142       }
143     } catch (IOException | NullPointerException exception) {
144       throw new RuntimeException(exception);
145     }
146     try {
147       if (urlFile != null) {
148         return urlFile.openStream();
149       } else {
150         throw new RuntimeException();
151       }
152     } catch (IOException | NullPointerException exception) {
153       throw new RuntimeException(exception);
154     }
155
156   }
157
158   /**
159    * To byte array byte [ ].
160    *
161    * @param input the input
162    * @return the byte [ ]
163    */
164   public static byte[] toByteArray(InputStream input) {
165     ByteArrayOutputStream output = new ByteArrayOutputStream();
166     try {
167       copy(input, output);
168     } catch (IOException exception) {
169       throw new RuntimeException(
170           "error will convertion input stream to byte array:" + exception.getMessage());
171     }
172     return output.toByteArray();
173   }
174
175   /**
176    * Copy int.
177    *
178    * @param input  the input
179    * @param output the output
180    * @return the int
181    * @throws IOException the io exception
182    */
183   public static int copy(InputStream input, OutputStream output) throws IOException {
184     long count = copyLarge(input, output);
185     return count > 2147483647L ? -1 : (int) count;
186   }
187
188   private static long copyLarge(InputStream input, OutputStream output) throws IOException {
189     return copyLarge(input, output, new byte[4096]);
190   }
191
192   private static long copyLarge(InputStream input, OutputStream output, byte[] buffer)
193       throws IOException {
194     long count = 0L;
195
196     int n1;
197     if (input == null) {
198       return count;
199     }
200     for (; -1 != (n1 = input.read(buffer)); count += (long) n1) {
201       output.write(buffer, 0, n1);
202     }
203
204     return count;
205
206
207   }
208
209   /**
210    * Gets file without extention.
211    *
212    * @param fileName the file name
213    * @return the file without extention
214    */
215   public static String getFileWithoutExtention(String fileName) {
216     if (!fileName.contains(".")) {
217       return fileName;
218     }
219     return fileName.substring(0, fileName.lastIndexOf("."));
220   }
221
222   /**
223    * Gets file content map from zip.
224    *
225    * @param zipData the zip data
226    * @return the file content map from zip
227    * @throws IOException the io exception
228    */
229   public static FileContentHandler getFileContentMapFromZip(byte[] zipData) throws IOException {
230     ZipEntry zipEntry;
231     FileContentHandler mapFileContent = new FileContentHandler();
232     try {
233       ZipInputStream inputZipStream;
234
235       byte[] fileByteContent;
236       String currentEntryName;
237       inputZipStream = new ZipInputStream(new ByteArrayInputStream(zipData));
238
239       while ((zipEntry = inputZipStream.getNextEntry()) != null) {
240         currentEntryName = zipEntry.getName();
241         fileByteContent = FileUtils.toByteArray(inputZipStream);
242         mapFileContent.addFile(currentEntryName, fileByteContent);
243       }
244
245     } catch (RuntimeException exception) {
246       throw new IOException(exception);
247     }
248     return mapFileContent;
249   }
250
251   /**
252    * The enum File extension.
253    */
254   public enum FileExtension {
255
256     /**
257      * Json file extension.
258      */
259     JSON("json"),
260     /**
261      * Yaml file extension.
262      */
263     YAML("yaml"),
264     /**
265      * Yml file extension.
266      */
267     YML("yml");
268
269     private String displayName;
270
271     FileExtension(String displayName) {
272       this.displayName = displayName;
273     }
274
275     /**
276      * Gets display name.
277      *
278      * @return the display name
279      */
280     public String getDisplayName() {
281       return displayName;
282     }
283   }
284
285
286 }