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-myrmidon/antlib/src/java/org/apache/antlib/archive BUnzip2.java BZip2.java GUnzip.java GZip.java Pack.java Unpack.java
Date Sun, 14 Apr 2002 10:54:02 GMT
donaldp     02/04/14 03:54:02

  Added:       antlib/src/java/org/apache/antlib/archive BUnzip2.java
                        BZip2.java GUnzip.java GZip.java Pack.java
                        Unpack.java
  Log:
  Added in archive files. Should also move the zip based tasks in here aswell - at least as
soon as we remove dependencies on ant1.x manifest wrappers.
  
  Revision  Changes    Path
  1.1                  jakarta-ant-myrmidon/antlib/src/java/org/apache/antlib/archive/BUnzip2.java
  
  Index: BUnzip2.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.antlib.archive;
  
  import java.io.IOException;
  import java.io.InputStream;
  import org.apache.myrmidon.api.TaskException;
  import org.apache.excalibur.bzip2.CBZip2InputStream;
  
  /**
   * Expands a file that has been compressed with the BZIP2 algorithm. Normally
   * used to compress non-compressed archives such as TAR files.
   *
   * @author <a href="mailto:umagesh@rediffmail.com">Magesh Umasankar</a>
   * @ant.task name="bunzip2"
   */
  public class BUnzip2
      extends Unpack
  {
      private static final String DEFAULT_EXTENSION = ".bz2";
  
      protected String getDefaultExtension()
      {
          return DEFAULT_EXTENSION;
      }
  
      protected InputStream getUnpackingStream( final InputStream input )
          throws TaskException, IOException
      {
          final int b1 = input.read();
          final int b2 = input.read();
          if( b1 != 'B' || b2 != 'Z' )
          {
              final String message = "Invalid bz2 file.";
              throw new TaskException( message );
          }
  
          return new CBZip2InputStream( input );
      }
  }
  
  
  
  1.1                  jakarta-ant-myrmidon/antlib/src/java/org/apache/antlib/archive/BZip2.java
  
  Index: BZip2.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.antlib.archive;
  
  import java.io.IOException;
  import java.io.OutputStream;
  import org.apache.excalibur.bzip2.CBZip2OutputStream;
  import org.apache.myrmidon.api.TaskException;
  
  /**
   * Compresses a file with the BZip2 algorithm. Normally used to compress
   * 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>
   * @ant.task name="bzip2"
   */
  public class BZip2
      extends Pack
  {
      private static final byte[] HEADER = new byte[]{(byte)'B', (byte)'Z'};
  
      protected OutputStream getPackingStream( OutputStream output )
          throws TaskException, IOException
      {
          output.write( HEADER );
          return new CBZip2OutputStream( output );
      }
  }
  
  
  
  1.1                  jakarta-ant-myrmidon/antlib/src/java/org/apache/antlib/archive/GUnzip.java
  
  Index: GUnzip.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.antlib.archive;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.zip.GZIPInputStream;
  import org.apache.myrmidon.api.TaskException;
  
  /**
   * Expands a file that has been compressed with the GZIP algorithm. Normally
   * used to compress non-compressed archives such as TAR files.
   *
   * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
   * @author <a href="mailto:umagesh@rediffmail.com">Magesh Umasankar</a>
   * @ant.task name="gunzip"
   */
  public class GUnzip
      extends Unpack
  {
      private static final String DEFAULT_EXTENSION = ".gz";
  
      protected String getDefaultExtension()
      {
          return DEFAULT_EXTENSION;
      }
  
      protected InputStream getUnpackingStream( InputStream input )
          throws TaskException, IOException
      {
          return new GZIPInputStream( input );
      }
  }
  
  
  
  1.1                  jakarta-ant-myrmidon/antlib/src/java/org/apache/antlib/archive/GZip.java
  
  Index: GZip.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.antlib.archive;
  
  import java.io.IOException;
  import java.io.OutputStream;
  import java.util.zip.GZIPOutputStream;
  import org.apache.myrmidon.api.TaskException;
  
  /**
   * Compresses a file with the GZIP algorithm. Normally used to compress
   * non-compressed archives such as TAR files.
   *
   * @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>
   * @ant.task name="gzip"
   */
  public class GZip
      extends Pack
  {
      protected OutputStream getPackingStream( final OutputStream output )
          throws TaskException, IOException
      {
          return new GZIPOutputStream( output );
      }
  }
  
  
  
  1.1                  jakarta-ant-myrmidon/antlib/src/java/org/apache/antlib/archive/Pack.java
  
  Index: Pack.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.antlib.archive;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.OutputStream;
  import org.apache.avalon.excalibur.io.IOUtil;
  import org.apache.myrmidon.api.AbstractTask;
  import org.apache.myrmidon.api.TaskException;
  
  /**
   * 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 AbstractTask
  {
      private File m_src;
      private File m_zipFile;
  
      public void setSrc( final File src )
      {
          m_src = src;
      }
  
      public void setZipfile( final File zipFile )
      {
          m_zipFile = zipFile;
      }
  
      public void execute()
          throws TaskException
      {
          validate();
          final String message = "Building: " + m_zipFile.getAbsolutePath();
          getContext().verbose( message );
          pack();
      }
  
      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 " + getContext().getName() +
                  ":" + ioe.getMessage();
              throw new TaskException( message, ioe );
          }
          finally
          {
              IOUtil.shutdownStream( output );
          }
      }
  
      protected abstract OutputStream getPackingStream( OutputStream output )
          throws TaskException, IOException;
  
      protected final void copy( final File file, final OutputStream output )
          throws IOException
      {
          final FileInputStream input = new FileInputStream( file );
          try
          {
              IOUtil.copy( input, output );
          }
          finally
          {
              IOUtil.shutdownStream( input );
          }
      }
  
      private void validate()
          throws TaskException
      {
          if( null == m_zipFile )
          {
              final String message = "zipfile attribute is required";
              throw new TaskException( message );
          }
  
          if( null == m_src )
          {
              final String message = "src attribute is required";
              throw new TaskException( message );
          }
  
          if( m_src.isDirectory() )
          {
              final String message = "Src attribute must not " +
                  "represent a directory!";
              throw new TaskException( message );
          }
      }
  
      protected final File getSrc()
      {
          return m_src;
      }
  
      protected final File getZipFile()
      {
          return m_zipFile;
      }
  }
  
  
  
  1.1                  jakarta-ant-myrmidon/antlib/src/java/org/apache/antlib/archive/Unpack.java
  
  Index: Unpack.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.antlib.archive;
  
  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;
  
  /**
   * 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 AbstractTask
  {
      private File m_dest;
      private File m_src;
  
      public void setDest( final File dest )
      {
          m_dest = dest;
      }
  
      public void setSrc( final File src )
      {
          m_src = src;
      }
  
      public void execute()
          throws TaskException
      {
          validate();
  
          final File src = getSrc();
          final File dest = getDest();
  
          if( src.lastModified() > dest.lastModified() )
          {
              final String message = "Expanding " + src.getAbsolutePath() +
                  " to " + dest.getAbsolutePath();
              getContext().verbose( message );
  
              extract();
          }
      }
  
      protected abstract String getDefaultExtension();
  
      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 File createDestFile()
      {
          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 ) ) )
          {
              final String child = sourceName.substring( 0, index );
              return new File( m_dest, child );
          }
          else
          {
              return new File( m_dest, sourceName );
          }
      }
  
      private void validate()
          throws TaskException
      {
          if( null == m_src )
          {
              final String message = "No Src for " + getContext().getName() + " specified";
              throw new TaskException( message );
          }
  
          if( !m_src.exists() )
          {
              final String message = "Src doesn't exist";
              throw new TaskException( message );
          }
  
          if( m_src.isDirectory() )
          {
              final String message = "Cannot expand a directory";
              throw new TaskException( message );
          }
  
          if( null == m_dest )
          {
              m_dest = new File( m_src.getParent() );
          }
  
          if( m_dest.isDirectory() )
          {
              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