Mercurial > hg > mpdl-group
comparison software/eXist/mpdl-modules/src/de/mpg/mpiwg/berlin/mpdl/util/FileUtil.java @ 0:408254cf2f1d
Erstellung
author | Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de> |
---|---|
date | Wed, 24 Nov 2010 17:24:23 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:408254cf2f1d |
---|---|
1 package de.mpg.mpiwg.berlin.mpdl.util; | |
2 | |
3 import java.io.BufferedInputStream; | |
4 import java.io.BufferedOutputStream; | |
5 import java.io.File; | |
6 import java.io.FileInputStream; | |
7 import java.io.FileNotFoundException; | |
8 import java.io.FileOutputStream; | |
9 import java.io.FilenameFilter; | |
10 import java.io.IOException; | |
11 import java.io.InputStream; | |
12 import java.io.OutputStream; | |
13 import java.io.RandomAccessFile; | |
14 import java.net.MalformedURLException; | |
15 import java.net.URI; | |
16 import java.net.URL; | |
17 import java.net.URLConnection; | |
18 import java.util.ArrayList; | |
19 import java.util.List; | |
20 | |
21 import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException; | |
22 | |
23 public class FileUtil { | |
24 private static FileUtil instance; | |
25 | |
26 public static FileUtil getInstance() throws ApplicationException { | |
27 if (instance == null) { | |
28 instance = new FileUtil(); | |
29 } | |
30 return instance; | |
31 } | |
32 | |
33 public void createDirectory(String dir) { | |
34 File destLocalDirectory = new File(dir); | |
35 destLocalDirectory.mkdirs(); | |
36 } | |
37 | |
38 public void deleteDirectory(String dir) { | |
39 File destLocalDirectory = new File(dir); | |
40 // directory with all files and subdirectories is deleted | |
41 deleteDirectory(destLocalDirectory); | |
42 } | |
43 | |
44 /** | |
45 * Deletes all files and subdirectories under dir. No exception is thrown | |
46 * if dir (or one of its children) does nor exist. | |
47 * @dir dir the directory to be deleted | |
48 * @return true if all deletions were successful. If a deletion fails, the method stops attempting to delete and returns false. | |
49 */ | |
50 public boolean deleteDirectory(File dir) { | |
51 if (dir.isDirectory()) { | |
52 String[] children = dir.list(); | |
53 for (int i=0; i<children.length; i++) { | |
54 boolean success = deleteDirectory(new File(dir, children[i])); | |
55 if (!success) { | |
56 return false; // if one of the files or subdirectories could not be deleted return false but recursively works further | |
57 } | |
58 } | |
59 } | |
60 // The directory is now empty so delete it | |
61 return dir.delete(); | |
62 } | |
63 | |
64 public void deleteFile(String fileName) { | |
65 File destLocalFile = new File(fileName); | |
66 // if destLocalFile does not exist nothing happens and no exception is thrown | |
67 // if destLocalFile is a directory and is not empty an exception is thrown (should not happen because this method is called only by remove) | |
68 destLocalFile.delete(); | |
69 } | |
70 | |
71 /** | |
72 * List all files in a directory | |
73 * @param dirStr | |
74 * @return | |
75 */ | |
76 public File[] getFiles(String dirStr) { | |
77 File dir = new File(dirStr); | |
78 File[] dirFiles = dir.listFiles(); | |
79 return dirFiles; | |
80 } | |
81 | |
82 /** | |
83 * | |
84 * @param dirStr | |
85 * @param filter | |
86 * @return | |
87 */ | |
88 public File[] getFiles(String dirStr, FilenameFilter filter) { | |
89 File dir = new File(dirStr); | |
90 File[] dirFiles = dir.listFiles(filter); | |
91 return dirFiles; | |
92 } | |
93 | |
94 /** | |
95 * Write all bytes into destFile. If directory for that destFile does not exist | |
96 * it creates this directory including parent directories. | |
97 * @param bytes bytes to write | |
98 * @param destFileName destination file name | |
99 * @throws Exception | |
100 */ | |
101 public void saveFile(byte[] bytes, String destFileName) throws ApplicationException { | |
102 OutputStream out = null; | |
103 try { | |
104 if (bytes == null) | |
105 return; // do nothing | |
106 File destFile = new File(destFileName); | |
107 File destDir = new File(destFile.getParent()); | |
108 if (! destDir.exists()) { | |
109 destDir.mkdirs(); // create the directory including parent directories which do not exist | |
110 } | |
111 out = new BufferedOutputStream(new FileOutputStream(destFile)); | |
112 out.write(bytes, 0, bytes.length); | |
113 out.flush(); | |
114 } catch (FileNotFoundException e) { | |
115 throw new ApplicationException(e); | |
116 } catch (IOException e) { | |
117 throw new ApplicationException(e); | |
118 } finally { | |
119 try { | |
120 if (out != null) | |
121 out.close(); | |
122 } catch (Exception e) { | |
123 // nothing: always close the stream at the end of the method | |
124 } | |
125 } | |
126 } | |
127 | |
128 public void copyFile(String srcFileName, String destFileName) throws ApplicationException { | |
129 InputStream in = null; | |
130 OutputStream out = null; | |
131 try { | |
132 File srcFile = new File(srcFileName); | |
133 if (! srcFile.exists()) | |
134 return; // do nothing | |
135 File destFile = new File(destFileName); | |
136 File destDir = new File(destFile.getParent()); | |
137 if (! destDir.exists()) { | |
138 destDir.mkdirs(); // create the directory including parent directories which do not exist | |
139 } | |
140 in = new BufferedInputStream(new FileInputStream(srcFile)); | |
141 out = new BufferedOutputStream(new FileOutputStream(destFile)); | |
142 int bufLen = 20000*1024; | |
143 byte[] buf = new byte[bufLen]; | |
144 int len = 0; | |
145 while ((len = in.read(buf)) > 0) { | |
146 out.write(buf, 0, len); | |
147 } | |
148 out.flush(); // buffered content is flushed to file | |
149 } catch (FileNotFoundException e) { | |
150 throw new ApplicationException(e); | |
151 } catch (IOException e) { | |
152 throw new ApplicationException(e); | |
153 } finally { | |
154 try { | |
155 if (in != null) | |
156 in.close(); | |
157 if (out != null) | |
158 out.close(); | |
159 } catch (Exception e) { | |
160 // nothing: always close the stream at the end of the method | |
161 } | |
162 } | |
163 } | |
164 | |
165 public void saveUrlToLocalFile(URL srcUrl, String destFileName) throws ApplicationException { | |
166 BufferedInputStream in = null; | |
167 BufferedOutputStream out = null; | |
168 try { | |
169 /* wenn ein Zugriff mit "http:" gemacht wird, wird die XML Deklaration (<?xml version="1.0"?>) nicht ausgelesen | |
170 * beim Zugriff mit "file;" ist das anders | |
171 * evtl. wieder einbauen, um die Deklaration manuell zu schreiben | |
172 URLConnection urlConn = srcUrl.openConnection(); | |
173 String contentTypeStr = urlConn.getContentType(); | |
174 String contentEncodingStr = urlConn.getContentEncoding(); | |
175 boolean contentTypeXml = false; | |
176 if (contentTypeStr != null) { | |
177 contentTypeStr = contentTypeStr.toLowerCase(); | |
178 if (contentTypeStr.indexOf("application/xml") != -1 || contentTypeStr.indexOf("text/xml") != -1) | |
179 contentTypeXml = true; | |
180 } | |
181 */ | |
182 InputStream inputStream = srcUrl.openStream(); | |
183 in = new BufferedInputStream(inputStream); | |
184 File outputFile = new File(destFileName); | |
185 File outputDir = new File(outputFile.getParent()); | |
186 if (! outputDir.exists()) { | |
187 outputDir.mkdirs(); // create the directory including parent directories which do not exist | |
188 } | |
189 out = new BufferedOutputStream(new FileOutputStream(outputFile)); | |
190 int bufLen = 1000*1024; | |
191 byte[] buf = new byte[bufLen]; | |
192 int len = 0; | |
193 /* | |
194 if (contentTypeXml) { | |
195 String xmlDecl = "<?xml version=\"1.0\"?>\n"; | |
196 out.write(xmlDecl.getBytes("utf-8")); | |
197 } | |
198 */ | |
199 while ((len = in.read(buf)) > 0) { | |
200 out.write(buf, 0, len); | |
201 out.flush(); | |
202 } | |
203 } catch (MalformedURLException e) { | |
204 throw new ApplicationException(e); | |
205 } catch (IOException e) { | |
206 throw new ApplicationException(e); | |
207 } finally { | |
208 try { | |
209 if (in != null) | |
210 in.close(); | |
211 if (out != null) | |
212 out.close(); | |
213 } catch (Exception e) { | |
214 // nothing: always close the stream at the end of the method | |
215 } | |
216 } | |
217 } | |
218 | |
219 public void saveInputStreamToLocalFile(InputStream srcInputStream, String destFileName) throws ApplicationException { | |
220 BufferedInputStream in = null; | |
221 BufferedOutputStream out = null; | |
222 try { | |
223 in = new BufferedInputStream(srcInputStream); | |
224 File outputFile = new File(destFileName); | |
225 File outputDir = new File(outputFile.getParent()); | |
226 if (! outputDir.exists()) { | |
227 outputDir.mkdirs(); // create the directory including parent directories which do not exist | |
228 } | |
229 out = new BufferedOutputStream(new FileOutputStream(outputFile)); | |
230 int bufLen = 1000*1024; | |
231 byte[] buf = new byte[bufLen]; | |
232 int len = 0; | |
233 while ((len = in.read(buf)) > 0) { | |
234 out.write(buf, 0, len); | |
235 out.flush(); | |
236 } | |
237 } catch (MalformedURLException e) { | |
238 throw new ApplicationException(e); | |
239 } catch (IOException e) { | |
240 throw new ApplicationException(e); | |
241 } finally { | |
242 try { | |
243 if (in != null) | |
244 in.close(); | |
245 if (out != null) | |
246 out.close(); | |
247 } catch (Exception e) { | |
248 // nothing: always close the stream at the end of the method | |
249 } | |
250 } | |
251 } | |
252 | |
253 public void deleteLastNBytes(File file, int countBytes) throws ApplicationException { | |
254 try { | |
255 RandomAccessFile raf = new RandomAccessFile(file, "rw"); | |
256 long length = raf.length(); | |
257 raf.setLength(length - countBytes); | |
258 raf.close(); | |
259 } catch (IOException e) { | |
260 throw new ApplicationException(e); | |
261 } | |
262 } | |
263 | |
264 public void testFile(String fileName) throws ApplicationException { | |
265 File file = new File(fileName); | |
266 boolean fileExists = file.exists(); | |
267 if (! fileExists) { | |
268 throw new ApplicationException("File: " + fileName + " does not exist"); | |
269 } | |
270 } | |
271 | |
272 /** | |
273 * Reads a chunk of data of an input stream. | |
274 * Does not close the stream until last bytes are read | |
275 * @in in the input stream to be read | |
276 * @chunkSize chunkSize length of the chunk which is read | |
277 * @return byte[] of bytes read | |
278 */ | |
279 public byte[] readBytes(InputStream in, int chunkSize) throws ApplicationException { | |
280 byte[] resultBytes = new byte[chunkSize]; | |
281 try { | |
282 int len = in.read(resultBytes, 0, chunkSize); | |
283 if (len == -1) { | |
284 try { in.close(); } catch (Exception e) { } // close the stream if end of file is reached | |
285 resultBytes = null; | |
286 } else if (len < chunkSize && len != chunkSize) { // if read chunk is last chunk of the file it delivers this chunk | |
287 byte[] tmp = new byte[len]; | |
288 System.arraycopy(resultBytes, 0, tmp, 0, len); | |
289 resultBytes = tmp; | |
290 } | |
291 } catch (FileNotFoundException e) { | |
292 throw new ApplicationException(e); | |
293 } catch (IOException e) { | |
294 throw new ApplicationException(e); | |
295 } | |
296 return resultBytes; | |
297 } | |
298 | |
299 /** | |
300 * Reads a file storing intermediate data into an array. | |
301 * @file file the file to be read | |
302 * @return byte[] of file content | |
303 */ | |
304 public byte[] readBytes(String fileName) throws ApplicationException { | |
305 InputStream in = null; | |
306 byte[] out = new byte[0]; | |
307 try { | |
308 in = new BufferedInputStream(new FileInputStream(fileName)); | |
309 // the length of a buffer can vary | |
310 int bufLen = 20000*1024; | |
311 byte[] buf = new byte[bufLen]; | |
312 byte[] tmp = null; | |
313 int len = 0; | |
314 while((len = in.read(buf, 0, bufLen)) != -1) { | |
315 // extend array | |
316 tmp = new byte[out.length + len]; | |
317 System.arraycopy(out, 0, tmp, 0, out.length); | |
318 System.arraycopy(buf, 0, tmp, out.length, len); | |
319 out = tmp; | |
320 tmp = null; | |
321 } | |
322 } catch (FileNotFoundException e) { | |
323 throw new ApplicationException(e); | |
324 } catch (IOException e) { | |
325 throw new ApplicationException(e); | |
326 } finally { | |
327 // always close the stream | |
328 if (in != null) try { in.close(); } catch (Exception e) { } | |
329 } | |
330 return out; | |
331 } | |
332 | |
333 public String getMimeType(String fileName) throws ApplicationException { | |
334 String mimeType = null; | |
335 File file = new File(fileName); | |
336 try { | |
337 URI uri = file.toURI(); | |
338 URL url = uri.toURL(); | |
339 URLConnection urlConnection = url.openConnection(); | |
340 mimeType = urlConnection.getContentType(); | |
341 } catch (MalformedURLException e) { | |
342 throw new ApplicationException(e); | |
343 } catch (IOException e) { | |
344 throw new ApplicationException(e); | |
345 } | |
346 return mimeType; | |
347 } | |
348 | |
349 /** | |
350 * Reads a file storing intermediate data into an array. | |
351 * @file file the file to be read | |
352 * @return byte array of the file content | |
353 * TODO test this method if it is really faster | |
354 */ | |
355 private byte[] readBytesFast(String file) throws ApplicationException { | |
356 InputStream in = null; | |
357 byte[] buf = null; | |
358 int bufLen = 20000*1024; | |
359 try { | |
360 in = new BufferedInputStream(new FileInputStream(file)); | |
361 buf = new byte[bufLen]; | |
362 byte[] tmp = null; | |
363 int len = 0; | |
364 List data = new ArrayList(24); // keeps pieces of data | |
365 while((len = in.read(buf, 0, bufLen)) != -1){ | |
366 tmp = new byte[len]; | |
367 System.arraycopy(buf, 0, tmp, 0, len); // still need to do copy | |
368 data.add(tmp); | |
369 } | |
370 /* This part os optional. This method could return a List data | |
371 for further processing, etc. */ | |
372 len = 0; | |
373 if (data.size() == 1) return (byte[]) data.get(0); | |
374 for (int i=0;i<data.size();i++) len += ((byte[]) data.get(i)).length; | |
375 buf = new byte[len]; // final output buffer | |
376 len = 0; | |
377 for (int i=0;i<data.size();i++){ // fill with data | |
378 tmp = (byte[]) data.get(i); | |
379 System.arraycopy(tmp,0,buf,len,tmp.length); | |
380 len += tmp.length; | |
381 } | |
382 } catch (FileNotFoundException e) { | |
383 throw new ApplicationException(e); | |
384 } catch (IOException e) { | |
385 throw new ApplicationException(e); | |
386 } finally { | |
387 if (in != null) try { in.close(); } catch (Exception e) {} | |
388 } | |
389 return buf; | |
390 } | |
391 | |
392 /* | |
393 * | |
394 Insert a document (today.rss as 20050401) by a URL connection (PUT request): | |
395 | |
396 URL u = "http://www.cafeaulait.org/today.rss"; | |
397 InputStream in = u.openStream(); | |
398 URL u = new URL("http://eliza.elharo.com:8080/exist/servlet/db/syndication/20050401"); | |
399 HttpURLConnection conn = (HttpURLConnection) u.openConnection(); | |
400 conn.setDoOutput(true); | |
401 conn.setRequestMethod("PUT"); | |
402 conn.setHeaderField("Content-type", "application/xml"); | |
403 OutputStream out = conn.getOutputStream(); | |
404 for (int c = in.read(); c != -1; c = in.read()) { | |
405 out.write(c); | |
406 } | |
407 out.flush(); | |
408 out.close(); | |
409 in.close(); | |
410 // read the response... | |
411 | |
412 Delete a document (20050401) by a URL connection: | |
413 | |
414 URL u = new URL("http://eliza.elharo.com:8080/exist/servlet/db/syndication/20050401"); | |
415 HttpURLConnection conn = (HttpURLConnection) u.openConnection(); | |
416 conn.setRequestMethod("DELETE"); | |
417 conn.connect(); | |
418 // read the response... | |
419 * | |
420 * | |
421 */ | |
422 | |
423 | |
424 } |