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 BUnzip2.java GUnzip.java Unpack.java
Date Sat, 19 Jan 2002 07:02:13 GMT
donaldp     02/01/18 23:02:13

  Modified:    proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs
                        BUnzip2.java GUnzip.java Unpack.java
  Log:
  Refactored Unpacking code so that only the absolute minimum is contained in sub-classes
  
  Revision  Changes    Path
  1.8       +12 -88    jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/BUnzip2.java
  
  Index: BUnzip2.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/BUnzip2.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- BUnzip2.java	23 Dec 2001 06:33:35 -0000	1.7
  +++ BUnzip2.java	19 Jan 2002 07:02:13 -0000	1.8
  @@ -7,10 +7,8 @@
    */
   package org.apache.tools.ant.taskdefs;
   
  -import java.io.BufferedInputStream;
  -import java.io.FileInputStream;
  -import java.io.FileOutputStream;
   import java.io.IOException;
  +import java.io.InputStream;
   import org.apache.myrmidon.api.TaskException;
   import org.apache.tools.bzip2.CBZip2InputStream;
   
  @@ -20,10 +18,9 @@
    *
    * @author <a href="mailto:umagesh@rediffmail.com">Magesh Umasankar</a>
    */
  -
  -public class BUnzip2 extends Unpack
  +public class BUnzip2
  +    extends Unpack
   {
  -
       private final static String DEFAULT_EXTENSION = ".bz2";
   
       protected String getDefaultExtension()
  @@ -31,90 +28,17 @@
           return DEFAULT_EXTENSION;
       }
   
  -    protected void extract()
  -        throws TaskException
  +    protected InputStream getUnpackingStream( final InputStream input )
  +        throws TaskException, IOException
       {
  -        if( source.lastModified() > dest.lastModified() )
  +        final int b1 = input.read();
  +        final int b2 = input.read();
  +        if( b1 != 'B' || b2 != 'Z' )
           {
  -            getLogger().info( "Expanding " + source.getAbsolutePath() + " to "
  -                              + dest.getAbsolutePath() );
  -
  -            FileOutputStream out = null;
  -            CBZip2InputStream zIn = null;
  -            FileInputStream fis = null;
  -            BufferedInputStream bis = null;
  -            try
  -            {
  -                out = new FileOutputStream( dest );
  -                fis = new FileInputStream( source );
  -                bis = new BufferedInputStream( fis );
  -                int b = bis.read();
  -                if( b != 'B' )
  -                {
  -                    throw new TaskException( "Invalid bz2 file." );
  -                }
  -                b = bis.read();
  -                if( b != 'Z' )
  -                {
  -                    throw new TaskException( "Invalid bz2 file." );
  -                }
  -                zIn = new CBZip2InputStream( bis );
  -                byte[] buffer = new byte[ 8 * 1024 ];
  -                int count = 0;
  -                do
  -                {
  -                    out.write( buffer, 0, count );
  -                    count = zIn.read( buffer, 0, buffer.length );
  -                } while( count != -1 );
  -            }
  -            catch( IOException ioe )
  -            {
  -                String msg = "Problem expanding bzip2 " + ioe.getMessage();
  -                throw new TaskException( msg, ioe );
  -            }
  -            finally
  -            {
  -                if( bis != null )
  -                {
  -                    try
  -                    {
  -                        bis.close();
  -                    }
  -                    catch( IOException ioex )
  -                    {
  -                    }
  -                }
  -                if( fis != null )
  -                {
  -                    try
  -                    {
  -                        fis.close();
  -                    }
  -                    catch( IOException ioex )
  -                    {
  -                    }
  -                }
  -                if( out != null )
  -                {
  -                    try
  -                    {
  -                        out.close();
  -                    }
  -                    catch( IOException ioex )
  -                    {
  -                    }
  -                }
  -                if( zIn != null )
  -                {
  -                    try
  -                    {
  -                        zIn.close();
  -                    }
  -                    catch( IOException ioex )
  -                    {
  -                    }
  -                }
  -            }
  +            final String message = "Invalid bz2 file.";
  +            throw new TaskException( message );
           }
  +
  +        return new CBZip2InputStream( input );
       }
   }
  
  
  
  1.8       +6 -67     jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/GUnzip.java
  
  Index: GUnzip.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/GUnzip.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- GUnzip.java	23 Dec 2001 06:33:35 -0000	1.7
  +++ GUnzip.java	19 Jan 2002 07:02:13 -0000	1.8
  @@ -7,9 +7,8 @@
    */
   package org.apache.tools.ant.taskdefs;
   
  -import java.io.FileInputStream;
  -import java.io.FileOutputStream;
   import java.io.IOException;
  +import java.io.InputStream;
   import java.util.zip.GZIPInputStream;
   import org.apache.myrmidon.api.TaskException;
   
  @@ -20,10 +19,9 @@
    * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
    * @author <a href="mailto:umagesh@rediffmail.com">Magesh Umasankar</a>
    */
  -
  -public class GUnzip extends Unpack
  +public class GUnzip
  +    extends Unpack
   {
  -
       private final static String DEFAULT_EXTENSION = ".gz";
   
       protected String getDefaultExtension()
  @@ -31,68 +29,9 @@
           return DEFAULT_EXTENSION;
       }
   
  -    protected void extract()
  -        throws TaskException
  +    protected InputStream getUnpackingStream( InputStream input )
  +        throws TaskException, IOException
       {
  -        if( source.lastModified() > dest.lastModified() )
  -        {
  -            getLogger().info( "Expanding " + source.getAbsolutePath() + " to "
  -                              + dest.getAbsolutePath() );
  -
  -            FileOutputStream out = null;
  -            GZIPInputStream zIn = null;
  -            FileInputStream fis = null;
  -            try
  -            {
  -                out = new FileOutputStream( dest );
  -                fis = new FileInputStream( source );
  -                zIn = new GZIPInputStream( fis );
  -                byte[] buffer = new byte[ 8 * 1024 ];
  -                int count = 0;
  -                do
  -                {
  -                    out.write( buffer, 0, count );
  -                    count = zIn.read( buffer, 0, buffer.length );
  -                } while( count != -1 );
  -            }
  -            catch( IOException ioe )
  -            {
  -                String msg = "Problem expanding gzip " + ioe.getMessage();
  -                throw new TaskException( msg, ioe );
  -            }
  -            finally
  -            {
  -                if( fis != null )
  -                {
  -                    try
  -                    {
  -                        fis.close();
  -                    }
  -                    catch( IOException ioex )
  -                    {
  -                    }
  -                }
  -                if( out != null )
  -                {
  -                    try
  -                    {
  -                        out.close();
  -                    }
  -                    catch( IOException ioex )
  -                    {
  -                    }
  -                }
  -                if( zIn != null )
  -                {
  -                    try
  -                    {
  -                        zIn.close();
  -                    }
  -                    catch( IOException ioex )
  -                    {
  -                    }
  -                }
  -            }
  -        }
  +        return new GZIPInputStream( input );
       }
   }
  
  
  
  1.6       +93 -35    jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Unpack.java
  
  Index: Unpack.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Unpack.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Unpack.java	23 Dec 2001 06:33:35 -0000	1.5
  +++ Unpack.java	19 Jan 2002 07:02:13 -0000	1.6
  @@ -8,89 +8,147 @@
   package org.apache.tools.ant.taskdefs;
   
   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 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 unpack tasks.
    *
    * @author <a href="mailto:umagesh@rediffmail.com">Magesh Umasankar</a>
  + * @author <a href="mailto:peter@apache.org">Peter Donald</a>
  + * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
    */
  -
  -public abstract class Unpack extends Task
  +public abstract class Unpack
  +    extends AbstractTask
   {
  -    protected File dest;
  -
  -    protected File source;
  +    private File m_dest;
  +    private File m_src;
   
  -    public void setDest( String dest )
  -        throws TaskException
  +    public void setDest( final File dest )
       {
  -        this.dest = resolveFile( dest );
  +        m_dest = dest;
       }
   
  -    public void setSrc( String src )
  -        throws TaskException
  +    public void setSrc( final File src )
       {
  -        source = resolveFile( src );
  +        m_src = src;
       }
   
       public void execute()
           throws TaskException
       {
           validate();
  -        extract();
  +
  +        final File source = getSrc();
  +        final File dest = getDest();
  +
  +        if( source.lastModified() > dest.lastModified() )
  +        {
  +            final String message = "Expanding " + source.getAbsolutePath() +
  +                " to " + dest.getAbsolutePath();
  +            getLogger().info( message );
  +
  +            extract();
  +        }
       }
   
       protected abstract String getDefaultExtension();
   
  -    protected abstract void extract()
  -        throws TaskException;
  +    protected abstract InputStream getUnpackingStream( InputStream input )
  +        throws TaskException, IOException;
  +
  +    private void extract()
  +        throws TaskException
  +    {
  +        OutputStream output = null;
  +        InputStream input = null;
  +        InputStream fileInput = null;
  +        try
  +        {
  +            output = new FileOutputStream( getDest() );
  +            fileInput = new FileInputStream( getSrc() );
  +            input = getUnpackingStream( fileInput );
  +            IOUtil.copy( input, output );
  +        }
  +        catch( final IOException ioe )
  +        {
  +            final String message = "Problem expanding " + getSrc() +
  +                ":" + ioe.getMessage();
  +            throw new TaskException( message, ioe );
  +        }
  +        finally
  +        {
  +            IOUtil.shutdownStream( fileInput );
  +            IOUtil.shutdownStream( output );
  +            IOUtil.shutdownStream( input );
  +        }
  +    }
   
  -    private void createDestFile( String defaultExtension )
  +    private File createDestFile()
       {
  -        String sourceName = source.getName();
  -        int len = sourceName.length();
  -        if( defaultExtension != null
  -            && len > defaultExtension.length()
  -            && defaultExtension.equalsIgnoreCase( sourceName.substring( len - defaultExtension.length()
) ) )
  +        final String extension = getDefaultExtension();
  +        final String sourceName = m_src.getName();
  +        final int length = sourceName.length();
  +        final int index = length - extension.length();
  +
  +        if( null != extension &&
  +            length > extension.length() &&
  +            extension.equalsIgnoreCase( sourceName.substring( index ) ) )
           {
  -            dest = new File( dest, sourceName.substring( 0,
  -                                                         len - defaultExtension.length()
) );
  +            final String child = sourceName.substring( 0, index );
  +            return new File( m_dest, child );
           }
           else
           {
  -            dest = new File( dest, sourceName );
  +            return new File( m_dest, sourceName );
           }
       }
   
       private void validate()
           throws TaskException
       {
  -        if( source == null )
  +        if( null == m_src )
           {
  -            throw new TaskException( "No Src for gunzip specified" );
  +            final String message = "No Src for " + getName() + " specified";
  +            throw new TaskException( message );
           }
   
  -        if( !source.exists() )
  +        if( !m_src.exists() )
           {
  -            throw new TaskException( "Src doesn't exist" );
  +            final String message = "Src doesn't exist";
  +            throw new TaskException( message );
           }
   
  -        if( source.isDirectory() )
  +        if( m_src.isDirectory() )
           {
  -            throw new TaskException( "Cannot expand a directory" );
  +            final String message = "Cannot expand a directory";
  +            throw new TaskException( message );
           }
   
  -        if( dest == null )
  +        if( null == m_dest )
           {
  -            dest = new File( source.getParent() );
  +            m_dest = new File( m_src.getParent() );
           }
   
  -        if( dest.isDirectory() )
  +        if( m_dest.isDirectory() )
           {
  -            String defaultExtension = getDefaultExtension();
  -            createDestFile( defaultExtension );
  +            m_dest = createDestFile();
           }
  +    }
  +
  +    protected final File getDest()
  +    {
  +        return m_dest;
  +    }
  +
  +    protected final File getSrc()
  +    {
  +        return m_src;
       }
   }
  
  
  

--
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