]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/org/splat/util/IOUtils.java
Salome HOME
The draft of the "Copy from existing study" action is added. The YACS step is introdu...
[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                         FileOutputStream outputStream = new FileOutputStream(target);
121                         try {
122
123                                 copy(inputStream, outputStream);
124                                 outputStream.flush();
125
126                         } finally {
127                                 outputStream.close();
128                         }
129
130                 } finally {
131                         inputStream.close();
132                 }
133         }
134
135         /**
136          * Copy a source file to a target file defined by their paths.
137          * 
138          * @param srcPath
139          *            source file path
140          * @param resPath
141          *            target file path
142          * @throws IOException
143          *             if reading or writing is failed
144          */
145         static public void copy(final String srcPath, final String resPath)
146                         throws IOException {
147
148                 FileInputStream inputStream = new FileInputStream(srcPath);
149                 try {
150
151                         FileOutputStream outputStream = new FileOutputStream(resPath);
152                         try {
153
154                                 copy(inputStream, outputStream);
155                                 outputStream.flush();
156
157                         } finally {
158                                 outputStream.close();
159                         }
160
161                 } finally {
162                         inputStream.close();
163                 }
164         }
165
166         /**
167          * Copy data from URL to a file.
168          * 
169          * @param srcUrl
170          *            source URL string
171          * @param resFilePath
172          *            result file path
173          * @throws IOException
174          *             if reading or writing is failed
175          */
176         static public void copyURLtoFile(final String srcUrl,
177                         final String resFilePath) throws IOException {
178
179                 URL url = new URL(srcUrl);
180
181                 InputStream inputStream = url.openStream();
182                 try {
183
184                         FileOutputStream res = new FileOutputStream(resFilePath);
185                         try {
186
187                                 IOUtils.copy(inputStream, res);
188                                 res.flush();
189
190                         } finally {
191                                 res.close();
192                         }
193
194                 } finally {
195                         inputStream.close();
196                 }
197         }
198
199         /**
200          * Delete the content of the folder or delete the file. If it is a folder <BR>
201          * then delete all children before.
202          * 
203          * @param file
204          *            the file or folder to delete
205          * @return return true if succeeded, otherwise return false
206          */
207         static public boolean delete(final File file) {
208                 if (file.isDirectory()) {
209                         for (File child : file.listFiles()) {
210                                 delete(child);
211                         }
212                 }
213                 return file.delete();
214         }
215 }