git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270783 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -7,10 +7,8 @@ | |||||
| */ | */ | ||||
| package org.apache.tools.ant.taskdefs; | 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.IOException; | ||||
| import java.io.InputStream; | |||||
| import org.apache.myrmidon.api.TaskException; | import org.apache.myrmidon.api.TaskException; | ||||
| import org.apache.tools.bzip2.CBZip2InputStream; | import org.apache.tools.bzip2.CBZip2InputStream; | ||||
| @@ -20,10 +18,9 @@ import org.apache.tools.bzip2.CBZip2InputStream; | |||||
| * | * | ||||
| * @author <a href="mailto:umagesh@rediffmail.com">Magesh Umasankar</a> | * @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"; | private final static String DEFAULT_EXTENSION = ".bz2"; | ||||
| protected String getDefaultExtension() | protected String getDefaultExtension() | ||||
| @@ -31,90 +28,17 @@ public class BUnzip2 extends Unpack | |||||
| return DEFAULT_EXTENSION; | 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 ); | |||||
| } | } | ||||
| } | } | ||||
| @@ -7,9 +7,8 @@ | |||||
| */ | */ | ||||
| package org.apache.tools.ant.taskdefs; | package org.apache.tools.ant.taskdefs; | ||||
| import java.io.FileInputStream; | |||||
| import java.io.FileOutputStream; | |||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.io.InputStream; | |||||
| import java.util.zip.GZIPInputStream; | import java.util.zip.GZIPInputStream; | ||||
| import org.apache.myrmidon.api.TaskException; | import org.apache.myrmidon.api.TaskException; | ||||
| @@ -20,10 +19,9 @@ import org.apache.myrmidon.api.TaskException; | |||||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | ||||
| * @author <a href="mailto:umagesh@rediffmail.com">Magesh Umasankar</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"; | private final static String DEFAULT_EXTENSION = ".gz"; | ||||
| protected String getDefaultExtension() | protected String getDefaultExtension() | ||||
| @@ -31,68 +29,9 @@ public class GUnzip extends Unpack | |||||
| return DEFAULT_EXTENSION; | 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 ); | |||||
| } | } | ||||
| } | } | ||||
| @@ -8,89 +8,147 @@ | |||||
| package org.apache.tools.ant.taskdefs; | package org.apache.tools.ant.taskdefs; | ||||
| import java.io.File; | 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.myrmidon.api.TaskException; | ||||
| import org.apache.tools.ant.Task; | |||||
| /** | /** | ||||
| * Abstract Base class for unpack tasks. | * Abstract Base class for unpack tasks. | ||||
| * | * | ||||
| * @author <a href="mailto:umagesh@rediffmail.com">Magesh Umasankar</a> | * @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() | public void execute() | ||||
| throws TaskException | throws TaskException | ||||
| { | { | ||||
| validate(); | 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 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 | else | ||||
| { | { | ||||
| dest = new File( dest, sourceName ); | |||||
| return new File( m_dest, sourceName ); | |||||
| } | } | ||||
| } | } | ||||
| private void validate() | private void validate() | ||||
| throws TaskException | 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; | |||||
| } | |||||
| } | } | ||||
| @@ -7,10 +7,8 @@ | |||||
| */ | */ | ||||
| package org.apache.tools.ant.taskdefs; | 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.IOException; | ||||
| import java.io.InputStream; | |||||
| import org.apache.myrmidon.api.TaskException; | import org.apache.myrmidon.api.TaskException; | ||||
| import org.apache.tools.bzip2.CBZip2InputStream; | import org.apache.tools.bzip2.CBZip2InputStream; | ||||
| @@ -20,10 +18,9 @@ import org.apache.tools.bzip2.CBZip2InputStream; | |||||
| * | * | ||||
| * @author <a href="mailto:umagesh@rediffmail.com">Magesh Umasankar</a> | * @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"; | private final static String DEFAULT_EXTENSION = ".bz2"; | ||||
| protected String getDefaultExtension() | protected String getDefaultExtension() | ||||
| @@ -31,90 +28,17 @@ public class BUnzip2 extends Unpack | |||||
| return DEFAULT_EXTENSION; | 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 ); | |||||
| } | } | ||||
| } | } | ||||
| @@ -7,9 +7,8 @@ | |||||
| */ | */ | ||||
| package org.apache.tools.ant.taskdefs; | package org.apache.tools.ant.taskdefs; | ||||
| import java.io.FileInputStream; | |||||
| import java.io.FileOutputStream; | |||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.io.InputStream; | |||||
| import java.util.zip.GZIPInputStream; | import java.util.zip.GZIPInputStream; | ||||
| import org.apache.myrmidon.api.TaskException; | import org.apache.myrmidon.api.TaskException; | ||||
| @@ -20,10 +19,9 @@ import org.apache.myrmidon.api.TaskException; | |||||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | ||||
| * @author <a href="mailto:umagesh@rediffmail.com">Magesh Umasankar</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"; | private final static String DEFAULT_EXTENSION = ".gz"; | ||||
| protected String getDefaultExtension() | protected String getDefaultExtension() | ||||
| @@ -31,68 +29,9 @@ public class GUnzip extends Unpack | |||||
| return DEFAULT_EXTENSION; | 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 ); | |||||
| } | } | ||||
| } | } | ||||
| @@ -8,89 +8,147 @@ | |||||
| package org.apache.tools.ant.taskdefs; | package org.apache.tools.ant.taskdefs; | ||||
| import java.io.File; | 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.myrmidon.api.TaskException; | ||||
| import org.apache.tools.ant.Task; | |||||
| /** | /** | ||||
| * Abstract Base class for unpack tasks. | * Abstract Base class for unpack tasks. | ||||
| * | * | ||||
| * @author <a href="mailto:umagesh@rediffmail.com">Magesh Umasankar</a> | * @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() | public void execute() | ||||
| throws TaskException | throws TaskException | ||||
| { | { | ||||
| validate(); | 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 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 | else | ||||
| { | { | ||||
| dest = new File( dest, sourceName ); | |||||
| return new File( m_dest, sourceName ); | |||||
| } | } | ||||
| } | } | ||||
| private void validate() | private void validate() | ||||
| throws TaskException | 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; | |||||
| } | |||||
| } | } | ||||