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