donaldp     02/02/02 05:11:52

  Modified:    src/java/org/apache/avalon/excalibur/io IOUtil.java
                        FileUtil.java
  Log:
  Refactor and add the ability to compare the contents of streams
  
  Revision  Changes    Path
  1.10      +63 -16    
jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/io/IOUtil.java
  
  Index: IOUtil.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/io/IOUtil.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- IOUtil.java       20 Jan 2002 09:45:49 -0000      1.9
  +++ IOUtil.java       2 Feb 2002 13:11:52 -0000       1.10
  @@ -9,8 +9,8 @@
   
   import java.io.BufferedInputStream;
   import java.io.BufferedOutputStream;
  -import java.io.BufferedReader;
  -import java.io.BufferedWriter;
  +import java.io.ByteArrayInputStream;
  +import java.io.ByteArrayOutputStream;
   import java.io.IOException;
   import java.io.InputStream;
   import java.io.InputStreamReader;
  @@ -20,9 +20,6 @@
   import java.io.StringReader;
   import java.io.StringWriter;
   import java.io.Writer;
  -import java.io.ByteArrayInputStream;
  -import java.io.ByteArrayOutputStream;
  -import java.io.PrintStream;
   
   /**
    * General IO Stream manipulation.
  @@ -71,7 +68,7 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Jeff Turner</a>
  - * @version CVS $Revision: 1.9 $ $Date: 2002/01/20 09:45:49 $
  + * @version CVS $Revision: 1.10 $ $Date: 2002/02/02 13:11:52 $
    * @since 4.0
    */
   
  @@ -129,8 +126,13 @@
               return;
           }
   
  -        try { input.close(); }
  -        catch( final IOException ioe ) {}
  +        try
  +        {
  +            input.close();
  +        }
  +        catch( final IOException ioe )
  +        {
  +        }
       }
   
       /**
  @@ -146,8 +148,13 @@
               return;
           }
   
  -        try { output.close(); }
  -        catch( final IOException ioe ) {}
  +        try
  +        {
  +            output.close();
  +        }
  +        catch( final IOException ioe )
  +        {
  +        }
       }
   
       /**
  @@ -162,8 +169,13 @@
               return;
           }
   
  -        try { output.close(); }
  -        catch( final IOException ioe ) {}
  +        try
  +        {
  +            output.close();
  +        }
  +        catch( final IOException ioe )
  +        {
  +        }
       }
   
       /**
  @@ -178,8 +190,13 @@
               return;
           }
   
  -        try { input.close(); }
  -        catch( final IOException ioe ) {}
  +        try
  +        {
  +            input.close();
  +        }
  +        catch( final IOException ioe )
  +        {
  +        }
       }
   
       ///////////////////////////////////////////////////////////////
  @@ -206,7 +223,7 @@
       {
           final byte[] buffer = new byte[ bufferSize ];
           int n = 0;
  -        while( -1 != (n = input.read( buffer )) )
  +        while( -1 != ( n = input.read( buffer ) ) )
           {
               output.write( buffer, 0, n );
           }
  @@ -230,7 +247,7 @@
       {
           final char[] buffer = new char[ bufferSize ];
           int n = 0;
  -        while( -1 != (n = input.read( buffer )) )
  +        while( -1 != ( n = input.read( buffer ) ) )
           {
               output.write( buffer, 0, n );
           }
  @@ -701,5 +718,35 @@
           throws IOException
       {
           output.write( input );
  +    }
  +
  +    /**
  +     * Compare the contents of two Streams to determine if they are equal or 
not.
  +     *
  +     * @param input1 the first stream
  +     * @param input2 the second stream
  +     * @return true if the content of the streams are equal or they both 
don't exist, false otherwise
  +     */
  +    public static boolean contentEquals( final InputStream input1,
  +                                         final InputStream input2 )
  +        throws IOException
  +    {
  +        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;
       }
   }
  
  
  
  1.18      +2 -17     
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.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- FileUtil.java     20 Jan 2002 09:46:43 -0000      1.17
  +++ FileUtil.java     2 Feb 2002 13:11:52 -0000       1.18
  @@ -44,7 +44,7 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Jeff Turner</a>
  - * @version CVS $Revision: 1.17 $ $Date: 2002/01/20 09:46:43 $
  + * @version CVS $Revision: 1.18 $ $Date: 2002/02/02 13:11:52 $
    * @since 4.0
    */
   public final class FileUtil
  @@ -91,23 +91,8 @@
           {
               input1 = new FileInputStream( file1 );
               input2 = new FileInputStream( file2 );
  -            final InputStream bufferedInput1 = new BufferedInputStream( 
input1 );
  -            final InputStream bufferedInput2 = new BufferedInputStream( 
input2 );
  +            return IOUtil.contentEquals( input1, 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
           {
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to