Clover coverage report -
Coverage timestamp: Sun Oct 12 2003 22:54:40 PDT
file stats: LOC: 376   Methods: 15
NCLOC: 205   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
FileUtility.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
 
 3   
 VRMoo Common - Virtual Reality Object Oriented MUD Common Code
 4   
 Copyright (C) 2003  VRMoo Development Team
 5   
 
 6   
 
 7   
 This program is free software; you can redistribute it and/or modify
 8   
 it under the terms of the GNU General Public License as published by
 9   
 the Free Software Foundation; either version 2 of the License, or
 10   
 (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   
 You should have received a copy of the GNU General Public License
 18   
 along with this program; if not, write to the Free Software
 19   
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 20   
 
 21   
 
 22   
 For information about VRMoo and its authors, please visit the website:
 23   
 http://www.vrmoo.org/
 24   
 
 25   
 */
 26   
 
 27   
 package org.vrmoo.common.util;
 28   
 
 29   
 import java.io.BufferedReader;
 30   
 import java.io.File;
 31   
 import java.io.FileInputStream;
 32   
 import java.io.FileOutputStream;
 33   
 import java.io.FileReader;
 34   
 import java.io.FileWriter;
 35   
 import java.io.FileFilter;
 36   
 import java.io.IOException;
 37   
 import java.io.InputStream;
 38   
 import java.io.OutputStream;
 39   
 import java.io.PrintWriter;
 40   
 import java.util.ArrayList;
 41   
 import java.util.List;
 42   
 import java.util.zip.CRC32;
 43   
 
 44   
 import org.vrmoo.common.exception.VRMooException;
 45   
 
 46   
 /**
 47   
  * Utility class used to provide basic file operations.
 48   
  *
 49   
  * @author Jeff Weston
 50   
  */
 51   
 public class FileUtility
 52   
 {
 53   
     /**
 54   
      * Make the default constructor private since this class isn't meant to be
 55   
      * instantiated.
 56   
      */
 57  0
     private FileUtility( )
 58   
     {
 59   
     }
 60   
 
 61   
     /**
 62   
      * Ensure that a given directory, as specified in a <code>File</code> object
 63   
      * exists. Create it if it doesn't.
 64   
      *
 65   
      * @param dir   a <code>File</code> object specifying the directory
 66   
      */
 67  0
     public static void ensureDirectoryExists( File dir )
 68   
     {
 69  0
         if ( dir.exists( ) )
 70   
         {
 71  0
             if ( ! dir.isDirectory( ) )
 72   
             {
 73  0
                 dir.delete( );
 74  0
                 dir.mkdirs( );
 75   
             }
 76   
         }
 77   
         else
 78   
         {
 79  0
             dir.mkdirs( );
 80   
         }
 81   
     }
 82   
 
 83   
     /**
 84   
      * Read in the file specified by the file descriptor and return the
 85   
      * contents of the file as an array of bytes.
 86   
      *
 87   
      * @param file   the file descriptor
 88   
      *
 89   
      * @return the contents of the file
 90   
      *
 91   
      * @throws IOException for any IO errors that occur
 92   
      */
 93  0
     public static byte[ ] readFile( File file )
 94   
             throws IOException
 95   
     {
 96  0
         List list = new ArrayList( );
 97  0
         InputStream stream = null;
 98   
 
 99  0
         try
 100   
         {
 101  0
             stream = new FileInputStream( file );
 102   
 
 103  0
             int data = stream.read( );
 104  0
             while ( data != -1 )
 105   
             {
 106  0
                 list.add( new Integer( data ) );
 107  0
                 data = stream.read( );
 108   
             }
 109   
         }
 110   
         finally
 111   
         {
 112  0
             if ( stream != null )
 113   
             {
 114  0
                 stream.close( );
 115   
             }
 116   
         }
 117   
 
 118  0
         byte[ ] dataArray = new byte[ list.size( ) ];
 119  0
         for ( int i = 0; i < list.size( ); i++ )
 120   
         {
 121  0
             dataArray[ i ] = ( byte ) ( ( Integer ) list.get( i ) ).intValue( );
 122   
         }
 123   
 
 124  0
         return dataArray;
 125   
     }
 126   
 
 127   
     /**
 128   
      * Write an array of bytes out to a file specified by a file descriptor.
 129   
      *
 130   
      * @param file   the file descriptor
 131   
      * @param data   the data to write
 132   
      *
 133   
      * @throws IOException for any IO errors that occur
 134   
      */
 135  0
     public static void writeFile( File file, byte[ ] data )
 136   
             throws IOException
 137   
     {
 138  0
         OutputStream stream = null;
 139   
 
 140  0
         try
 141   
         {
 142  0
             stream = new FileOutputStream( file );
 143  0
             stream.write( data );
 144   
         }
 145   
         finally
 146   
         {
 147  0
             if ( stream != null )
 148   
             {
 149  0
                 stream.close( );
 150   
             }
 151   
         }
 152   
     }
 153   
 
 154   
     /**
 155   
      * Compute the CRC-32 of a given file.
 156   
      *
 157   
      * @param file   the file descriptor
 158   
      *
 159   
      * @return the CRC-32 of the file.
 160   
      *
 161   
      * @throws IOException for any IO errors that occur
 162   
      */
 163  0
     public static long computeCRC32( File file )
 164   
             throws IOException
 165   
     {
 166  0
         byte[ ] data = readFile( file );
 167  0
         CRC32 crc = new CRC32( );
 168  0
         crc.update( data );
 169  0
         return crc.getValue( );
 170   
     }
 171   
 
 172   
     /**
 173   
      * Recursively copies a directory structure from a source directory to a
 174   
      * target directory.
 175   
      *
 176   
      * @param source   the directory to copy
 177   
      * @param target   the destination
 178   
      *
 179   
      * @throws VRMooException   if the source is not a directory or if the
 180   
      *                            target already exists
 181   
      * @throws IOException      for any IO errors that occur
 182   
      */
 183  0
     public static void copyDirectory( String source, String target )
 184   
             throws VRMooException, IOException
 185   
     {
 186  0
         File sourceDir = new File( source );
 187  0
         File targetDir = new File( target );
 188   
 
 189  0
         if ( ! sourceDir.isDirectory( ) )
 190   
         {
 191  0
             throw new VRMooException( "Source is not a directory." );
 192   
         }
 193   
 
 194  0
         if ( targetDir.exists( ) )
 195   
         {
 196  0
             throw new VRMooException( "Target directory already exists." );
 197   
         }
 198   
 
 199  0
         ensureDirectoryExists( targetDir );
 200   
 
 201  0
         File[ ] files = sourceDir.listFiles( );
 202   
 
 203  0
         for ( int i = 0; i < files.length; i++ )
 204   
         {
 205  0
             if ( files[ i ].isDirectory( ) )
 206   
             {
 207   
                 // Ignore CVS directories.
 208  0
                 if ( ! files[ i ].getName( ).equals( "CVS" ) )
 209   
                 {
 210  0
                     copyDirectory( source + "/" + files[ i ].getName( ),
 211   
                                    target + "/" + files[ i ].getName( ) );
 212   
                 }
 213   
             }
 214   
             else
 215   
             {
 216  0
                 copyFile( source + "/" + files[ i ].getName( ),
 217   
                           target + "/" + files[ i ].getName( ) );
 218   
             }
 219   
         }
 220   
     }
 221   
 
 222   
     /**
 223   
      * Copies a source file to a target file.
 224   
      *
 225   
      * @param source   the file to copy
 226   
      * @param target   the destination
 227   
      *
 228   
      * @throws VRMooException   if the source is not a file or if the target
 229   
      *                            already exists
 230   
      * @throws IOException      for any IO errors that occur
 231   
      */
 232  0
     public static void copyFile( String source, String target )
 233   
             throws VRMooException, IOException
 234   
     {
 235  0
         File sourceFile = new File( source );
 236  0
         File targetFile = new File( target );
 237   
 
 238  0
         if ( sourceFile.isDirectory( ) )
 239   
         {
 240  0
             throw new VRMooException( "Source is not a file." );
 241   
         }
 242   
 
 243  0
         if ( targetFile.exists( ) )
 244   
         {
 245  0
             throw new VRMooException( "Target file already exists." );
 246   
         }
 247   
 
 248  0
         byte[ ] data = readFile( sourceFile );
 249  0
         writeFile( targetFile, data );
 250   
     }
 251   
 
 252   
     /**
 253   
      * Get a <code>BufferedReader</code> for the specified file name.
 254   
      *
 255   
      * @param fileName   the file name to create a <code>BufferedReader</code>
 256   
      *                     for
 257   
      *
 258   
      * @return the <code>BufferedReader</code> for the specified file name
 259   
      *
 260   
      * @throws IOException for any IO errors that occur
 261   
      */
 262  0
     public static BufferedReader getBufferedReader( String fileName )
 263   
             throws IOException
 264   
     {
 265  0
         FileReader fileReader = new FileReader( fileName );
 266  0
         BufferedReader bufferedReader = new BufferedReader( fileReader );
 267  0
         return bufferedReader;
 268   
     }
 269   
 
 270   
     /**
 271   
      * Get a <code>PrintWriter</code> for the specified file name.
 272   
      *
 273   
      * @param fileName   the file name to create a <code>PrintWriter</code> for
 274   
      *
 275   
      * @return the <code>PrintWriter</code> for the specified file name
 276   
      *
 277   
      * @throws IOException for any IO errors that occur
 278   
      */
 279  0
     public static PrintWriter getPrintWriter( String fileName )
 280   
             throws IOException
 281   
     {
 282  0
         FileWriter fileWriter = new FileWriter( fileName, false );
 283  0
         PrintWriter printWriter = new PrintWriter( fileWriter );
 284  0
         return printWriter;
 285   
     }
 286   
 
 287   
     /**
 288   
      * Deletes a directory, and everything in it.
 289   
      *
 290   
      * @param directory   the directory to delete
 291   
      *
 292   
      * @throws VRMooException   if the specified directory is not a directory
 293   
      */
 294  0
     public static void deleteDirectory( String directory ) throws VRMooException
 295   
     {
 296  0
         File dir = new File( directory );
 297   
 
 298  0
         if ( ! dir.isDirectory( ) )
 299   
         {
 300  0
             throw new VRMooException(
 301   
                     "The specified directory is not a directory." );
 302   
         }
 303   
 
 304  0
         File[ ] files = dir.listFiles( );
 305   
 
 306  0
         for ( int i = 0; i < files.length; i++ )
 307   
         {
 308  0
             if ( files[ i ].isDirectory( ) )
 309   
             {
 310  0
                 deleteDirectory( directory + "/" + files[ i ].getName( ) );
 311   
             }
 312   
             else
 313   
             {
 314  0
                 files[ i ].delete( );
 315   
             }
 316   
         }
 317   
 
 318  0
         dir.delete( );
 319   
     }
 320   
 
 321   
     /**
 322   
      * Get a <code>FileFilter</code> for listing image files of
 323   
      * type gif, jpg, and png.
 324   
      *
 325   
      * @return a <code>FileFilter</code> for filtering images
 326   
      */
 327  0
      public static FileFilter getImageFilter() {
 328  0
          return new FileFilter() {
 329  0
             public boolean accept(File f) {
 330  0
                 String name = f.getName().toLowerCase();
 331  0
                 return (name.endsWith("jpg") ||
 332   
                     name.endsWith("png") || name.endsWith("gif"));
 333   
             }
 334  0
             public String getDescription() {
 335  0
                 return "JPG, GIF, and PNG (image) files";
 336   
             }
 337   
         };
 338   
      }
 339   
 
 340   
      /**
 341   
       * Get the extension from the name of a file. This is the part of the
 342   
       * filename after the last period, or null if no period exists.
 343   
       * @param file the File to return an extension for
 344   
       * @return the extension from the file's name or null
 345   
       */
 346  0
       public static String getExtension(File file) {
 347  0
           String name = file.getName();
 348  0
           int dotIndex = name.lastIndexOf(".");
 349  0
           if (dotIndex == -1) {
 350  0
               return null;
 351   
           }
 352  0
           return name.substring(dotIndex + 1);
 353   
       }
 354   
 
 355   
       /**
 356   
        * Get an image file with the given name. The name does not
 357   
        * contain an extension; this method searches for the given
 358   
        * file with .png, .jpg, .gif (in that order) extensions and
 359   
        * returns the first one it finds or null if no such file
 360   
        * exists.
 361   
        * @param fileName a file name without an extension
 362   
        * @return a File with the given name plus an image extension
 363   
        * or null if no such file exists.
 364   
        */
 365  0
       public static File getImageFile(String fileName) {
 366  0
           String[] extensions = new String[] {".png", ".gif", ".jpg"};
 367  0
           for (int i = 0; i < extensions.length; i++) {
 368  0
               File f = new File(fileName + extensions[i]);
 369  0
               if (f.exists()) {
 370  0
                   return f;
 371   
               }
 372   
           }
 373  0
           return null;
 374   
       }
 375   
 }
 376