Clover Coverage Report
Coverage timestamp: Sa Aug 2 2008 13:56:27 CEST
../../../../img/srcFileCovDistChart4.png 82% of files have more coverage
291   986   124   6,77
102   545   0,43   43
43     2,88  
1    
 
  IoUtility       Line # 57 291 124 37,6% 0.3761468
 
  (44)
 
1    /* $Id: IoUtility.java,v 1.2 2008/08/02 04:29:50 m31 Exp $
2    *
3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
4    *
5    * Copyright 2000-2008, Michael Meyling <mime@qedeq.org>.
6    *
7    * "Hilbert II" is free software; you can redistribute
8    * it and/or modify it under the terms of the GNU General Public
9    * License as published by the Free Software Foundation; either
10    * version 2 of the License, or (at your option) any later version.
11    *
12    * This program is distributed in the hope that it will be useful,
13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15    * GNU General Public License for more details.
16    */
17   
18    package org.qedeq.base.io;
19   
20    import java.io.BufferedOutputStream;
21    import java.io.BufferedReader;
22    import java.io.BufferedWriter;
23    import java.io.ByteArrayInputStream;
24    import java.io.File;
25    import java.io.FileFilter;
26    import java.io.FileInputStream;
27    import java.io.FileOutputStream;
28    import java.io.FileReader;
29    import java.io.FileWriter;
30    import java.io.IOException;
31    import java.io.InputStream;
32    import java.io.InputStreamReader;
33    import java.io.OutputStream;
34    import java.io.OutputStreamWriter;
35    import java.io.Reader;
36    import java.io.UnsupportedEncodingException;
37    import java.lang.reflect.Field;
38    import java.net.MalformedURLException;
39    import java.net.URL;
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.List;
45    import java.util.Properties;
46    import java.util.StringTokenizer;
47   
48   
49    /**
50    * A collection of useful static methods for input and output.
51    *
52    * LATER mime 20070101: use StringBuilder instead of StringBuffer if working under JDK 1.5
53    *
54    * @version $Revision: 1.2 $
55    * @author Michael Meyling
56    */
 
57    public final class IoUtility {
58   
59    /**
60    * Constructor, should never be called.
61    */
 
62  0 toggle private IoUtility() {
63    // don't call me
64    }
65   
66    /**
67    * Get default encoding for this system.
68    *
69    * @return Default encoding for this system.
70    */
 
71  2 toggle public static String getDefaultEncoding() {
72  2 return new InputStreamReader(
73    new ByteArrayInputStream(new byte[0])).getEncoding();
74    }
75   
76   
77    /**
78    * Get working Java encoding.
79    *
80    * @param encoding Try this encoding.
81    * @return This is <code>encoding</code> if it is supported. Or an other
82    * encoding that is supported by this system.
83    */
 
84  0 toggle public static String getWorkingEncoding(final String encoding) {
85  0 if (encoding != null) {
86  0 try {
87  0 if (Charset.isSupported(encoding)
88    && Charset.forName(encoding).canEncode()) {
89  0 return encoding;
90    }
91    } catch (RuntimeException e) {
92    // ignore
93    }
94    }
95    // TODO mime 20080309: we must inform someone, but using
96    // Trace within this class is not wise, because it is used
97    // before the Trace is initialized.
98  0 System.err.println("not supported encoding: " + encoding);
99  0 return "ISO-8859-1"; // every system must support this
100    }
101   
102    /**
103    * Reads a file and returns the contents as a <code>String</code>.
104    *
105    * @param filename Name of the file (could include path).
106    * @param encoding Take this encoding.
107    * @return Contents of file.
108    * @throws IOException File exception occurred.
109    */
 
110  0 toggle public static String loadFile(final String filename, final String encoding)
111    throws IOException {
112   
113  0 final StringBuffer buffer = new StringBuffer();
114  0 loadFile(filename, buffer, encoding);
115  0 return buffer.toString();
116    }
117   
118    /**
119    * Reads contents of a file into a string buffer.
120    *
121    * @param filename Name of the file (could include path).
122    * @param buffer Buffer to fill with file contents.
123    * @param encoding Take this encoding.
124    * @throws IOException File exception occurred.
125    */
 
126  0 toggle public static void loadFile(final String filename,
127    final StringBuffer buffer, final String encoding)
128    throws IOException {
129  0 loadFile(new File(filename), buffer, encoding);
130    }
131   
132    /**
133    * Reads contents of a stream into a string buffer.
134    *
135    * @param in This stream will be loaded.
136    * @param buffer Buffer to fill with file contents.
137    * @throws IOException File exception occurred.
138    *
139    * @deprecated Use {@link #loadReader(Reader, StringBuffer)}.
140    */
 
141  0 toggle public static void loadStream(final InputStream in, final StringBuffer buffer)
142    throws IOException {
143   
144  0 buffer.setLength(0);
145  0 int c;
146  0 while ((c = in.read()) >= 0) {
147  0 buffer.append((char) c);
148    }
149    }
150   
151    /**
152    * Reads contents of a {@link Reader} into a string buffer.
153    *
154    * @param in This reader will be loaded.
155    * @param buffer Buffer to fill with file contents.
156    * @throws IOException File exception occurred.
157    */
 
158  2 toggle public static void loadReader(final Reader in, final StringBuffer buffer)
159    throws IOException {
160   
161  2 buffer.setLength(0);
162  2 int c;
163  ? while ((c = in.read()) >= 0) {
164  7322 buffer.append((char) c);
165    }
166    }
167   
168    /**
169    * Reads contents of a file into a string buffer. Uses default encoding.
170    *
171    * @param file This file will be loaded.
172    * @param buffer Buffer to fill with file contents.
173    * @throws IOException File exception occurred.
174    *
175    * @deprecated Use {@link #loadFile(File, StringBuffer, String)}.
176    */
 
177  0 toggle public static void loadFile(final File file,
178    final StringBuffer buffer)
179    throws IOException {
180   
181  0 final int size = (int) file.length();
182  0 buffer.setLength(0);
183  0 final FileReader in = new FileReader(file);
184  0 final char[] data = new char[size];
185  0 int charsread = 0;
186  0 while (charsread < size) {
187  0 charsread += in.read(data, charsread, size - charsread);
188    }
189  0 in.close();
190  0 buffer.insert(0, data);
191    }
192   
193    /**
194    * Reads contents of a file into a string buffer.
195    *
196    * @param file This file will be loaded.
197    * @param buffer Buffer to fill with file contents.
198    * @param encoding Take this encoding.
199    * @throws IOException File exception occurred.
200    */
 
201  1 toggle public static void loadFile(final File file,
202    final StringBuffer buffer, final String encoding)
203    throws IOException {
204   
205  1 buffer.setLength((int) file.length()); // ensure capacity
206  1 buffer.setLength(0);
207  1 final InputStreamReader in = new InputStreamReader(new FileInputStream(file), encoding);
208  1 final char[] data = new char[10 * 1024];
209   
210  1 try {
211  1 int charsread = 0;
212  ? while (0 < (charsread = in.read(data, 0, data.length))) {
213  1 buffer.append(data, 0, charsread);
214    }
215    } finally {
216  1 in.close();
217    }
218    }
219   
220    /**
221    * Reads a file and returns the contents as a <code>String</code>.
222    *
223    * @param file File to load from.
224    * @return Contents of file.
225    * @throws IOException File exception occurred.
226    */
 
227  0 toggle public static final byte[] loadFileBinary(final File file) throws IOException {
228  0 final int size = (int) file.length();
229  0 final FileInputStream in = new FileInputStream(file);
230  0 try {
231  0 final byte[] data = new byte[size];
232  0 int charsread = 0;
233  0 while (charsread < size) {
234  0 final int read = in.read(data, charsread, size - charsread);
235  0 if (read == -1) {
236  0 final byte[] result = new byte[charsread];
237  0 System.arraycopy(data, 0, result, 0, charsread);
238  0 return result;
239    }
240  0 charsread += read;
241    }
242  0 in.close();
243  0 return data;
244    } finally {
245  0 close(in);
246    }
247    }
248   
249   
250    /**
251    * Reads contents of an URL into a string buffer. The filling is character set dependent.
252    * @param url This URL will be loaded.
253    * @param buffer Buffer to fill with file contents.
254    * @throws IOException Reading failed.
255    *
256    * @deprecated Choose correct encoding.
257    */
 
258  0 toggle public static void loadFile(final URL url, final StringBuffer buffer) throws IOException {
259  0 InputStream in = null;
260  0 BufferedReader dis = null;
261  0 try {
262  0 in = url.openStream();
263  0 dis = new BufferedReader(new InputStreamReader(in));
264  0 int i;
265  0 while ((i = dis.read()) != -1) {
266  0 buffer.append((char) i);
267    }
268    } finally {
269  0 close(in);
270  0 close(dis);
271    }
272    }
273   
274    /**
275    * Reads contents of an URL into a string buffer. The filling is character set dependent.
276    * @param url This URL will be loaded.
277    * @param buffer Buffer to fill with file contents.
278    * @param encoding Take this encoding.
279    * @throws IOException Reading failed.
280    */
 
281  0 toggle public static void loadFile(final URL url, final StringBuffer buffer, final String encoding)
282    throws IOException {
283  0 InputStream in = null;
284  0 BufferedReader dis = null;
285  0 try {
286  0 in = url.openStream();
287  0 dis = new BufferedReader(new InputStreamReader(in, encoding));
288  0 int i;
289  0 while ((i = dis.read()) != -1) {
290  0 buffer.append((char) i);
291    }
292    } finally {
293  0 close(in);
294  0 close(dis);
295    }
296    }
297   
298    /**
299    * Save binary contents of an URL into a file.
300    *
301    * @param url This URL will be loaded.
302    * @param file Write into this file.
303    * @throws IOException Reading or writing failed.
304    */
 
305  0 toggle public static void saveFile(final URL url, final File file) throws IOException {
306  0 saveFile(url.openStream(), file);
307    }
308   
309    /**
310    * Save binary contents of an input stream into a file. The input stream is closed even
311    * if exceptions occur.
312    * @param in Read this stream.
313    * @param file Write into this file.
314    *
315    * @throws IOException Reading or writing failed.
316    */
 
317  18 toggle public static void saveFile(final InputStream in, final File file) throws IOException {
318  18 FileOutputStream out = null;
319  18 try {
320  18 out = new FileOutputStream(file);
321  18 final byte[] data = new byte[8 * 1024];
322  18 int length;
323  ? while ((length = in.read(data)) != -1) {
324  90 out.write(data, 0, length);
325    }
326    } finally {
327  18 close(in);
328  18 close(out);
329    }
330    }
331   
332    /**
333    * Convert String into a {@link Reader}.
334    *
335    * <a href="http://bugs.sun.com/bugdatabase/view_bug.do;:WuuT?bug_id=4094886">
336    * Bug ID: 4094886</a>
337    *
338    * @param data Convert this.
339    * @return Resulting reader.
340    */
 
341  137 toggle public static final Reader stringToReader(final String data) {
342  137 try {
343  137 return new InputStreamReader(new ByteArrayInputStream(data.getBytes("ISO-8859-1")));
344    } catch (UnsupportedEncodingException e) {
345  0 throw new RuntimeException(e);
346    }
347    }
348   
349   
350    /**
351    * Saves a <code>String</code> into a file.
352    *
353    * @param filename Name of the file (could include path).
354    * @param text Data to save in the file.
355    * @throws IOException File exception occurred.
356    *
357    * @deprecated Use {@link #saveFile(File, String, String)} that has an encoding.
358    */
 
359  0 toggle public static void saveFile(final String filename, final String text)
360    throws IOException {
361  0 saveFile(new File(filename), text);
362    }
363   
364    /**
365    * Saves a <code>StringBuffer</code> in a file.
366    *
367    * @param filename Name of the file (could include path).
368    * @param text Data to save in the file.
369    * @throws IOException File exception occurred.
370    *
371    * @deprecated Use {@link #saveFile(File, StringBuffer, String)} that has an encoding.
372    */
 
373  0 toggle public static void saveFile(final String filename, final StringBuffer text)
374    throws IOException {
375  0 saveFile(new File(filename), text.toString());
376    }
377   
378    /**
379    * Saves a <code>StringBuffer</code> in a file.
380    *
381    * @param file File to save into.
382    * @param text Data to save in the file.
383    * @throws IOException File exception occurred.
384    *
385    * @deprecated Use {@link #saveFile(File, StringBuffer, String)} that has an encoding
386    * parameter.
387    */
 
388  0 toggle public static void saveFile(final File file, final StringBuffer text)
389    throws IOException {
390  0 saveFile(file, text.toString());
391    }
392   
393    /**
394    * Saves a <code>String</code> in a file. Uses default encoding.
395    *
396    * @param file File to save the data in.
397    * @param text Data to save in the file.
398    * @throws IOException File exception occurred.
399    *
400    * @deprecated Use {@link #saveFile(File, String, String)} that has an encoding parameter.
401    */
 
402  0 toggle public static void saveFile(final File file, final String text)
403    throws IOException {
404  0 BufferedWriter out = new BufferedWriter(
405    new FileWriter(file));
406  0 out.write(text);
407  0 out.close();
408    }
409   
410    /**
411    * Saves a <code>String</code> in a file.
412    *
413    * @param file File to save the data in.
414    * @param text Data to save in the file.
415    * @param encoding Use this encoding.
416    * @throws IOException File exception occurred.
417    */
 
418  0 toggle public static void saveFile(final File file, final StringBuffer text, final String encoding)
419    throws IOException {
420  0 saveFile(file, text.toString(), encoding);
421    }
422   
423    /**
424    * Saves a <code>String</code> in a file.
425    *
426    * @param file File to save the data in.
427    * @param text Data to save in the file.
428    * @param encoding Use this encoding.
429    * @throws IOException File exception occurred.
430    */
 
431  1 toggle public static void saveFile(final File file, final String text, final String encoding)
432    throws IOException {
433  1 BufferedWriter out = new BufferedWriter(
434    new OutputStreamWriter(new FileOutputStream(file), encoding));
435  1 out.write(text);
436  1 out.close();
437    }
438   
439    /**
440    * Saves a <code>data</code> in a file.
441    *
442    * @param file File to save the data in.
443    * @param data Data to save in the file.
444    * @throws IOException File exception occurred.
445    */
 
446  0 toggle public static void saveFileBinary(final File file, final byte[] data)
447    throws IOException {
448  0 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
449  0 out.write(data);
450  0 out.close();
451    }
452   
453    /**
454    * Copies a file to a different location.
455    *
456    * @param from Copy source.
457    * @param to Copy destination.
458    * @throws IOException File exception occurred.
459    */
 
460  54 toggle public static void copyFile(final File from, final File to)
461    throws IOException {
462   
463  54 if (from.getCanonicalFile().equals(to.getCanonicalFile())) {
464  0 return;
465    }
466  54 FileInputStream in = new FileInputStream(from);
467  54 FileOutputStream out = new FileOutputStream(to);
468   
469  54 byte[] data = new byte[8 * 1024];
470  54 int length;
471   
472  ? while ((length = in.read(data)) != -1) {
473  538 out.write(data, 0, length);
474    }
475  54 in.close();
476  54 out.close();
477    }
478   
479    /**
480    * Compare two files.
481    *
482    * @param from Compare source.
483    * @param with Compare with this file.
484    * @return Is the contents of the two files binary equal?
485    * @throws IOException File exception occurred.
486    */
 
487  3 toggle public static boolean compareFilesBinary(final File from, final File with)
488    throws IOException {
489  3 if (from == null && with == null) {
490  0 return true;
491    }
492  3 if (from == null || with == null) {
493  0 return false;
494    }
495  3 if (from.getAbsoluteFile().equals(with.getAbsoluteFile())) {
496  0 return true;
497    }
498  3 if (from.length() != with.length()) {
499  0 return false;
500    }
501  3 byte[] dataOne = new byte[8 * 1024];
502  3 byte[] dataTwo = new byte[8 * 1024];
503  3 int length;
504   
505  3 FileInputStream one = null;
506  3 FileInputStream two = null;
507  3 try {
508  3 one = new FileInputStream(from);
509  3 two = new FileInputStream(with);
510   
511  ? while ((length = one.read(dataOne)) != -1) {
512  36 if (length != two.read(dataTwo)) {
513  0 return false;
514    }
515  36 if (!Arrays.equals(dataOne, dataTwo)) {
516  0 return false;
517    }
518    }
519  3 return true;
520    } finally {
521  3 close(one);
522  3 close(two);
523    }
524    }
525   
526    /**
527    * Delete file directory recursive.
528    *
529    * @param directory Directory to delete.
530    * @param deleteDir Delete directory itself too?
531    * @return Was deletion successful?
532    */
 
533  0 toggle public static boolean deleteDir(final File directory, final boolean deleteDir) {
534    // to see if this directory is actually a symbolic link to a directory,
535    // we want to get its canonical path - that is, we follow the link to
536    // the file it's actually linked to
537  0 File candir;
538  0 try {
539  0 candir = directory.getCanonicalFile();
540    } catch (IOException e) {
541  0 return false;
542    }
543   
544    // a symbolic link has a different canonical path than its actual path,
545    // unless it's a link to itself
546  0 if (!candir.equals(directory.getAbsoluteFile())) {
547    // this file is a symbolic link, and there's no reason for us to
548    // follow it, because then we might be deleting something outside of
549    // the directory we were told to delete
550  0 return false;
551    }
552   
553    // now we go through all of the files and subdirectories in the
554    // directory and delete them one by one
555  0 boolean success = true;
556  0 File[] files = candir.listFiles();
557  0 if (files != null) {
558  0 for (int i = 0; i < files.length; i++) {
559  0 File file = files[i];
560   
561    // in case this directory is actually a symbolic link, or it's
562    // empty, we want to try to delete the link before we try
563    // anything
564  0 boolean deleted = file.delete();
565  0 if (!deleted) {
566    // deleting the file failed, so maybe it's a non-empty
567    // directory
568  0 if (file.isDirectory()) {
569  0 deleted = deleteDir(file, true);
570    }
571   
572    // otherwise, there's nothing else we can do
573    }
574  0 success = success && deleted;
575    }
576    }
577   
578    // now that we tried to clear the directory out, we can try to delete it
579    // again
580  0 if (deleteDir) {
581  0 return directory.delete();
582    }
583  0 return success;
584    }
585   
586    /**
587    * Delete directory contents for all files that match the filter. The main directory itself is
588    * not deleted.
589    *
590    * @param directory Directory to scan for files to delete.
591    * @param filter Filter files (and directories) to delete.
592    * @return Was deletion successful?
593    */
 
594  0 toggle public static boolean deleteDir(final File directory, final FileFilter filter) {
595    // to see if this directory is actually a symbolic link to a directory,
596    // we want to get its canonical path - that is, we follow the link to
597    // the file it's actually linked to
598  0 File candir;
599  0 try {
600  0 candir = directory.getCanonicalFile();
601    } catch (IOException e) {
602  0 return false;
603    }
604   
605    // a symbolic link has a different canonical path than its actual path,
606    // unless it's a link to itself
607  0 if (!candir.equals(directory.getAbsoluteFile())) {
608    // this file is a symbolic link, and there's no reason for us to
609    // follow it, because then we might be deleting something outside of
610    // the directory we were told to delete
611  0 return false;
612    }
613   
614    // now we go through all of the files and subdirectories in the
615    // directory and delete them one by one
616  0 boolean success = true;
617  0 File[] files = candir.listFiles(filter);
618  0 if (files != null) {
619  0 for (int i = 0; i < files.length; i++) {
620  0 File file = files[i];
621   
622    // in case this directory is actually a symbolic link, or it's
623    // empty, we want to try to delete the link before we try
624    // anything
625  0 boolean deleted = file.delete();
626  0 if (!deleted) {
627    // deleting the file failed, so maybe it's a non-empty
628    // directory
629  0 if (file.isDirectory()) {
630  0 deleted = deleteDir(file, true);
631    }
632   
633    // otherwise, there's nothing else we can do
634    }
635  0 success = success && deleted;
636    }
637    }
638   
639  0 return success;
640    }
641   
642    /**
643    * Print current system properties to System.out.
644    */
 
645  0 toggle public static void printAllSystemProperties() {
646  0 Properties sysprops = System.getProperties();
647  0 for (Enumeration e = sysprops.propertyNames(); e.hasMoreElements(); ) {
648  0 String key = (String) e.nextElement();
649  0 String value = sysprops.getProperty(key);
650  0 System.out.println(key + "=" + value);
651    }
652    }
653   
654    /**
655    * Get home directory of user.
656    *
657    * @return Home directory of user.
658    */
 
659  0 toggle public static File getUserHomeDirectory() {
660  0 return new File((String) System.getProperties().get("user.home"));
661    }
662   
663    /**
664    * Convert file in URL.
665    *
666    * @param file File.
667    * @return URL.
668    */
 
669  7658 toggle public static URL toUrl(final File file) {
670  7658 try {
671  7658 return file.toURI().toURL();
672    } catch (MalformedURLException e) { // should only happen if there is a bug in the JDK
673  0 throw new RuntimeException(e);
674    }
675    }
676   
677    /**
678    * Creates necessary parent directories for a file.
679    *
680    * @param file File.
681    */
 
682  39 toggle public static void createNecessaryDirectories(final File file) {
683  39 if (file.getParentFile() != null) {
684  39 file.getParentFile().mkdirs();
685    }
686    }
687   
688    /**
689    * Create relative address from <code>orgin</code> to <code>next</code>.
690    *
691    * @param orgin this is the original location
692    * @param next this should be the next location
693    * @return relative (or if necessary absolute) file path
694    */
 
695  4 toggle public static final String createRelativePath(final File orgin, final File next) {
696  4 try {
697  4 if (orgin.equals(next)) {
698  0 return "";
699    }
700  4 try {
701  4 String org = orgin.getCanonicalPath().replace('\\', '/');
702  4 if (!org.endsWith("/")) {
703  4 org += ('/');
704    }
705  4 String nex = next.getCanonicalPath().replace('\\', '/');
706  4 if (!nex.endsWith("/")) {
707  4 nex += ('/');
708    }
709  4 if (org.equals(nex)) {
710  0 return "";
711    }
712  4 int i = -1; // position of next '/'
713  4 int j = 0; // position of last '/'
714  ? while (0 <= (i = org.indexOf("/", j))) {
715  17 if (i >= 0 && nex.length() > i
716    && org.substring(j, i).equals(
717    nex.substring(j, i))) {
718  15 j = i + 1;
719    } else {
720  2 break;
721    }
722    }
723  4 if (j > 0) {
724  4 i = j;
725  4 final StringBuffer result = new StringBuffer(nex.length());
726  ? while (0 <= (i = org.indexOf("/", i))) {
727  3 i++;
728  3 result.append("../");
729    }
730  4 result.append(nex.substring(j, nex.length() - 1));
731  4 return result.toString();
732    }
733  0 return nex.substring(0, nex.length() - 1);
734    } catch (RuntimeException e) {
735  0 return next.toString();
736    }
737    } catch (IOException e) {
738  0 return new File(orgin, next.getPath()).getPath();
739    }
740    }
741   
742    /**
743    * Waits until a '\n' was read from System.in.
744    */
 
745  0 toggle public static void waitln() {
746  0 System.out.println("\n..press <return> to continue");
747  0 try {
748  0 (new java.io.BufferedReader(new java.io.InputStreamReader(
749    System.in))).readLine();
750    } catch (IOException e) {
751    // ignore
752    }
753    }
754   
755    /**
756    * Closes input stream without exception.
757    *
758    * @param in Input stream, maybe <code>null</code>.
759    */
 
760  24 toggle public static void close(final InputStream in) {
761  24 if (in != null) {
762  24 try {
763  24 in.close();
764    } catch (Exception e) {
765    // ignore
766    }
767    }
768    }
769   
770    /**
771    * Closes out stream without exception.
772    *
773    * @param out Output stream, maybe <code>null</code>.
774    */
 
775  57 toggle public static void close(final OutputStream out) {
776  57 if (out != null) {
777  57 try {
778  57 out.close();
779    } catch (Exception e) {
780    // ignore
781    }
782    }
783    }
784   
785    /**
786    * Closes input reader without exception.
787    *
788    * @param reader Reader, maybe <code>null</code>.
789    */
 
790  61691 toggle public static void close(final Reader reader) {
791  61691 if (reader != null) {
792  61691 try {
793  61691 reader.close();
794    } catch (Exception e) {
795    // ignore
796    }
797    }
798    }
799   
800    /**
801    * Get start directory for application. Within the start directory all newly created data is
802    * stored in. If this is no Java Webstart version the result is
803    * <code>new File(".")</code>. Otherwise the start directory is the subdirectory
804    * "." concatenated <code>application</code> within <code>user.home</code>.
805    *
806    * @param application Application name, used for Java Webstart version. Should
807    * be written in lowercase letters. A "." is automatically appended at
808    * the beginning.
809    * @return Start directory for application.
810    */
 
811  0 toggle public static final File getStartDirectory(final String application) {
812  0 final File startDirectory;
813  0 if (isWebStarted()) {
814  0 final String userHome = System.getProperty("user.home", ".");
815  0 startDirectory = new File(new File(userHome), "." + application);
816    } else {
817  0 startDirectory = new File(".");
818    }
819  0 return startDirectory;
820    }
821   
822    /**
823    * Was the application started by Java Webstart?
824    *
825    * @return Was the application started by Java Webstart.
826    */
 
827  0 toggle public static final boolean isWebStarted() {
828  0 final String webStart = (String) System.getProperties().get("javawebstart.version");
829  0 return webStart != null;
830    }
831   
832    /**
833    * Loads a property file from given URL.
834    *
835    * @param url URL to load properties from.
836    * @return Loaded properties.
837    * @throws MalformedURLException Invalid URL.
838    * @throws IOException Reading error.
839    */
 
840  0 toggle public static Properties loadProperties(final URL url)
841    throws IOException {
842  0 Properties newprops = new Properties();
843  0 InputStream in = url.openStream();
844  0 newprops.load(in);
845  0 in.close();
846  0 return newprops;
847    }
848   
849    /**
850    * This method returns the contents of an object variable (even if it is private).
851    *
852    * @param obj Object.
853    * @param name Variable name
854    * @return Contents of variable.
855    */
 
856  25278 toggle public static Object getFieldContent(final Object obj, final String name) {
857  25278 final Field field;
858  25278 try {
859  25278 field = obj.getClass().getDeclaredField(name);
860  25278 field.setAccessible(true);
861    } catch (SecurityException e) {
862  0 throw new RuntimeException(e);
863    } catch (NoSuchFieldException e) {
864  0 throw new RuntimeException(e);
865    }
866  25278 try {
867  25278 return field.get(obj);
868    } catch (IllegalArgumentException e) {
869  0 throw new RuntimeException(e);
870    } catch (IllegalAccessException e) {
871  0 throw new RuntimeException(e);
872    }
873    }
874   
875    /**
876    * This method returns the contents of an object variable (even if it is private).
877    *
878    * @param obj Object.
879    * @param name Variable name
880    * @return Contents of variable.
881    */
 
882  0 toggle public static Object getFieldContentSuper(final Object obj, final String name) {
883  0 Field field = null;
884  0 try {
885  0 Class cl = obj.getClass();
886  0 while (!Object.class.equals(cl)) {
887  0 try {
888  0 field = cl.getDeclaredField(name);
889  0 break;
890    } catch (NoSuchFieldException ex) {
891  0 cl = cl.getSuperclass();
892  0 System.out.println(cl);
893    }
894    }
895  0 field.setAccessible(true);
896    } catch (SecurityException e) {
897  0 throw new RuntimeException(e);
898    // } catch (NoSuchFieldException e) {
899    // throw new RuntimeException(e);
900    }
901  0 try {
902  0 return field.get(obj);
903    } catch (IllegalArgumentException e) {
904  0 throw new RuntimeException(e);
905    } catch (IllegalAccessException e) {
906  0 throw new RuntimeException(e);
907    }
908    }
909   
910    /**
911    * This method sets the contents of an object variable (even if it is private).
912    *
913    * @param obj Object.
914    * @param name Variable name.
915    * @param value Value to set.
916    */
 
917  3 toggle public static void setFieldContent(final Object obj, final String name, final Object value) {
918  3 final Field field;
919  3 try {
920  3 field = obj.getClass().getDeclaredField(name);
921  3 field.setAccessible(true);
922    } catch (SecurityException e) {
923  0 throw new RuntimeException(e);
924    } catch (NoSuchFieldException e) {
925  0 throw new RuntimeException(e);
926    }
927  3 try {
928  3 field.set(obj, value);
929    } catch (IllegalArgumentException e) {
930  0 throw new RuntimeException(e);
931    } catch (IllegalAccessException e) {
932  0 throw new RuntimeException(e);
933    }
934    }
935   
936    /**
937    * Sleep my little class.
938    *
939    * @param ms Milliseconds to wait.
940    */
 
941  0 toggle public static void sleep(final int ms) {
942  0 final String monitor = "";
943  0 synchronized (monitor) {
944  0 try {
945  0 monitor.wait(ms);
946    } catch (InterruptedException e) {
947    }
948    }
949    }
950   
951    /**
952    * Get currently running java version and subversion numbers. This is the running JRE version.
953    * If no version could be identified <code>null</code> is returned.
954    *
955    * @return Array of version and subversion numbers.
956    */
 
957  39 toggle public static int[] getJavaVersion() {
958  39 final String version = System.getProperty("java.version");
959  39 final List numbers = new ArrayList();
960  39 final StringTokenizer tokenizer = new StringTokenizer(version, ".");
961  156 while (tokenizer.hasMoreElements()) {
962  117 String sub = (String) tokenizer.nextToken();
963  234 for (int i = 0; i < sub.length(); i++) {
964  156 if (!Character.isDigit(sub.charAt(i))) {
965  39 sub = sub.substring(0, i);
966  39 break;
967    }
968    }
969  117 try {
970  117 numbers.add(new Integer(Integer.parseInt(sub)));
971    } catch (Exception e) {
972  0 e.printStackTrace();
973  0 break;
974    }
975    }
976  39 if (numbers.size() == 0) {
977  0 return null;
978    }
979  39 final int[] result = new int[numbers.size()];
980  156 for (int i = 0; i < numbers.size(); i++) {
981  117 result[i] = ((Integer) numbers.get(i)).intValue();
982    }
983  39 return result;
984    }
985   
986    }