From b90678157d3ee5bb3ea2904ef4267a77ec0a7686 Mon Sep 17 00:00:00 2001 From: Peter Donald Date: Sat, 19 Jan 2002 07:10:44 +0000 Subject: [PATCH] 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 --- .../org/apache/tools/ant/taskdefs/BZip2.java | 42 ++------- .../org/apache/tools/ant/taskdefs/GZip.java | 33 +------ .../org/apache/tools/ant/taskdefs/Pack.java | 94 ++++++++++++------- .../org/apache/tools/ant/taskdefs/BZip2.java | 42 ++------- .../org/apache/tools/ant/taskdefs/GZip.java | 33 +------ .../org/apache/tools/ant/taskdefs/Pack.java | 94 ++++++++++++------- 6 files changed, 146 insertions(+), 192 deletions(-) diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/BZip2.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/BZip2.java index ac7d470b4..398926a33 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/BZip2.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/BZip2.java @@ -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 Magesh Umasankar + * @author Peter Donald */ - 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 ); } } diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/GZip.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/GZip.java index 6da6eff23..d6b09effc 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/GZip.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/GZip.java @@ -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 duncan@x180.com * @author Jon S. Stevens jon@clearink.com * @author Magesh Umasankar + * @author Peter Donald */ 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 ); } } diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Pack.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Pack.java index afb375b28..1b753509e 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Pack.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Pack.java @@ -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 Magesh Umasankar + * @author Peter Donald */ - -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; } } diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/BZip2.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/BZip2.java index ac7d470b4..398926a33 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/BZip2.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/BZip2.java @@ -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 Magesh Umasankar + * @author Peter Donald */ - 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 ); } } diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/GZip.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/GZip.java index 6da6eff23..d6b09effc 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/GZip.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/GZip.java @@ -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 duncan@x180.com * @author Jon S. Stevens jon@clearink.com * @author Magesh Umasankar + * @author Peter Donald */ 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 ); } } diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Pack.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Pack.java index afb375b28..1b753509e 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Pack.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Pack.java @@ -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 Magesh Umasankar + * @author Peter Donald */ - -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; } }