Coverage Report - net.sf.statcvs.util.FileUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
FileUtils
22%
17/78
24%
8/34
2.667
 
 1  
 /*
 2  
     StatCvs - CVS statistics generation 
 3  
     Copyright (C) 2002  Lukasz Pekacki <lukasz@pekacki.de>
 4  
     http://statcvs.sf.net/
 5  
     
 6  
     This library is free software; you can redistribute it and/or
 7  
     modify it under the terms of the GNU Lesser General Public
 8  
     License as published by the Free Software Foundation; either
 9  
     version 2.1 of the License, or (at your option) any later version.
 10  
 
 11  
     This library is distributed in the hope that it will be useful,
 12  
     but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14  
     Lesser General Public License for more details.
 15  
 
 16  
     You should have received a copy of the GNU Lesser General Public
 17  
     License along with this library; if not, write to the Free Software
 18  
     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 19  
     
 20  
         $RCSfile: FileUtils.java,v $ 
 21  
         Created on $Date: 2008/04/02 11:52:02 $ 
 22  
 */
 23  
 package net.sf.statcvs.util;
 24  
 
 25  
 import java.io.File;
 26  
 import java.io.FileNotFoundException;
 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  
 
 33  
 /**
 34  
  * Some helpful file functions
 35  
  * TODO: Remove redundancy, write tests
 36  
  * @author Lukasz Pekacki
 37  
  * @version $Id: FileUtils.java,v 1.20 2008/04/02 11:52:02 benoitx Exp $
 38  
  */
 39  0
 public class FileUtils {
 40  
     /**
 41  
      * Copyies a file to a specified desitination
 42  
      * @param inputName File
 43  
      * @param destination Filename
 44  
      * @throws FileNotFoundException if no input file exists
 45  
      * @throws IOException if cannot read or write
 46  
      */
 47  
     public static void copyFile(final String inputName, final String destination) throws FileNotFoundException, IOException {
 48  0
         final File input = new File(inputName);
 49  0
         final File outputFile = new File(destination);
 50  0
         FileReader in = null;
 51  0
         FileWriter out = null;
 52  
         try {
 53  0
             in = new FileReader(input);
 54  0
             out = new FileWriter(outputFile);
 55  
             int c;
 56  0
             while ((c = in.read()) != -1) {
 57  0
                 out.write(c);
 58  0
             }
 59  0
         } finally {
 60  0
             try {
 61  0
                 if (in != null) {
 62  0
                     in.close();
 63  
                 }
 64  
             } finally {
 65  0
                 if (out != null) {
 66  0
                     out.close();
 67  
                 }
 68  
             }
 69  0
         }
 70  0
     }
 71  0
 
 72  0
     /**
 73  0
      * Copy a InputStream into a File
 74  0
      * @param in source
 75  
      * @param out destination
 76  0
      * @throws FileNotFoundException if not found
 77  0
      * @throws IOException if read/write error
 78  0
      */
 79  
     public static void copyFile(final InputStream in, final File out) throws FileNotFoundException, IOException {
 80  0
         final InputStream fis = in;
 81  0
         FileOutputStream fos = null;
 82  
         try {
 83  0
             fos = new FileOutputStream(out);
 84  0
             final byte[] buf = new byte[1024];
 85  0
             int i = 0;
 86  0
             while ((i = fis.read(buf)) > 0) {
 87  0
                 fos.write(buf, 0, i);
 88  
             }
 89  0
         } finally {
 90  0
             try {
 91  0
                 fis.close();
 92  
             } finally {
 93  0
                 if (fos != null) {
 94  0
                     fos.close();
 95  0
                 }
 96  
             }
 97  0
         }
 98  0
     }
 99  
 
 100  
     /**
 101  
      * Takes a filename with path and returns just the filename.
 102  
      * @param filename a filename with path
 103  
      * @return just the filename part
 104  0
      */
 105  
     public static String getFilenameWithoutPath(final String filename) {
 106  0
         final File f = new File(filename);
 107  0
         return f.getName();
 108  
     }
 109  
 
 110  
     /**
 111  
      * Returns the os dependend path seperator
 112  
      * @return String os dependend path seperator
 113  
      */
 114  
     public static String getDirSeparator() {
 115  60
         return System.getProperty("file.separator");
 116  
     }
 117  
 
 118  
     /**
 119  
      * Returns the java path seperator
 120  
      * @return String java  path seperator
 121  
      */
 122  
     public static String getDefaultDirSeparator() {
 123  
         // Thanks for this hint in our bug tracking system
 124  60
         return java.io.File.separator;
 125  0
     }
 126  0
 
 127  
     /**
 128  0
      * Deletes the ending directory separator of a 
 129  
      * given <code>path</code> if there is one and returns
 130  
      * the result.
 131  
      * Otherwise the path is unhandled returned.
 132  
      * 
 133  
      * <p>The separator is the one used bye the
 134  
      * underlying operating system and it is the one returned
 135  
      * bye the <code>getDirSeparator()</code> method.
 136  
      * 
 137  
      * @param path The <code>path</code> to delete the directory
 138  
      * separator from.
 139  
      * @return The <code>path</code> without the ending
 140  0
      * directory separator.
 141  
      * @see net.sf.statcvs.util.FileUtils#getDirSeparator
 142  
      */
 143  
     public static String getPathWithoutEndingSlash(final String path) {
 144  0
         if (path.endsWith(getDefaultDirSeparator())) {
 145  0
             final int pos = path.lastIndexOf(getDefaultDirSeparator());
 146  0
             return path.substring(0, pos);
 147  
         }
 148  0
         return path;
 149  0
     }
 150  0
 
 151  
     /**
 152  0
      * Concatenates <code>path</code> and filename to an
 153  0
      * absolute filename by inserting the system file separator.
 154  0
      * 
 155  0
      * @param path The path to use.
 156  
      * @param filename The filename for concatenation.
 157  0
      * @return The concatenated absolute filename.
 158  
      */
 159  
     public static String getAbsoluteName(final String path, final String filename) {
 160  60
         return path + getDirSeparator() + filename;
 161  
     }
 162  
 
 163  
     /**
 164  
      * Returns the last compontent of a directory path.
 165  
      * @param path a directory, ending in "/", for example "src/net/sf/statcvs/"
 166  0
      * @return the last component of the path, for example "statcvs"
 167  0
      */
 168  
     public static String getDirectoryName(final String path) {
 169  90
         if ("".equals(path)) {
 170  6
             throw new IllegalArgumentException("can't get directory name for root");
 171  0
         }
 172  84
         final String pathWithoutLastSlash = path.substring(0, path.length() - 1);
 173  84
         final int lastSlash = pathWithoutLastSlash.lastIndexOf('/');
 174  84
         if (lastSlash == -1) {
 175  24
             return pathWithoutLastSlash;
 176  
         }
 177  60
         return pathWithoutLastSlash.substring(lastSlash + 1);
 178  
     }
 179  
 
 180  
     /**
 181  
      * Returns all but the last compontent of a directory path
 182  
      * @param path a directory, ending in "/", for example "src/net/sf/statcvs/"
 183  
      * @return all but the last component of the path, for example "src/net/sf/"
 184  
      */
 185  
     public static String getParentDirectoryPath(final String path) {
 186  150
         if ("".equals(path)) {
 187  6
             throw new IllegalArgumentException("can't get directory name for root");
 188  
         }
 189  144
         final String pathWithoutLastSlash = path.substring(0, path.length() - 1);
 190  144
         final int lastSlash = pathWithoutLastSlash.lastIndexOf('/');
 191  144
         if (lastSlash == -1) {
 192  84
             return "";
 193  
         }
 194  60
         return pathWithoutLastSlash.substring(0, lastSlash + 1);
 195  
     }
 196  
 }