Clover Coverage Report
Coverage timestamp: Sat Sep 18 2010 04:09:52 UTC
../../../../img/srcFileCovDistChart7.png 67% of files have more coverage
421   1,312   182   7.8
156   763   0.43   54
54     3.37  
1    
 
  IoUtility       Line # 61 421 182 68% 0.6798732
 
  (91)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2010, Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10    * This program is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    * GNU General Public License for more details.
14    */
15   
16    package org.qedeq.base.io;
17   
18    import java.io.BufferedOutputStream;
19    import java.io.BufferedReader;
20    import java.io.BufferedWriter;
21    import java.io.ByteArrayInputStream;
22    import java.io.File;
23    import java.io.FileFilter;
24    import java.io.FileInputStream;
25    import java.io.FileOutputStream;
26    import java.io.FileReader;
27    import java.io.FileWriter;
28    import java.io.IOException;
29    import java.io.InputStream;
30    import java.io.InputStreamReader;
31    import java.io.OutputStream;
32    import java.io.OutputStreamWriter;
33    import java.io.Reader;
34    import java.io.UnsupportedEncodingException;
35    import java.io.Writer;
36    import java.lang.reflect.Field;
37    import java.net.MalformedURLException;
38    import java.net.URL;
39    import java.net.URLDecoder;
40    import java.nio.charset.Charset;
41    import java.util.ArrayList;
42    import java.util.Arrays;
43    import java.util.Enumeration;
44    import java.util.Iterator;
45    import java.util.List;
46    import java.util.Map;
47    import java.util.Properties;
48    import java.util.StringTokenizer;
49    import java.util.TreeMap;
50   
51    import org.apache.commons.lang.SystemUtils;
52   
53   
54    /**
55    * A collection of useful static methods for input and output.
56    *
57    * LATER mime 20070101: use StringBuilder instead of StringBuffer if working under JDK 1.5
58    *
59    * @author Michael Meyling
60    */
 
61    public final class IoUtility {
62   
63    /**
64    * Constructor, should never be called.
65    */
 
66  0 toggle private IoUtility() {
67    // don't call me
68    }
69   
70    /**
71    * Get default encoding for this system.
72    *
73    * @return Default encoding for this system.
74    */
 
75  7 toggle public static String getDefaultEncoding() {
76  7 return SystemUtils.FILE_ENCODING;
77    // mime 20090630: under ubuntu the following gave the encoding ASCII:
78    // return new InputStreamReader(
79    // new ByteArrayInputStream(new byte[0])).getEncoding();
80    // but it was: file.encoding="ANSI_X3.41968"
81    }
82   
83    /**
84    * Get working Java encoding.
85    *
86    * @param encoding Try this encoding.
87    * @return This is <code>encoding</code> if it is supported. Or an other
88    * encoding that is supported by this system.
89    */
 
90  5 toggle public static String getWorkingEncoding(final String encoding) {
91  5 if (encoding != null) {
92  4 try {
93  4 if (Charset.isSupported(encoding)
94    && Charset.forName(encoding).canEncode()) {
95  3 return encoding;
96    }
97    } catch (RuntimeException e) {
98    // ignore
99    }
100    }
101    // we must inform someone, but using
102    // Trace within this class is not wise, because it is used
103    // before the Trace is initialized.
104  2 System.err.println("not supported encoding: " + encoding);
105  2 return "ISO-8859-1"; // every system must support this
106    }
107   
108    /**
109    * Reads a file and returns the contents as a <code>String</code>.
110    *
111    * @param filename Name of the file (could include path).
112    * @param encoding Take this encoding.
113    * @return Contents of file.
114    * @throws IOException File exception occurred.
115    */
 
116  4 toggle public static String loadFile(final String filename, final String encoding)
117    throws IOException {
118   
119  4 final StringBuffer buffer = new StringBuffer();
120  4 loadFile(filename, buffer, encoding);
121  4 return buffer.toString();
122    }
123   
124    /**
125    * Reads contents of a file into a string buffer.
126    *
127    * @param filename Name of the file (could include path).
128    * @param buffer Buffer to fill with file contents.
129    * @param encoding Take this encoding.
130    * @throws IOException File exception occurred.
131    */
 
132  4 toggle public static void loadFile(final String filename,
133    final StringBuffer buffer, final String encoding)
134    throws IOException {
135  4 loadFile(new File(filename), buffer, encoding);
136    }
137   
138    /**
139    * Reads contents of a stream into a string buffer. Stream is not closed.
140    *
141    * @param in This stream will be loaded.
142    * @param buffer Buffer to fill with file contents.
143    * @throws IOException File exception occurred.
144    *
145    * @deprecated Use {@link #loadReader(Reader, StringBuffer)}.
146    */
 
147  1 toggle public static void loadStream(final InputStream in, final StringBuffer buffer)
148    throws IOException {
149   
150  1 buffer.setLength(0);
151  1 int c;
152  ? while ((c = in.read()) >= 0) {
153  37 buffer.append((char) c);
154    }
155    }
156   
157    /**
158    * Returns contents of a stream into a string, respecting a maximum length.
159    * No exceptions are thrown. Stream is not closed.
160    *
161    * @param in This stream will be loaded.
162    * @param maxLength This length is not exceeded.
163    * @return readData Data read, is not <code>null</code>.
164    */
 
165  10 toggle public static String loadStreamWithoutException(final InputStream in, final int maxLength) {
166   
167  10 if (in == null) {
168  2 return "";
169    }
170  8 final StringBuffer buffer = new StringBuffer();
171  8 buffer.setLength(0);
172  8 try {
173  8 int counter = 0;
174  8 int c;
175  6115 while (counter++ < maxLength) {
176  6109 c = in.read();
177  6109 if (c < 0) {
178  2 break;
179    }
180  6107 buffer.append((char) c);
181    }
182    } catch (IOException e) {
183    // ignored
184    } catch (RuntimeException e) {
185    // ignored
186    }
187  8 return buffer.toString();
188    }
189   
190    /**
191    * Reads contents of a {@link Reader} into a string buffer. Reader is not closed.
192    *
193    * @param in This reader will be loaded.
194    * @param buffer Buffer to fill with file contents.
195    * @throws IOException File exception occurred.
196    */
 
197  3 toggle public static void loadReader(final Reader in, final StringBuffer buffer)
198    throws IOException {
199   
200  3 buffer.setLength(0);
201  3 int c;
202  ? while ((c = in.read()) >= 0) {
203  7392 buffer.append((char) c);
204    }
205    }
206   
207    /**
208    * Reads contents of a file into a string buffer. Uses default encoding.
209    *
210    * @param file This file will be loaded.
211    * @param buffer Buffer to fill with file contents.
212    * @throws IOException File exception occurred.
213    *
214    * @deprecated Use {@link #loadFile(File, StringBuffer, String)}.
215    */
 
216  1 toggle public static void loadFile(final File file,
217    final StringBuffer buffer)
218    throws IOException {
219   
220  1 final int size = (int) file.length();
221  1 final char[] data = new char[size];
222  1 buffer.setLength(0);
223  1 FileReader in = null;
224  1 try {
225  1 in = new FileReader(file);
226  1 int charsread = 0;
227  2 while (charsread < size) {
228  1 charsread += in.read(data, charsread, size - charsread);
229    }
230    } finally {
231  1 close(in);
232    }
233  1 buffer.insert(0, data);
234    }
235   
236    /**
237    * Reads contents of a file into a string buffer.
238    *
239    * @param file This file will be loaded.
240    * @param buffer Buffer to fill with file contents.
241    * @param encoding Take this encoding.
242    * @throws IOException File exception occurred.
243    */
 
244  8 toggle public static void loadFile(final File file,
245    final StringBuffer buffer, final String encoding)
246    throws IOException {
247   
248  8 buffer.setLength((int) file.length()); // ensure capacity
249  8 buffer.setLength(0);
250  8 final InputStreamReader in = new InputStreamReader(new FileInputStream(file), encoding);
251  8 final char[] data = new char[10 * 1024];
252   
253  8 try {
254  8 int charsread = 0;
255  ? while (0 < (charsread = in.read(data, 0, data.length))) {
256  8 buffer.append(data, 0, charsread);
257    }
258    } finally {
259  8 in.close();
260    }
261    }
262   
263    /**
264    * Reads a file and returns the contents as a <code>String</code>.
265    *
266    * @param file File to load from.
267    * @return Contents of file.
268    * @throws IOException File exception occurred.
269    */
 
270  4 toggle public static final byte[] loadFileBinary(final File file) throws IOException {
271  4 final int size = (int) file.length();
272  4 final FileInputStream in = new FileInputStream(file);
273  4 try {
274  4 final byte[] data = new byte[size];
275  4 int charsread = 0;
276  8 while (charsread < size) {
277  4 final int read = in.read(data, charsread, size - charsread);
278  4 if (read == -1) {
279  0 final byte[] result = new byte[charsread];
280  0 System.arraycopy(data, 0, result, 0, charsread);
281  0 return result;
282    }
283  4 charsread += read;
284    }
285  4 in.close();
286  4 return data;
287    } finally {
288  4 close(in);
289    }
290    }
291   
292   
293    /**
294    * Reads contents of an URL into a string buffer. The filling is character set dependent.
295    * Content is added to the end of buffer. (Existing data is not cleared.)
296    * <p>
297    * All parameters should not be <code>null</code>.
298    * @param url This URL will be loaded.
299    * @param buffer Buffer to fill with file contents.
300    * @throws IOException Reading failed.
301    *
302    * @deprecated Choose correct encoding.
303    */
 
304  1 toggle public static void loadFile(final URL url, final StringBuffer buffer) throws IOException {
305  1 InputStream in = null;
306  1 BufferedReader dis = null;
307  1 try {
308  1 in = url.openStream();
309  1 dis = new BufferedReader(new InputStreamReader(in));
310  1 int i;
311  ? while ((i = dis.read()) != -1) {
312  37 buffer.append((char) i);
313    }
314    } finally {
315  1 close(in);
316  1 close(dis);
317    }
318    }
319   
320    /**
321    * Reads contents of an URL into a StringBuffer. The filling is character set dependent. The
322    * buffer is not cleared, contents is just added.
323    * <p>
324    * All parameters should not be <code>null</code>.
325    * @param url This URL will be loaded.
326    * @param buffer Buffer to fill with file contents.
327    * @param encoding Take this encoding.
328    * @throws IOException Reading failed.
329    */
 
330  3 toggle public static void loadFile(final URL url, final StringBuffer buffer, final String encoding)
331    throws IOException {
332  3 InputStream in = null;
333  3 BufferedReader dis = null;
334  3 try {
335  3 in = url.openStream();
336  3 dis = new BufferedReader(new InputStreamReader(in, encoding));
337  3 int i;
338  ? while ((i = dis.read()) != -1) {
339  6072 buffer.append((char) i);
340    }
341    } finally {
342  3 close(in);
343  3 close(dis);
344    }
345    }
346   
347    /**
348    * Save binary contents of an URL into a file. Existing files are overwritten.
349    *
350    * @param url This URL will be loaded.
351    * @param file Write into this file.
352    * @throws IOException Reading or writing failed.
353    */
 
354  2 toggle public static void saveFile(final URL url, final File file) throws IOException {
355  2 saveFile(url.openStream(), file);
356    }
357   
358    /**
359    * Save binary contents of an input stream into a file. The input stream is closed even
360    * if exceptions occur. Existing files are overwritten.
361    * @param in Read this stream.
362    * @param file Write into this file.
363    *
364    * @throws IOException Reading or writing failed.
365    */
 
366  27 toggle public static void saveFile(final InputStream in, final File file) throws IOException {
367  27 FileOutputStream out = null;
368  27 try {
369  27 out = new FileOutputStream(file);
370  27 final byte[] data = new byte[8 * 1024];
371  27 int length;
372  ? while ((length = in.read(data)) != -1) {
373  113 out.write(data, 0, length);
374    }
375    } finally {
376  27 close(in);
377  27 close(out);
378    }
379    }
380   
381    /**
382    * Convert String into a {@link Reader}.
383    *
384    * <a href="http://bugs.sun.com/bugdatabase/view_bug.do;:WuuT?bug_id=4094886">
385    * Bug ID: 4094886</a>
386    *
387    * @param data Convert this.
388    * @return Resulting reader.
389    */
 
390  138 toggle public static final Reader stringToReader(final String data) {
391  138 try {
392  138 return new InputStreamReader(new ByteArrayInputStream(data.getBytes("ISO-8859-1")));
393    } catch (UnsupportedEncodingException e) {
394  0 throw new RuntimeException(e);
395    }
396    }
397   
398    /**
399    * Saves a <code>String</code> into a file. Existing files are overwritten.
400    *
401    * @param filename Name of the file (could include path).
402    * @param text Data to save in the file.
403    * @throws IOException File exception occurred.
404    *
405    * @deprecated Use {@link #saveFile(File, String, String)} that has an encoding.
406    */
 
407  1 toggle public static void saveFile(final String filename, final String text)
408    throws IOException {
409  1 saveFile(new File(filename), text);
410    }
411   
412    /**
413    * Saves a <code>StringBuffer</code> in a file. Existing files are overwritten.
414    *
415    * @param filename Name of the file (could include path).
416    * @param text Data to save in the file.
417    * @throws IOException File exception occurred.
418    *
419    * @deprecated Use {@link #saveFile(File, StringBuffer, String)} that has an encoding.
420    */
 
421  1 toggle public static void saveFile(final String filename, final StringBuffer text)
422    throws IOException {
423  1 saveFile(new File(filename), text.toString());
424    }
425   
426    /**
427    * Saves a <code>StringBuffer</code> in a file. Existing files are overwritten.
428    *
429    * @param file File to save into.
430    * @param text Data to save in the file.
431    * @throws IOException File exception occurred.
432    *
433    * @deprecated Use {@link #saveFile(File, StringBuffer, String)} that has an encoding
434    * parameter.
435    */
 
436  1 toggle public static void saveFile(final File file, final StringBuffer text)
437    throws IOException {
438  1 saveFile(file, text.toString());
439    }
440   
441    /**
442    * Saves a <code>String</code> in a file. Uses default encoding. Existing files are
443    * overwritten.
444    *
445    * @param file File to save the data in.
446    * @param text Data to save in the file.
447    * @throws IOException File exception occurred.
448    *
449    * @deprecated Use {@link #saveFile(File, String, String)} that has an encoding parameter.
450    */
 
451  4 toggle public static void saveFile(final File file, final String text)
452    throws IOException {
453  4 BufferedWriter out = null;
454  4 try {
455  4 out = new BufferedWriter(new FileWriter(file));
456  4 out.write(text);
457    } finally {
458  4 close(out);
459    }
460    }
461   
462    /**
463    * Saves a <code>String</code> in a file. Existing files are overwritten.
464    *
465    * @param file File to save the data in.
466    * @param text Data to save in the file.
467    * @param encoding Use this encoding.
468    * @throws IOException File exception occurred.
469    */
 
470  2 toggle public static void saveFile(final File file, final StringBuffer text, final String encoding)
471    throws IOException {
472  2 saveFile(file, text.toString(), encoding);
473    }
474   
475    /**
476    * Saves a <code>String</code> in a file.
477    *
478    * @param file File to save the data in.
479    * @param text Data to save in the file.
480    * @param encoding Use this encoding.
481    * @throws IOException File exception occurred.
482    */
 
483  41 toggle public static void saveFile(final File file, final String text, final String encoding)
484    throws IOException {
485  41 BufferedWriter out = new BufferedWriter(
486    new OutputStreamWriter(new FileOutputStream(file), encoding));
487  41 try {
488  41 out.write(text);
489    } finally {
490  41 out.close();
491    }
492    }
493   
494    /**
495    * Saves a <code>data</code> in a file. Existing files are overwritten.
496    *
497    * @param file File to save the data in.
498    * @param data Data to save in the file.
499    * @throws IOException File exception occurred.
500    */
 
501  19 toggle public static void saveFileBinary(final File file, final byte[] data)
502    throws IOException {
503  19 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
504  19 try {
505  19 out.write(data);
506    } finally {
507  19 out.close();
508    }
509    }
510   
511    /**
512    * Copies a file to a different location.
513    *
514    * @param from Copy source.
515    * @param to Copy destination.
516    * @throws IOException File exception occurred.
517    */
 
518  56 toggle public static void copyFile(final File from, final File to)
519    throws IOException {
520   
521  56 if (from.getCanonicalFile().equals(to.getCanonicalFile())) {
522  1 return;
523    }
524  55 createNecessaryDirectories(to);
525  55 FileInputStream in = null;
526  55 FileOutputStream out = null;
527  55 try {
528  55 in = new FileInputStream(from);
529  55 out = new FileOutputStream(to);
530   
531  55 byte[] data = new byte[8 * 1024];
532  55 int length;
533  ? while ((length = in.read(data