From e094f083b680f93fd54c41f38216b20022b6dffa Mon Sep 17 00:00:00 2001 From: Peter Donald Date: Sat, 19 Jan 2002 07:02:13 +0000 Subject: [PATCH] Refactored Unpacking code so that only the absolute minimum is contained in sub-classes git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270783 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/ant/taskdefs/BUnzip2.java | 100 ++------------ .../org/apache/tools/ant/taskdefs/GUnzip.java | 73 +--------- .../org/apache/tools/ant/taskdefs/Unpack.java | 128 +++++++++++++----- .../apache/tools/ant/taskdefs/BUnzip2.java | 100 ++------------ .../org/apache/tools/ant/taskdefs/GUnzip.java | 73 +--------- .../org/apache/tools/ant/taskdefs/Unpack.java | 128 +++++++++++++----- 6 files changed, 222 insertions(+), 380 deletions(-) diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/BUnzip2.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/BUnzip2.java index 4d1192c3e..27a208542 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/BUnzip2.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/BUnzip2.java @@ -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 @@ import org.apache.tools.bzip2.CBZip2InputStream; * * @author Magesh Umasankar */ - -public class BUnzip2 extends Unpack +public class BUnzip2 + extends Unpack { - private final static String DEFAULT_EXTENSION = ".bz2"; protected String getDefaultExtension() @@ -31,90 +28,17 @@ public class BUnzip2 extends Unpack 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 ); } } diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/GUnzip.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/GUnzip.java index e026e95a5..3510d8639 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/GUnzip.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/GUnzip.java @@ -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 @@ import org.apache.myrmidon.api.TaskException; * @author Stefan Bodewig * @author Magesh Umasankar */ - -public class GUnzip extends Unpack +public class GUnzip + extends Unpack { - private final static String DEFAULT_EXTENSION = ".gz"; protected String getDefaultExtension() @@ -31,68 +29,9 @@ public class GUnzip extends Unpack 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 ); } } diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Unpack.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Unpack.java index e0346f5d2..51db213bf 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Unpack.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/Unpack.java @@ -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 Magesh Umasankar + * @author Peter Donald + * @author Stefan Bodewig */ - -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; + } } diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/BUnzip2.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/BUnzip2.java index 4d1192c3e..27a208542 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/BUnzip2.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/BUnzip2.java @@ -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 @@ import org.apache.tools.bzip2.CBZip2InputStream; * * @author Magesh Umasankar */ - -public class BUnzip2 extends Unpack +public class BUnzip2 + extends Unpack { - private final static String DEFAULT_EXTENSION = ".bz2"; protected String getDefaultExtension() @@ -31,90 +28,17 @@ public class BUnzip2 extends Unpack 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 ); } } diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/GUnzip.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/GUnzip.java index e026e95a5..3510d8639 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/GUnzip.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/GUnzip.java @@ -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 @@ import org.apache.myrmidon.api.TaskException; * @author Stefan Bodewig * @author Magesh Umasankar */ - -public class GUnzip extends Unpack +public class GUnzip + extends Unpack { - private final static String DEFAULT_EXTENSION = ".gz"; protected String getDefaultExtension() @@ -31,68 +29,9 @@ public class GUnzip extends Unpack 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 ); } } diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Unpack.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Unpack.java index e0346f5d2..51db213bf 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Unpack.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/Unpack.java @@ -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 Magesh Umasankar + * @author Peter Donald + * @author Stefan Bodewig */ - -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; + } }