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