Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: So Feb 25 2007 22:22:30 CET
file stats: LOC: 601   Methods: 28
NCLOC: 309   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
IoUtility.java 15,3% 22,3% 21,4% 20,2%
coverage coverage
 1    /* $Id: IoUtility.java,v 1.8 2007/02/25 20:05:38 m31 Exp $
 2    *
 3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
 4    *
 5    * Copyright 2000-2007, 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.kernel.utility;
 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.FileInputStream;
 26    import java.io.FileOutputStream;
 27    import java.io.FileReader;
 28    import java.io.FileWriter;
 29    import java.io.IOException;
 30    import java.io.InputStream;
 31    import java.io.InputStreamReader;
 32    import java.io.Reader;
 33    import java.io.UnsupportedEncodingException;
 34    import java.net.MalformedURLException;
 35    import java.net.URL;
 36    import java.util.Enumeration;
 37    import java.util.Properties;
 38   
 39   
 40    /**
 41    * A collection of useful static methods for input and output.
 42    *
 43    * LATER mime 20070101: use StringBuilder instead of StringBuffer if working under JDK 1.5
 44    *
 45    * @version $Revision: 1.8 $
 46    * @author Michael Meyling
 47    */
 48    public final class IoUtility {
 49   
 50    /**
 51    * Constructor, should never be called.
 52    */
 53  0 private IoUtility() {
 54    // don't call me
 55    }
 56   
 57    /**
 58    * Reads a file and returns the contents as a <code>String</code>.
 59    *
 60    * @param filename Name of the file (could include path).
 61    * @return Contents of file.
 62    * @throws IOException File exception occurred.
 63    */
 64  0 public static String loadFile(final String filename)
 65    throws IOException {
 66   
 67  0 final StringBuffer buffer = new StringBuffer();
 68  0 loadFile(filename, buffer);
 69  0 return buffer.toString();
 70    }
 71   
 72    /**
 73    * Reads contents of a file into a string buffer.
 74    *
 75    * @param filename Name of the file (could include path).
 76    * @param buffer Buffer to fill with file contents.
 77    * @throws IOException File exception occurred.
 78    */
 79  0 public static void loadFile(final String filename,
 80    final StringBuffer buffer)
 81    throws IOException {
 82  0 loadFile(new File(filename), buffer);
 83    }
 84   
 85    /**
 86    * Reads contents of a stream into a string buffer.
 87    *
 88    * @param in This stream will be loaded.
 89    * @param buffer Buffer to fill with file contents.
 90    * @throws IOException File exception occurred.
 91    */
 92  0 public static void loadStream(final InputStream in,
 93    final StringBuffer buffer)
 94    throws IOException {
 95   
 96  0 buffer.setLength(0);
 97  0 int c;
 98  0 while ((c = in.read()) >= 0) {
 99  0 buffer.append((char) c);
 100    }
 101    }
 102   
 103    /**
 104    * Reads contents of a file into a string buffer.
 105    *
 106    * @param file This file will be loaded.
 107    * @param buffer Buffer to fill with file contents.
 108    * @throws IOException File exception occurred.
 109    */
 110  26554 public static void loadFile(final File file,
 111    final StringBuffer buffer)
 112    throws IOException {
 113   
 114  26554 final int size = (int) file.length();
 115  26554 buffer.setLength(0);
 116  26554 final FileReader in = new FileReader(file);
 117  26554 final char[] data = new char[size];
 118  26554 int charsread = 0;
 119  26554 while (charsread < size) {
 120  26554 charsread += in.read(data, charsread, size - charsread);
 121    }
 122  26554 in.close();
 123  26554 buffer.insert(0, data);
 124    }
 125   
 126    /**
 127    * Reads a file and returns the contents as a <code>String</code>.
 128    *
 129    * @param file File to load from.
 130    * @return Contents of file.
 131    * @throws IOException File exception occurred.
 132    */
 133  0 public static final byte[] loadFileBinary(final File file) throws IOException {
 134  0 final int size = (int) file.length();
 135  0 final FileInputStream in = new FileInputStream(file);
 136  0 try {
 137  0 final byte[] data = new byte[size];
 138  0 int charsread = 0;
 139  0 while (charsread < size) {
 140  0 final int read = in.read(data, charsread, size - charsread);
 141  0 if (read == -1) {
 142  0 final byte[] result = new byte[charsread];
 143  0 System.arraycopy(data, 0, result, 0, charsread);
 144  0 return result;
 145    }
 146  0 charsread += read;
 147    }
 148  0 in.close();
 149  0 return data;
 150    } finally {
 151  0 closeStream(in);
 152    }
 153    }
 154   
 155    /**
 156    * Reads contents of an URL into a string buffer. The filling is character set dependent.
 157    *
 158    * @param url This URL will be loaded.
 159    * @param buffer Buffer to fill with file contents.
 160    * @throws IOException Reading failed.
 161    */
 162  0 public static void loadFile(final URL url, final StringBuffer buffer) throws IOException {
 163  0 InputStream in = null;
 164  0 BufferedReader dis = null;
 165  0 try {
 166  0 in = url.openStream();
 167  0 dis = new BufferedReader(new InputStreamReader(in));
 168  0 int i;
 169  0 while ((i = dis.read()) != -1) {
 170  0 buffer.append((char) i);
 171    }
 172    } finally {
 173  0 closeStream(in);
 174  0 closeReader(dis);
 175    }
 176    }
 177   
 178    /**
 179    * Convert String into a {@link Reader}.
 180    *
 181    * <a href="http://bugs.sun.com/bugdatabase/view_bug.do;:WuuT?bug_id=4094886">
 182    * Bug ID: 4094886</a>
 183    *
 184    * @param data Convert this.
 185    * @return Resulting reader.
 186    */
 187  0 public static final Reader stringToReader(final String data) {
 188  0 try {
 189  0 return new InputStreamReader(new ByteArrayInputStream(data.getBytes("ISO-8859-1")));
 190    } catch (UnsupportedEncodingException e) {
 191  0 throw new RuntimeException(e);
 192    }
 193    }
 194   
 195    /**
 196    * Saves a <code>String</code> into a file.
 197    *
 198    * @param filename Name of the file (could include path).
 199    * @param text Data to save in the file.
 200    * @throws IOException File exception occurred.
 201    */
 202  0 public static void saveFile(final String filename, final String text)
 203    throws IOException {
 204  0 saveFile(new File(filename), text);
 205    }
 206   
 207    /**
 208    * Saves a <code>StringBuffer</code> in a file.
 209    *
 210    * @param filename Name of the file (could include path).
 211    * @param text Data to save in the file.
 212    * @throws IOException File exception occurred.
 213    */
 214  0 public static void saveFile(final String filename, final StringBuffer text)
 215    throws IOException {
 216  0 saveFile(new File(filename), text.toString());
 217    }
 218   
 219   
 220    /**
 221    * Saves a <code>StringBuffer</code> in a file.
 222    *
 223    * @param file File to save into.
 224    * @param text Data to save in the file.
 225    * @throws IOException File exception occurred.
 226    */
 227  0 public static void saveFile(final File file, final StringBuffer text)
 228    throws IOException {
 229  0 saveFile(file, text.toString());
 230    }
 231   
 232   
 233    /**
 234    * Saves a <code>String</code> in a file.
 235    *
 236    * @param file File to save the data in.
 237    * @param text Data to save in the file.
 238    * @throws IOException File exception occurred.
 239    */
 240  1 public static void saveFile(final File file, final String text)
 241    throws IOException {
 242  1 BufferedWriter out = new BufferedWriter(
 243    new FileWriter(file));
 244  1 out.write(text);
 245  1 out.close();
 246    }
 247   
 248    /**
 249    * Saves a <code>String</code> in a file.
 250    *
 251    * @param file File to save the data in.
 252    * @param data Data to save in the file.
 253    * @throws IOException File exception occurred.
 254    */
 255  0 public static void saveFileBinary(final File file, final byte[] data)
 256    throws IOException {
 257  0 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
 258  0 out.write(data);
 259  0 out.close();
 260    }
 261   
 262    /**
 263    * Copies a file to a different location.
 264    *
 265    * @param from Copy source.
 266    * @param to Copy destination.
 267    * @throws IOException File exception occurred.
 268    */
 269  20 public static void copyFile(final File from, final File to)
 270    throws IOException {
 271    /*
 272    final byte[] data = loadFileBinary(from);
 273    saveFileBinary(to, data);
 274    */
 275   
 276  20 if (from.getAbsoluteFile().equals(to.getAbsoluteFile())) {
 277  0 return;
 278    }
 279  20 FileInputStream in = new FileInputStream(from);
 280  20 FileOutputStream out = new FileOutputStream(to);
 281   
 282  20 byte[] data = new byte[8 * 1024];
 283  20 int length;
 284   
 285  ? while ((length = in.read(data)) != -1) {
 286  145 out.write(data, 0, length);
 287    }
 288  20 in.close();
 289  20 out.close();
 290    }
 291   
 292   
 293    /**
 294    * Quotes a <code>String</code>. If no quotes exist in the
 295    * <code>String</code>, a quote character is appended at the
 296    * beginning and the end of the <code>String</code>.
 297    *
 298    * @param unquoted the unquoted <code>String</code>
 299    * @return quoted <code>String</code>
 300    * @throws NullPointerException if <code>unquoted == null</code>
 301    */
 302  0 public static String quote(final String unquoted) {
 303   
 304  0 String result = "\"";
 305   
 306  0 for (int i = 0; i < unquoted.length(); i++) {
 307  0 if (unquoted.charAt(i) == '\"') {
 308  0 result += "\"\"";
 309    } else {
 310  0 result += unquoted.charAt(i);
 311    }
 312    }
 313  0 result += '\"';
 314  0 return result;
 315    }
 316   
 317    /**
 318    * Tests if given <code>String</code> begins with a letter and contains
 319    * only letters and digits.
 320    *
 321    * @param text test this
 322    * @return is <code>text</code> only made of letters and digits and has
 323    * a leading letter?
 324    * @throws NullPointerException if <code>text == null</code>
 325    */
 326  0 public static boolean isLetterDigitString(final String text) {
 327  0 if (text.length() <= 0) {
 328  0 return false;
 329    }
 330  0 if (!Character.isLetter(text.charAt(0))) {
 331  0 return false;
 332    }
 333  0 for (int i = 1; i < text.length(); i++) {
 334  0 if (!Character.isLetterOrDigit(text.charAt(i))) {
 335  0 return false;
 336    }
 337    }
 338  0 return true;
 339    }
 340   
 341    /**
 342    * Delete file directory recursive.
 343    *
 344    * @param directory Directory to delete.
 345    * @return Was deletion successful?
 346    */
 347  0 public static boolean deleteDir(final File directory) {
 348    // to see if this directory is actually a symbolic link to a directory,
 349    // we want to get its canonical path - that is, we follow the link to
 350    // the file it's actually linked to
 351  0 File candir;
 352  0 try {
 353  0 candir = directory.getCanonicalFile();
 354    } catch (IOException e) {
 355  0 return false;
 356    }
 357   
 358    // a symbolic link has a different canonical path than its actual path,
 359    // unless it's a link to itself
 360  0 if (!candir.equals(directory.getAbsoluteFile())) {
 361    // this file is a symbolic link, and there's no reason for us to
 362    // follow it, because then we might be deleting something outside of
 363    // the directory we were told to delete
 364  0 return false;
 365    }
 366   
 367    // now we go through all of the files and subdirectories in the
 368    // directory and delete them one by one
 369  0 File[] files = candir.listFiles();
 370  0 if (files != null) {
 371  0 for (int i = 0; i < files.length; i++) {
 372  0 File file = files[i];
 373   
 374    // in case this directory is actually a symbolic link, or it's
 375    // empty, we want to try to delete the link before we try
 376    // anything
 377  0 boolean deleted = file.delete();
 378  0 if (!deleted) {
 379    // deleting the file failed, so maybe it's a non-empty
 380    // directory
 381  0 if (file.isDirectory()) {
 382  0 deleteDir(file);
 383    }
 384   
 385    // otherwise, there's nothing else we can do
 386    }
 387    }
 388    }
 389   
 390    // now that we tried to clear the directory out, we can try to delete it
 391    // again
 392  0 return directory.delete();
 393    }
 394   
 395   
 396    /**
 397    * Get amount of spaces.
 398    *
 399    * @param length number of spaces
 400    * @return String contains exactly <code>number</code> spaces
 401    */
 402  0 public static StringBuffer getSpaces(final int length) {
 403  0 final StringBuffer buffer = new StringBuffer(length >= 0 ? length : 0);
 404  0 for (int i = 0; i < length; i++) {
 405  0 buffer.append(' ');
 406    }
 407  0 return buffer;
 408    }
 409   
 410    /**
 411    * Get non qualified class name.
 412    *
 413    * @param clazz Class.
 414    * @return Non qualified class name.
 415    */
 416  19700 public static String getClassName(final Class clazz) {
 417  19700 return clazz.getName().substring(clazz.getName().lastIndexOf('.') + 1);
 418    }
 419   
 420    /**
 421    * Print current system properties to System.out.
 422    */
 423  0 public static void printAllSystemProperties() {
 424  0 Properties sysprops = System.getProperties();
 425  0 for (Enumeration e = sysprops.propertyNames(); e.hasMoreElements(); ) {
 426  0 String key = (String) e.nextElement();
 427  0 String value = sysprops.getProperty(key);
 428  0 System.out.println(key + "=" + value);
 429    }
 430    }
 431   
 432    /**
 433    * Get home directory of user.
 434    *
 435    * @return Home directory of user.
 436    */
 437  0 public static File getUserHomeDirectory() {
 438  0 return new File((String) System.getProperties().get("user.home"));
 439    }
 440   
 441    /**
 442    * Convert file in URL.
 443    *
 444    * @param file File.
 445    * @return URL.
 446    */
 447  0 public static URL toUrl(final File file) {
 448  0 try {
 449  0 return file.toURI().toURL();
 450    } catch (MalformedURLException e) { // should only happen if there is a bug in the JDK
 451  0 throw new RuntimeException(e);
 452    }
 453    }
 454   
 455    /**
 456    * Creates necessary parent directories for a file.
 457    *
 458    * @param file File.
 459    */
 460  11 public static void createNecessaryDirectories(final File file) {
 461  11 if (file.getParentFile() != null) {
 462  11 file.getParentFile().mkdirs();
 463    }
 464    }
 465   
 466    /**
 467    * Create relative address from <code>orgin</code> to <code>next</code>.
 468    *
 469    * @param orgin this is the original location
 470    * @param next this should be the next location
 471    * @return relative (or if necessary absolute) file path
 472    */
 473  0 public static final String createRelativePath(final File orgin, final File next) {
 474  0 try {
 475  0 if (orgin.equals(next)) {
 476  0 return "";
 477    }
 478  0