Browse Source

Refactored Packing code so that only the absolute minimum is contained in sub-classes

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270784 13f79535-47bb-0310-9956-ffa450edef68
master
Peter Donald 23 years ago
parent
commit
b90678157d
6 changed files with 146 additions and 192 deletions
  1. +8
    -34
      proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/BZip2.java
  2. +5
    -28
      proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/GZip.java
  3. +60
    -34
      proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Pack.java
  4. +8
    -34
      proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/BZip2.java
  5. +5
    -28
      proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/GZip.java
  6. +60
    -34
      proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Pack.java

+ 8
- 34
proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/BZip2.java View File

@@ -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 @@ import org.apache.tools.bzip2.CBZip2OutputStream;
* 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 );
}
}

+ 5
- 28
proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/GZip.java View File

@@ -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 @@ import org.apache.myrmidon.api.TaskException;
* @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 );
}
}

+ 60
- 34
proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Pack.java View File

@@ -9,88 +9,114 @@ 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 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 void zipFile( File file, OutputStream zOut )
protected abstract OutputStream getPackingStream( OutputStream output )
throws TaskException, IOException;

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()
{
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_src;
}

protected final File getZipFile()
{
return m_zipFile;
}
}

+ 8
- 34
proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/BZip2.java View File

@@ -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 @@ import org.apache.tools.bzip2.CBZip2OutputStream;
* 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 );
}
}

+ 5
- 28
proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/GZip.java View File

@@ -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 @@ import org.apache.myrmidon.api.TaskException;
* @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 );
}
}

+ 60
- 34
proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Pack.java View File

@@ -9,88 +9,114 @@ 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 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 void zipFile( File file, OutputStream zOut )
protected abstract OutputStream getPackingStream( OutputStream output )
throws TaskException, IOException;

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()
{
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_src;
}

protected final File getZipFile()
{
return m_zipFile;
}
}

Loading…
Cancel
Save