0f07dc507397d415b1d2b40b4ef8ae4bc1892cad
[sdc/sdc-workflow-designer.git] /
1 /**
2  * Copyright (c) 2018 ZTE Corporation.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the Apache License, Version 2.0
5  * and the Eclipse Public License v1.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     ZTE - initial API and implementation and/or initial documentation
11  */
12
13 package org.onap.sdc.workflowdesigner.utils;
14
15 import java.io.BufferedReader;
16 import java.io.File;
17 import java.io.FileOutputStream;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.InputStreamReader;
21 import java.io.OutputStream;
22 import java.io.Reader;
23 import java.io.Writer;
24 import java.nio.file.Files;
25 import java.nio.file.Paths;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import org.apache.commons.io.IOUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * common utility class.
35  * 
36  */
37 public class FileCommonUtils {
38   private static final Logger logger = LoggerFactory.getLogger(FileCommonUtils.class);
39
40   /**
41    * @param ins
42    */
43   public static void closeInputStream(InputStream ins) {
44     if (ins != null) {
45       try {
46         ins.close();
47       } catch (IOException e) {
48         logger.info("Close InputStream failed.", e);
49       }
50     }
51   }
52   
53
54   /**
55    * 
56    * @param os
57    */
58   public static void closeOutputStream(OutputStream os) {
59     if (os != null) {
60       try {
61         os.close();
62       } catch (IOException e) {
63         logger.info("Close OutputStream failed.", e);
64       }
65     }
66   }
67   
68
69   /**
70    * @param reader
71    */
72   public static void closeReader(Reader reader) {
73     if (reader != null) {
74       try {
75         reader.close();
76       } catch (IOException e) {
77         logger.info("Close Reader failed.", e);
78       }
79     }
80   }
81   
82
83   /**
84    * 
85    * @param writer
86    */
87   public static void closeWriter(Writer writer) {
88     if (writer != null) {
89       try {
90         writer.close();
91       } catch (IOException e) {
92         logger.info("Close Writer failed.", e);
93       }
94     }
95   }
96
97
98   /**
99    * 
100    * @param ins
101    * @return
102    * @throws IOException
103    */
104   public static String[] readLines(InputStream ins) throws IOException {
105     InputStreamReader insReader = new InputStreamReader(ins);
106     BufferedReader reader = new BufferedReader(insReader);
107
108     List<String> lineList = new ArrayList<>();
109     String line;
110     try {
111       while ((line = reader.readLine()) != null) {
112         lineList.add(line);
113       }
114     } finally {
115       closeReader(reader);
116       closeReader(insReader);
117     }
118
119     return lineList.toArray(new String[0]);
120   }
121   
122
123   /**
124    * 
125    * @param ins
126    * @return
127    * @throws IOException
128    */
129   public static String readString(InputStream ins) throws IOException {
130     return IOUtils.toString(ins, "UTF-8");
131   }
132   
133
134   /**
135    * 
136    * @param filePath
137    * @return
138    * @throws IOException
139    */
140   public static String readString(String filePath) throws IOException {
141     InputStream ins = null;
142     try {
143       ins = Files.newInputStream(Paths.get(filePath));
144       return readString(ins);
145     } finally {
146       closeInputStream(ins);
147     }
148   }
149
150   
151   /**
152    * 
153    * @param ins
154    * @param path
155    * @param fileName
156    * @return
157    * @throws IOException
158    */
159   public static String saveFile(InputStream ins, String path, String fileName) throws IOException {
160     File tmpPath = new File(path);
161     if (!tmpPath.exists()) {
162       tmpPath.mkdirs();
163     }
164
165     File file = new File(path + File.separator + fileName);
166     OutputStream os = null;
167     try {
168       int read = 0;
169       byte[] bytes = new byte[1024];
170       os = new FileOutputStream(file, false);
171       while ((read = ins.read(bytes)) != -1) {
172         os.write(bytes, 0, read);
173       }
174       os.flush();
175       return file.getAbsolutePath();
176     } finally {
177       closeOutputStream(os);
178     }
179   }
180   
181
182   /**
183    * 
184    * @param path
185    * @param fileName
186    * @param content
187    * @throws IOException
188    */
189   public static void writetoAbsoluteFile(String path, String fileName, String content)
190       throws IOException {
191     writetoAbsoluteFile(path, fileName, content, FileCommonConstants.DEFAULT_CHARSET_NAME);
192   }
193   
194
195   /**
196    * 
197    * @param path
198    * @param fileName
199    * @param content
200    * @param charsetName
201    * @throws IOException
202    */
203   public static void writetoAbsoluteFile(String path, String fileName, String content,
204       String charsetName) throws IOException {
205     write(path, fileName, content, charsetName);
206   }
207   
208
209   /**
210    * 
211    * @param fileName
212    * @param s
213    * @throws IOException
214    */
215   public static void write(String fileName, String s) throws IOException {
216     write(".", fileName, s, FileCommonConstants.DEFAULT_CHARSET_NAME);
217
218   }
219   
220
221   /**
222    * 
223    * @param path
224    * @param fileName
225    * @param s
226    * @param charsetName
227    * @throws IOException
228    */
229   public static void write(String path, String fileName, String s, String charsetName)
230       throws IOException {
231     File tmpPath = new File(path);
232     if (!tmpPath.exists()) {
233       tmpPath.mkdirs();
234     }
235
236     String absolutePath = path + File.separator + fileName;
237     FileOutputStream out = null;
238     try {
239       out = new FileOutputStream(absolutePath);
240       out.write(s.getBytes(charsetName));
241       out.close();
242     } finally {
243       closeOutputStream(out);
244     }
245   }
246   
247
248   /**
249    * 
250    * @param fileName
251    * @param s
252    * @param charsetName
253    * @throws IOException
254    */
255   public static void write(String fileName, String s, String charsetName) throws IOException {
256     write(".", fileName, s, charsetName);
257   }
258
259   
260   /**
261    * 
262    * @param fileName
263    * @param ss
264    * @throws IOException
265    */
266   public static void write(String fileName, String[] ss) throws IOException {
267     write(fileName, ss, FileCommonConstants.DEFAULT_CHARSET_NAME);
268   }
269   
270
271   /**
272    * 
273    * @param fileName
274    * @param ss
275    * @param charsetName
276    * @throws IOException
277    */
278   public static void write(String fileName, String[] ss, String charsetName) throws IOException {
279     StringBuilder sb = new StringBuilder();
280     for (int i = 0; i < ss.length; i++) {
281       sb.append(ss[i]).append(System.lineSeparator());
282     }
283
284     write(fileName, sb.toString(), charsetName);
285   }
286
287 }