Salome HOME
Creation of a new study from an existing one is implemented.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / util / IOUtils.java
1 /*****************************************************************************
2  * Company         OPEN CASCADE
3  * Application     SIMAN
4  * File            $Id$ 
5  * Creation date   21.03.2013
6  * @author         $Author$
7  * @version        $Revision$
8  * @copyright      OPEN CASCADE 2012
9  *****************************************************************************/
10 package org.splat.util;
11
12 import java.io.File;
13 import java.io.FileInputStream;
14 import java.io.FileOutputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.io.Reader;
19 import java.io.Writer;
20 import java.net.URL;
21
22 /**
23  * Class for working with files.
24  */
25 public final class IOUtils {
26
27         /**
28          * Private constructor.
29          */
30         private IOUtils() {
31
32         }
33
34         /**
35          * Copy a source stream to a target stream using a buffer of 1024 bytes.
36          * 
37          * @param src
38          *            source stream
39          * @param target
40          *            target stream
41          * 
42          * @throws IOException
43          *             if reading or writing is failed
44          */
45         static public void copy(final InputStream src, final OutputStream target)
46                         throws IOException {
47
48                 byte[] buffer = new byte[1024];
49
50                 for (int i = src.read(buffer); i != -1; i = src.read(buffer)) {
51                         target.write(buffer, 0, i);
52                 }
53
54                 target.flush();
55         }
56
57         /**
58          * Copy a source stream to a target file defined by its path.
59          * 
60          * @param src
61          *            source stream
62          * @param targetFilePath
63          *            target file path
64          * @throws IOException
65          *             if reading or writing is failed
66          */
67         static public void copy(final InputStream src, final String targetFilePath)
68                         throws IOException {
69
70                 FileOutputStream outputStream = new FileOutputStream(targetFilePath);
71                 try {
72
73                         copy(src, outputStream);
74                         outputStream.flush();
75
76                 } finally {
77                         outputStream.close();
78                 }
79
80         }
81
82         /**
83          * Copy a Reader to a Writer using a buffer of 1024.
84          * 
85          * @param src
86          *            source reader
87          * @param res
88          *            result writer
89          * @throws IOException
90          *             if reading or writing is failed
91          */
92         static public void copy(final Reader src, final Writer res)
93                         throws IOException {
94
95                 char[] buffer = new char[1024];
96
97                 for (int i = src.read(buffer); i != -1; i = src.read(buffer)) {
98                         res.write(buffer, 0, i);
99                 }
100
101                 res.flush();
102         }
103
104         /**
105          * Copy a source file to a target file.
106          * 
107          * @param src
108          *            source file
109          * @param target
110          *            target file
111          * @throws IOException
112          *             if reading or writing is failed
113          */
114         static public void copy(final File src, final File target)
115                         throws IOException {
116
117                 FileInputStream inputStream = new FileInputStream(src);
118                 try {
119
120                         // Create target directory if necessary
121                         if (!target.getParentFile().exists()) {
122                                 target.getParentFile().mkdirs();
123                         }
124                         FileOutputStream outputStream = new FileOutputStream(target);
125                         try {
126
127                                 copy(inputStream, outputStream);
128                                 outputStream.flush();
129
130                         } finally {
131                                 outputStream.close();
132                         }
133
134                 } finally {
135                         inputStream.close();
136                 }
137         }
138
139         /**
140          * Copy a source file to a target file defined by their paths.
141          * 
142          * @param srcPath
143          *            source file path
144          * @param resPath
145          *            target file path
146          * @throws IOException
147          *             if reading or writing is failed
148          */
149         static public void copy(final String srcPath, final String resPath)
150                         throws IOException {
151
152                 FileInputStream inputStream = new FileInputStream(srcPath);
153                 try {
154
155                         FileOutputStream outputStream = new FileOutputStream(resPath);
156                         try {
157
158                                 copy(inputStream, outputStream);
159                                 outputStream.flush();
160
161                         } finally {
162                                 outputStream.close();
163                         }
164
165                 } finally {
166                         inputStream.close();
167                 }
168         }
169
170         /**
171          * Copy data from URL to a file.
172          * 
173          * @param srcUrl
174          *            source URL string
175          * @param resFilePath
176          *            result file path
177          * @throws IOException
178          *             if reading or writing is failed
179          */
180         static public void copyURLtoFile(final String srcUrl,
181                         final String resFilePath) throws IOException {
182
183                 URL url = new URL(srcUrl);
184
185                 InputStream inputStream = url.openStream();
186                 try {
187
188                         FileOutputStream res = new FileOutputStream(resFilePath);
189                         try {
190
191                                 IOUtils.copy(inputStream, res);
192                                 res.flush();
193
194                         } finally {
195                                 res.close();
196                         }
197
198                 } finally {
199                         inputStream.close();
200                 }
201         }
202
203         /**
204          * Delete the content of the folder or delete the file. If it is a folder <BR>
205          * then delete all children before.
206          * 
207          * @param file
208          *            the file or folder to delete
209          * @return return true if succeeded, otherwise return false
210          */
211         static public boolean delete(final File file) {
212                 if (file.isDirectory()) {
213                         for (File child : file.listFiles()) {
214                                 delete(child);
215                         }
216                 }
217                 return file.delete();
218         }
219 }