ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs BZip2.java GZip.java Pack.java
Date Sat, 19 Jan 2002 07:10:44 GMT
donaldp     02/01/18 23:10:44

  Modified:    proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs
                        BZip2.java GZip.java Pack.java
  Log:
  Refactored Packing code so that only the absolute minimum is contained in sub-classes
  
  Revision  Changes    Path
  1.6       +8 -34     jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/BZip2.java
  
  Index: BZip2.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/BZip2.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- BZip2.java	23 Dec 2001 06:33:35 -0000	1.5
  +++ BZip2.java	19 Jan 2002 07:10:44 -0000	1.6
  @@ -7,9 +7,8 @@
    */
   package org.apache.tools.ant.taskdefs;
   
  -import java.io.BufferedOutputStream;
  -import java.io.FileOutputStream;
   import java.io.IOException;
  +import java.io.OutputStream;
   import org.apache.myrmidon.api.TaskException;
   import org.apache.tools.bzip2.CBZip2OutputStream;
   
  @@ -18,42 +17,17 @@
    * non-compressed archives such as TAR files.
    *
    * @author <a href="mailto:umagesh@rediffmail.com">Magesh Umasankar</a>
  + * @author <a href="mailto:peter@apache.org">Peter Donald</a>
    */
  -
   public class BZip2
       extends Pack
   {
  -    protected void pack()
  -        throws TaskException
  +    private static final byte[] HEADER = new byte[]{(byte)'B', (byte)'Z'};
  +
  +    protected OutputStream getPackingStream( OutputStream output )
  +        throws TaskException, IOException
       {
  -        CBZip2OutputStream zOut = null;
  -        try
  -        {
  -            BufferedOutputStream bos =
  -                new BufferedOutputStream( new FileOutputStream( zipFile ) );
  -            bos.write( 'B' );
  -            bos.write( 'Z' );
  -            zOut = new CBZip2OutputStream( bos );
  -            zipFile( source, zOut );
  -        }
  -        catch( IOException ioe )
  -        {
  -            String msg = "Problem creating bzip2 " + ioe.getMessage();
  -            throw new TaskException( msg, ioe );
  -        }
  -        finally
  -        {
  -            if( zOut != null )
  -            {
  -                try
  -                {
  -                    // close up
  -                    zOut.close();
  -                }
  -                catch( IOException e )
  -                {
  -                }
  -            }
  -        }
  +        output.write( HEADER );
  +        return new CBZip2OutputStream( output );
       }
   }
  
  
  
  1.6       +5 -28     jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/GZip.java
  
  Index: GZip.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/GZip.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- GZip.java	23 Dec 2001 06:33:35 -0000	1.5
  +++ GZip.java	19 Jan 2002 07:10:44 -0000	1.6
  @@ -7,8 +7,8 @@
    */
   package org.apache.tools.ant.taskdefs;
   
  -import java.io.FileOutputStream;
   import java.io.IOException;
  +import java.io.OutputStream;
   import java.util.zip.GZIPOutputStream;
   import org.apache.myrmidon.api.TaskException;
   
  @@ -19,37 +19,14 @@
    * @author James Davidson <a href="mailto:duncan@x180.com">duncan@x180.com</a>
    * @author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a>
    * @author <a href="mailto:umagesh@rediffmail.com">Magesh Umasankar</a>
  + * @author <a href="mailto:peter@apache.org">Peter Donald</a>
    */
   public class GZip
       extends Pack
   {
  -    protected void pack()
  -        throws TaskException
  +    protected OutputStream getPackingStream( final OutputStream output )
  +        throws TaskException, IOException
       {
  -        GZIPOutputStream zOut = null;
  -        try
  -        {
  -            zOut = new GZIPOutputStream( new FileOutputStream( zipFile ) );
  -            zipFile( source, zOut );
  -        }
  -        catch( IOException ioe )
  -        {
  -            String msg = "Problem creating gzip " + ioe.getMessage();
  -            throw new TaskException( msg, ioe );
  -        }
  -        finally
  -        {
  -            if( zOut != null )
  -            {
  -                try
  -                {
  -                    // close up
  -                    zOut.close();
  -                }
  -                catch( IOException e )
  -                {
  -                }
  -            }
  -        }
  +        return new GZIPOutputStream( output );
       }
   }
  
  
  
  1.8       +60 -34    jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Pack.java
  
  Index: Pack.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Pack.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- Pack.java	23 Dec 2001 06:33:35 -0000	1.7
  +++ Pack.java	19 Jan 2002 07:10:44 -0000	1.8
  @@ -9,88 +9,114 @@
   
   import java.io.File;
   import java.io.FileInputStream;
  +import java.io.FileOutputStream;
   import java.io.IOException;
  -import java.io.InputStream;
   import java.io.OutputStream;
  +import java.util.zip.GZIPOutputStream;
  +import org.apache.avalon.excalibur.io.IOUtil;
  +import org.apache.myrmidon.api.AbstractTask;
   import org.apache.myrmidon.api.TaskException;
  -import org.apache.tools.ant.Task;
   
   /**
    * Abstract Base class for pack tasks.
    *
    * @author <a href="mailto:umagesh@rediffmail.com">Magesh Umasankar</a>
  + * @author <a href="mailto:peter@apache.org">Peter Donald</a>
    */
  -
  -public abstract class Pack extends Task
  +public abstract class Pack
  +    extends AbstractTask
   {
  -    protected File source;
  -
  -    protected File zipFile;
  +    private File m_src;
  +    private File m_zipFile;
   
  -    public void setSrc( File src )
  +    public void setSrc( final File src )
       {
  -        source = src;
  +        m_src = src;
       }
   
  -    public void setZipfile( File zipFile )
  +    public void setZipfile( final File zipFile )
       {
  -        this.zipFile = zipFile;
  +        m_zipFile = zipFile;
       }
   
       public void execute()
           throws TaskException
       {
           validate();
  -        getLogger().info( "Building: " + zipFile.getAbsolutePath() );
  +        final String message = "Building: " + m_zipFile.getAbsolutePath();
  +        getLogger().info( message );
           pack();
       }
   
  -    protected abstract void pack()
  -        throws TaskException;
  +    private void pack()
  +        throws TaskException
  +    {
  +        OutputStream output = null;
  +        try
  +        {
  +            final FileOutputStream fileOutput = new FileOutputStream( getZipFile() );
  +            output = getPackingStream( fileOutput );
  +            copy( getSrc(), output );
  +        }
  +        catch( final IOException ioe )
  +        {
  +            final String message = "Problem creating " + getName() +
  +                ":" + ioe.getMessage();
  +            throw new TaskException( message, ioe );
  +        }
  +        finally
  +        {
  +            IOUtil.shutdownStream( output );
  +        }
  +    }
  +
  +    protected abstract OutputStream getPackingStream( OutputStream output )
  +        throws TaskException, IOException;
   
  -    protected void zipFile( File file, OutputStream zOut )
  +    protected final void copy( final File file, final OutputStream output )
           throws IOException
       {
  -        FileInputStream fIn = new FileInputStream( file );
  +        final FileInputStream input = new FileInputStream( file );
           try
           {
  -            zipFile( fIn, zOut );
  +            IOUtil.copy( input, output );
           }
           finally
           {
  -            fIn.close();
  +            IOUtil.shutdownStream( input );
           }
       }
   
       private void validate()
           throws TaskException
       {
  -        if( zipFile == null )
  +        if( null == m_zipFile )
           {
  -            throw new TaskException( "zipfile attribute is required" );
  +            final String message = "zipfile attribute is required";
  +            throw new TaskException( message );
           }
   
  -        if( source == null )
  +        if( null == m_src )
           {
  -            throw new TaskException( "src attribute is required" );
  +            final String message = "src attribute is required";
  +            throw new TaskException( message );
           }
   
  -        if( source.isDirectory() )
  +        if( m_src.isDirectory() )
           {
  -            throw new TaskException( "Src attribute must not " +
  -                                     "represent a directory!" );
  +            final String message = "Src attribute must not " +
  +                "represent a directory!";
  +            throw new TaskException( message );
           }
       }
   
  -    private void zipFile( InputStream in, OutputStream zOut )
  -        throws IOException
  +    protected final File getSrc()
  +    {
  +        return m_src;
  +    }
  +
  +    protected final File getZipFile()
       {
  -        byte[] buffer = new byte[ 8 * 1024 ];
  -        int count = 0;
  -        do
  -        {
  -            zOut.write( buffer, 0, count );
  -            count = in.read( buffer, 0, buffer.length );
  -        } while( count != -1 );
  +        return m_zipFile;
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message