You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by do...@apache.org on 2002/01/20 10:46:43 UTC

cvs commit: jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/io FileUtil.java

donaldp     02/01/20 01:46:43

  Modified:    src/java/org/apache/avalon/excalibur/io FileUtil.java
  Log:
  Add contentEquals method from ant and extract some of exception messages into variables (in prep for i18n)
  
  Revision  Changes    Path
  1.17      +88 -19    jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/io/FileUtil.java
  
  Index: FileUtil.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/io/FileUtil.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- FileUtil.java	11 Dec 2001 09:53:29 -0000	1.16
  +++ FileUtil.java	20 Jan 2002 09:46:43 -0000	1.17
  @@ -12,6 +12,7 @@
   import java.io.File;
   import java.io.FileInputStream;
   import java.io.FileOutputStream;
  +import java.io.BufferedInputStream;
   import java.net.URL;
   
   /**
  @@ -36,14 +37,14 @@
    * There are methods to  create a {@link #toFile File from a URL}, copy a
    * {@link #copyFileToDirectory File to a directory},
    * copy a {@link #copyFile File to another File},
  - * copy a {@link #copyURLToFile URL's contents to a File}, 
  + * copy a {@link #copyURLToFile URL's contents to a File},
    * as well as methods to {@link #deleteDirectory(File) delete} and {@link #cleanDirectory(File)
    * clean} a directory.
    * </p>
    *
    * @author <a href="mailto:peter@apache.org">Peter Donald</a>
    * @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
  - * @version CVS $Revision: 1.16 $ $Date: 2001/12/11 09:53:29 $
  + * @version CVS $Revision: 1.17 $ $Date: 2002/01/20 09:46:43 $
    * @since 4.0
    */
   public final class FileUtil
  @@ -57,6 +58,65 @@
       }
   
       /**
  +     * Compare the contents of two files to determine if they are equal or not.
  +     *
  +     * @param file1 the first file
  +     * @param file2 the second file
  +     * @return true if the content of the files are equal or they both don't exist, false otherwise
  +     */
  +    public static boolean contentEquals( final File file1, final File file2 )
  +        throws IOException
  +    {
  +        final boolean file1Exists = file1.exists();
  +        if( file1Exists != file2.exists() )
  +        {
  +            return false;
  +        }
  +
  +        if( !file1Exists )
  +        {
  +            // two not existing files are equal
  +            return true;
  +        }
  +
  +        if( file1.isDirectory() || file2.isDirectory() )
  +        {
  +            // don't want to compare directory contents
  +            return false;
  +        }
  +
  +        InputStream input1 = null;
  +        InputStream input2 = null;
  +        try
  +        {
  +            input1 = new FileInputStream( file1 );
  +            input2 = new FileInputStream( file2 );
  +            final InputStream bufferedInput1 = new BufferedInputStream( input1 );
  +            final InputStream bufferedInput2 = new BufferedInputStream( input2 );
  +
  +            int ch = bufferedInput1.read();
  +            while( ch != -1 )
  +            {
  +                if( ch != bufferedInput2.read() )
  +                {
  +                    return false;
  +                }
  +                ch = bufferedInput1.read();
  +            }
  +            if( -1 != bufferedInput2.read() )
  +            {
  +                return false;
  +            }
  +            return true;
  +        }
  +        finally
  +        {
  +            IOUtil.shutdownStream( input1 );
  +            IOUtil.shutdownStream( input2 );
  +        }
  +    }
  +
  +    /**
        * Convert from a <code>URL</code> to a <code>File</code>.
        * @param url File URL.
        * @return The equivalent <code>File</code> object, or <code>null</code> if the URL's protocol
  @@ -251,7 +311,7 @@
        * (and any parent directories) will be created. If a file <code>source</code> in
        * <code>destinationDirectory</code> exists, it will be overwritten.
        *
  -     * @param source An existing <code>File</code> to copy. 
  +     * @param source An existing <code>File</code> to copy.
        * @param destination A directory to copy <code>source</code> into.
        *
        * @throws FileNotFoundException if <code>source</code> isn't a normal file.
  @@ -259,7 +319,7 @@
        * @throws IOException if <code>source</code> does not exist, the file in
        * <code>destinationDirectory</code> cannot be written to, or an IO error occurs during copying.
        */
  -        public static void copyFileToDirectory( final String source,
  +    public static void copyFileToDirectory( final String source,
                                               final String destinationDirectory )
           throws IOException
       {
  @@ -272,7 +332,7 @@
        * (and any parent directories) will be created. If a file <code>source</code> in
        * <code>destinationDirectory</code> exists, it will be overwritten.
        *
  -     * @param source An existing <code>File</code> to copy. 
  +     * @param source An existing <code>File</code> to copy.
        * @param destination A directory to copy <code>source</code> into.
        *
        * @throws FileNotFoundException if <code>source</code> isn't a normal file.
  @@ -297,7 +357,7 @@
        * created if they don't already exist. <code>destination</code> will be overwritten if it
        * already exists.
        *
  -     * @param source An existing non-directory <code>File</code> to copy bytes from. 
  +     * @param source An existing non-directory <code>File</code> to copy bytes from.
        * @param destination A non-directory <code>File</code> to write bytes to (possibly
        * overwriting).
        *
  @@ -313,7 +373,8 @@
           //check source exists
           if( !source.exists() )
           {
  -            throw new IOException( "File " + source + " does not exist" );
  +            final String message = "File " + source + " does not exist";
  +            throw new IOException( message );
           }
   
           //does destinations directory exist ?
  @@ -326,8 +387,9 @@
           //make sure we can write to destination
           if( destination.exists() && !destination.canWrite() )
           {
  -            throw new IOException( "Unable to open file " +
  -                                   destination + " for writing." );
  +            final String message = "Unable to open file " +
  +                destination + " for writing.";
  +            throw new IOException( message );
           }
   
           final FileInputStream input = new FileInputStream( source );
  @@ -338,8 +400,9 @@
   
           if( source.length() != destination.length() )
           {
  -            throw new IOException( "Failed to copy full contents from " + source +
  -                                   " to " + destination );
  +            final String message = "Failed to copy full contents from " + source +
  +                " to " + destination;
  +            throw new IOException( message );
           }
       }
   
  @@ -348,7 +411,7 @@
        * The directories up to <code>destination</code> will be created if they don't already exist.
        * <code>destination</code> will be overwritten if it already exists.
        *
  -     * @param source A <code>URL</code> to copy bytes from. 
  +     * @param source A <code>URL</code> to copy bytes from.
        * @param destination A non-directory <code>File</code> to write bytes to (possibly
        * overwriting).
        *
  @@ -372,8 +435,9 @@
           //make sure we can write to destination
           if( destination.exists() && !destination.canWrite() )
           {
  -            throw new IOException( "Unable to open file " +
  -                                   destination + " for writing." );
  +            final String message = "Unable to open file " +
  +                destination + " for writing.";
  +            throw new IOException( message );
           }
   
   
  @@ -574,7 +638,8 @@
           {
               if( false == file.delete() )
               {
  -                throw new IOException( "File " + file + " unable to be deleted." );
  +                final String message = "File " + file + " unable to be deleted.";
  +                throw new IOException( message );
               }
           }
       }
  @@ -623,12 +688,14 @@
       {
           if( !directory.exists() )
           {
  -            throw new IllegalArgumentException( directory + " does not exist" );
  +            final String message = directory + " does not exist";
  +            throw new IllegalArgumentException( message );
           }
   
           if( !directory.isDirectory() )
           {
  -            throw new IllegalArgumentException( directory + " is not a directory" );
  +            final String message = directory + " is not a directory";
  +            throw new IllegalArgumentException( message );
           }
   
           final File[] files = directory.listFiles();
  @@ -692,12 +759,14 @@
       {
           if( !directory.exists() )
           {
  -            throw new IllegalArgumentException( directory + " does not exist" );
  +            final String message = directory + " does not exist";
  +            throw new IllegalArgumentException( message );
           }
   
           if( !directory.isDirectory() )
           {
  -            throw new IllegalArgumentException( directory + " is not a directory" );
  +            final String message = directory + " is not a directory";
  +            throw new IllegalArgumentException( message );
           }
   
           long size = 0;
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>