maven-wagon-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/result ByteArrayResult.java FileResult.java StreamResult.java
Date Sat, 06 Dec 2003 05:35:47 GMT
donaldp     2003/12/05 21:35:47

  Modified:    wagon-api/src/main/java/org/apache/maven/wagon/result
                        ByteArrayResult.java FileResult.java
                        StreamResult.java
  Log:
  Rework the results so that they all extend StreamResult and share as much code as possible
  
  Revision  Changes    Path
  1.8       +27 -56    maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/result/ByteArrayResult.java
  
  Index: ByteArrayResult.java
  ===================================================================
  RCS file: /home/cvs/maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/result/ByteArrayResult.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ByteArrayResult.java	2 Dec 2003 18:01:29 -0000	1.7
  +++ ByteArrayResult.java	6 Dec 2003 05:35:47 -0000	1.8
  @@ -3,9 +3,9 @@
   /*
    * ====================================================================
    * The Apache Software License, Version 1.1
  - * 
  + *
    * Copyright (c) 2003 The Apache Software Foundation. All rights reserved.
  - * 
  + *
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions are met: 1.
    * Redistributions of source code must retain the above copyright notice, this
  @@ -24,7 +24,7 @@
    * Products derived from this software may not be called "Apache", "Apache
    * Maven", nor may "Apache" appear in their name, without prior written
    * permission of the Apache Software Foundation.
  - * 
  + *
    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
    * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  @@ -36,11 +36,11 @@
    * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    * ====================================================================
  - * 
  + *
    * This software consists of voluntary contributions made by many individuals
    * on behalf of the Apache Software Foundation. For more information on the
    * Apache Software Foundation, please see <http://www.apache.org/> .
  - * 
  + *
    * ====================================================================
    */
   
  @@ -49,72 +49,43 @@
   import java.io.OutputStream;
   
   /**
  - * Implementation of {@link org.apache.maven.wagon.result.WagonResult}
  - * which send transferd data to the memomry buffer
  - * 
  + * Implementation of WagonResult which send transferd data to the memomry
  + * buffer
  + *
    * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
  + * @author Peter Donald
    * @version $Id$
    */
  -public class ByteArrayResult implements WagonResult
  +public class ByteArrayResult
  +    extends StreamResult
   {
  -    /** the output stream to which trasfered data will be written */
  -    private ByteArrayOutputStream outputStream = null;
  -
  -    /** Flag indicating that this Result object was closed */
  -    private boolean closed = false;
  -
  -    /**
  -     * @see WagonResult#getOutputStream()
  -     */
  -    public synchronized OutputStream getOutputStream() throws IOException
  -    {
  -        if ( closed )
  -        {
  -            return null;
  -        }
  -
  -        if ( outputStream == null )
  -        {
  -            outputStream = new ByteArrayOutputStream();
  -        }
  -
  -        return outputStream;
  -    }
  -
       /**
  -     * @return 
  +     * Create instance of result.
        */
  -    public byte[] getByteArray()
  +    public ByteArrayResult()
       {
  -
  -        return outputStream.toByteArray();
  +        super( null, true );
       }
   
  -
       /**
  -     * @see org.apache.maven.wagon.result.WagonResult#close()
  +     * Create new ByteArrayOutputStream.
  +     *
  +     * @return the ByteArrayOutputStream
        */
  -    public synchronized void close()
  +    protected OutputStream createOutputStream()
       {
  -        //Closing a ByteArrayOutputStream has no effect
  -        try
  -        {
  -            outputStream.close();
  -        }
  -        catch ( Exception e )
  -        {
  -
  -        }
  -        closed = true;
  -
  +        return new ByteArrayOutputStream();
       }
   
       /**
  -     * @see org.apache.maven.wagon.result.WagonResult#isClosed()
  +     * Return the byte array for result.
  +     *
  +     * @return the byte array for result.
        */
  -    public boolean isClosed()
  +    public byte[] getByteArray()
  +        throws IOException
       {
  -        return closed;
  +        final ByteArrayOutputStream stream = (ByteArrayOutputStream)getOutputStream();
  +        return stream.toByteArray();
       }
  -
   }
  
  
  
  1.9       +37 -73    maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/result/FileResult.java
  
  Index: FileResult.java
  ===================================================================
  RCS file: /home/cvs/maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/result/FileResult.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- FileResult.java	2 Dec 2003 18:01:29 -0000	1.8
  +++ FileResult.java	6 Dec 2003 05:35:47 -0000	1.9
  @@ -3,9 +3,9 @@
   /*
    * ====================================================================
    * The Apache Software License, Version 1.1
  - * 
  + *
    * Copyright (c) 2003 The Apache Software Foundation. All rights reserved.
  - * 
  + *
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions are met: 1.
    * Redistributions of source code must retain the above copyright notice, this
  @@ -24,7 +24,7 @@
    * Products derived from this software may not be called "Apache", "Apache
    * Maven", nor may "Apache" appear in their name, without prior written
    * permission of the Apache Software Foundation.
  - * 
  + *
    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
    * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  @@ -36,60 +36,63 @@
    * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    * ====================================================================
  - * 
  + *
    * This software consists of voluntary contributions made by many individuals
    * on behalf of the Apache Software Foundation. For more information on the
    * Apache Software Foundation, please see <http://www.apache.org/> .
  - * 
  + *
    * ====================================================================
    */
   
  -import java.io.*;
  +import java.io.BufferedOutputStream;
  +import java.io.File;
  +import java.io.FileOutputStream;
  +import java.io.IOException;
  +import java.io.OutputStream;
   
   /**
  - * Implementation of {@link org.apache.maven.wagon.result.WagonResult}
  - * which sends transferd data to the file
  - * 
  + * Implementation of WagonResult which sends transferd data to the file.
  + *
    * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
  + * @author Peter Donald
    * @version $Id$
    */
  -public class FileResult implements WagonResult
  +public class FileResult
  +    extends StreamResult
   {
  -
       /** The file name in local file system */
       private File file;
   
  -    /** The output stream associated with the file */
  -    private OutputStream outputStream;
  -
  -    /** Flag indicating that this Result object was closed */
  -    private boolean closed = false;
  -
  -
       /**
        * Constructor
  -     * 
  -     * @param filename The name of the file to which transfered data will be directed
  +     *
  +     * @param filename The name of the file to which transfered data will be
  +     * directed
        */
       public FileResult( final String filename )
       {
  -        this.file = new File( filename );
  +        this( new File( filename ) );
       }
   
       /**
        * Constructor
  -     * 
  -     * @param file The file to which transfered data will be directed
  +     *
  +     * @param file The file to which transfered data will be directed.
        */
       public FileResult( final File file )
       {
  +        super( null, true );
  +        if( null == file )
  +        {
  +            throw new NullPointerException( "file" );
  +        }
           this.file = file;
       }
   
       /**
  -     * Return a file to which transfered data is directed
  -     * 
  -     * @return 
  +     * Return the file to which transfered data is directed.
  +     *
  +     * @return the file to which transfered data is directed.
        */
       public File getFile()
       {
  @@ -97,53 +100,14 @@
       }
   
       /**
  -     * @see WagonResult#getOutputStream()
  +     * Create output stream pointing at file.
  +     *
  +     * @return the output stream
  +     * @throws IOException if unable to open file
        */
  -    public synchronized OutputStream getOutputStream() throws IOException
  +    protected OutputStream createOutputStream()
  +        throws IOException
       {
  -        if ( closed )
  -        {
  -            return null;
  -        }
  -        if ( outputStream == null )
  -        {
  -            outputStream = new BufferedOutputStream( new FileOutputStream( file ) );
  -        }
  -        return outputStream;
  +        return new BufferedOutputStream( new FileOutputStream( file ) );
       }
  -
  -    /**
  -     * Closes InputStream which was associated with File
  -     * 
  -     * @see org.apache.maven.wagon.result.WagonResult#close()
  -     */
  -    public synchronized void close()
  -    {
  -        if ( closed )
  -        {
  -            return;
  -        }
  -
  -        if ( outputStream != null )
  -            try
  -            {
  -                outputStream.close();
  -            }
  -            catch ( Exception e )
  -            {
  -                //ignore
  -            }
  -        outputStream = null;
  -        closed = true;
  -    }
  -
  -    /**
  -     * @see org.apache.maven.wagon.result.WagonResult#isClosed()
  -     */
  -    public boolean isClosed()
  -    {
  -        return closed;
  -    }
  -
  -
   }
  
  
  
  1.8       +90 -31    maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/result/StreamResult.java
  
  Index: StreamResult.java
  ===================================================================
  RCS file: /home/cvs/maven-wagon/wagon-api/src/main/java/org/apache/maven/wagon/result/StreamResult.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- StreamResult.java	26 Nov 2003 20:58:19 -0000	1.7
  +++ StreamResult.java	6 Dec 2003 05:35:47 -0000	1.8
  @@ -3,9 +3,9 @@
   /*
    * ====================================================================
    * The Apache Software License, Version 1.1
  - * 
  + *
    * Copyright (c) 2003 The Apache Software Foundation. All rights reserved.
  - * 
  + *
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions are met:
    *  1. Redistributions of source code must retain the above copyright notice,
  @@ -25,7 +25,7 @@
    *  5. Products derived from this software may not be called "Apache", "Apache
    * Maven", nor may "Apache" appear in their name, without prior written
    * permission of the Apache Software Foundation.
  - * 
  + *
    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
    * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  @@ -37,76 +37,135 @@
    * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    * ====================================================================
  - * 
  + *
    * This software consists of voluntary contributions made by many individuals
    * on behalf of the Apache Software Foundation. For more information on the
    * Apache Software Foundation, please see <http://www.apache.org/> .
  - * 
  + *
    * ====================================================================
    */
   
  +import java.io.IOException;
   import java.io.OutputStream;
   
   /**
  - * Implementation of {@link org.apache.maven.wagon.result.WagonResult}
  - * which directs transferd data to output stream
  - * <br/>
  - * 
  + * Implementation of WagonResult which directs transferd data to output stream.
  + *
    * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
  + * @author Peter Donald
    * @version $Id$
    */
  -public class StreamResult implements WagonResult
  +public class StreamResult
  +    implements WagonResult
   {
  -    private OutputStream outputStream = null;
  +    /** The underlying output stream. */
  +    private OutputStream outputStream;
   
  -    /** Flag indicating that this Result object was closed */
  -    private boolean closed = false;
  +    /** The flag indicating whether close() should be called on the stream. */
  +    private boolean closeStreamOnClose;
   
  +    /** Flag indicating that this Result object was closed */
  +    private boolean closed;
   
       /**
  -     * <b>NOTE that output stream will not be closed when transfer is terminated</b>
  -     * 
  -     * @param outputStream 
  +     * <b>NOTE that output stream will not be closed when transfer is
  +     * terminated</b>
        */
       public StreamResult( final OutputStream outputStream )
       {
  +        this( outputStream, false );
  +    }
  +
  +    /**
  +     * Create StreamResult.
  +     *
  +     * @param outputStream the output stream
  +     * @param closeStreamOnClose flag to indicate whether stream will be closed
  +     * on transfer
  +     */
  +    protected StreamResult( final OutputStream outputStream,
  +                            final boolean closeStreamOnClose )
  +    {
  +        setOutputStream( outputStream, closeStreamOnClose );
  +    }
  +
  +    /**
  +     * Set the output stream.
  +     *
  +     * @param outputStream the outputStream
  +     */
  +    protected void setOutputStream( final OutputStream outputStream,
  +                                    final boolean closeStreamOnClose )
  +    {
           this.outputStream = outputStream;
  +        this.closeStreamOnClose = closeStreamOnClose;
       }
   
       /**
  -     * @see org.apache.maven.wagon.result.WagonResult#getOutputStream()(
  +     * @see WagonResult#getOutputStream()
        */
       public synchronized OutputStream getOutputStream()
  +        throws IOException
       {
  -
  +        if( isClosed() )
  +        {
  +            return null;
  +        }
  +        if( null == outputStream )
  +        {
  +            outputStream = createOutputStream();
  +        }
           return outputStream;
       }
   
       /**
  -     * @see org.apache.maven.wagon.result.WagonResult#close()
  +     * Create an instance of outputStream for Result. This method can be
  +     * overidden in subclasses.
  +     *
  +     * @return the outputStream
  +     */
  +    protected OutputStream createOutputStream()
  +        throws IOException
  +    {
  +        return null;
  +    }
  +
  +    /**
  +     * @see WagonResult#close()
        */
       public synchronized void close()
       {
           closed = true;
  -        try
  -        {
  -            outputStream.flush();
  -        }
  -        catch ( Exception e )
  +        if( null != outputStream )
           {
  -            //@todo should this be ignored?
  +            try
  +            {
  +                outputStream.flush();
  +            }
  +            catch( Exception e )
  +            {
  +                //TODO: Should this be ignored?
  +            }
  +            try
  +            {
  +                if( closeStreamOnClose )
  +                {
  +                    outputStream.close();
  +                }
  +            }
  +            catch( final IOException ioe )
  +            {
  +                //TODO: Should this be ignored?
  +            }
  +            outputStream = null;
           }
  -        outputStream = null;
  -
       }
   
       /**
  -     * @see org.apache.maven.wagon.result.WagonResult#isClosed()
  +     * @see WagonResult#isClosed()
        */
       public boolean isClosed()
       {
           return closed;
       }
  -
  -
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: wagon-cvs-unsubscribe@maven.apache.org
For additional commands, e-mail: wagon-cvs-help@maven.apache.org


Mime
View raw message