You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@excalibur.apache.org by le...@apache.org on 2004/09/28 05:04:22 UTC

svn commit: rev 47375 - in excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source: . impl

Author: leif
Date: Mon Sep 27 20:04:22 2004
New Revision: 47375

Modified:
   excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/ModifiableSource.java
   excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/Source.java
   excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/SourceUtil.java
   excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/AbstractSource.java
   excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/FTPSource.java
   excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/FileSource.java
   excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/HTTPClientSource.java
   excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/ResourceSource.java
   excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/URLSource.java
Log:
Fix a problem where the streams opened by SourceUtil.copy were not being closed.  Reported by Vadim Gritsenko.
Also added javadocs describing when streams need to be closed and the fact that calling cancel will close a stream.

Modified: excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/ModifiableSource.java
==============================================================================
--- excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/ModifiableSource.java	(original)
+++ excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/ModifiableSource.java	Mon Sep 27 20:04:22 2004
@@ -38,6 +38,8 @@
 {
     /**
      * Return an {@link OutputStream} to write to.
+     *
+     * The returned stream must be closed or cancelled by the calling code.
      */
     OutputStream getOutputStream() throws IOException;
     
@@ -56,7 +58,7 @@
 
     /**
      * Cancel the data sent to an <code>OutputStream</code> returned by
-     * {@link #getOutputStream()}.
+     * {@link #getOutputStream()}.  Cancelling the stream will also close it.
      *
      * <p>After cancelling, the stream should no longer be used.</p>
      */

Modified: excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/Source.java
==============================================================================
--- excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/Source.java	(original)
+++ excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/Source.java	Mon Sep 27 20:04:22 2004
@@ -74,6 +74,8 @@
      * This is the data at the point of invocation of this method,
      * so if this is Modifiable, you might get different content
      * from two different invocations.
+     *
+     * The returned stream must be closed by the calling code.
      * 
      * @return the <code>InputStream</code> to read data from (never <code>null</code>).
      * @throws IOException if some I/O problem occurs.

Modified: excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/SourceUtil.java
==============================================================================
--- excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/SourceUtil.java	(original)
+++ excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/SourceUtil.java	Mon Sep 27 20:04:22 2004
@@ -16,7 +16,13 @@
  */
 package org.apache.excalibur.source;
 
-import java.io.*;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.UnsupportedEncodingException;
 import java.util.BitSet;
 import java.util.Iterator;
 
@@ -479,7 +485,8 @@
      */
     static public void copy(Source source,
                             Source destination)
-    throws SourceException {
+        throws SourceException
+    {
         if (source instanceof MoveableSource
             && source.getClass().equals(destination.getClass()))
         {
@@ -492,12 +499,45 @@
                                           destination.getURI()+
                                           "' is not writeable");
             }
-
-            try {
-                OutputStream out = ((ModifiableSource) destination).getOutputStream();
+            
+            ModifiableSource modDestination = (ModifiableSource)destination;
+            try
+            {
                 InputStream in = source.getInputStream();
-
-                copy(in, out);
+                try
+                {
+                    OutputStream out = modDestination.getOutputStream();
+                    try
+                    {
+                        try
+                        {
+                            copy(in, out);
+                        }
+                        catch ( IOException e )
+                        {
+                            if ( modDestination.canCancel( out ) )
+                            {
+                                modDestination.cancel( out );
+                                out = null;
+                            }
+                            
+                            // Rethrow the exception.  It will be recaught below.
+                            throw e;
+                        }
+                    }
+                    finally
+                    {
+                        // out may have already been closed if there was a problem.
+                        if ( out != null )
+                        {
+                            out.close();
+                        }
+                    }
+                }
+                finally
+                {
+                    in.close();
+                }
             } catch (IOException ioe) {
                 throw new SourceException("Could not copy source '"+
                                           source.getURI()+"' to '"+

Modified: excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/AbstractSource.java
==============================================================================
--- excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/AbstractSource.java	(original)
+++ excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/AbstractSource.java	Mon Sep 27 20:04:22 2004
@@ -63,6 +63,8 @@
     /**
      * Return an <code>InputStream</code> object to read from the source.
      *
+     * The returned stream must be closed by the calling code.
+     *
      * @throws SourceException if file not found or
      *         HTTP location does not exist.
      * @throws IOException if I/O error occured.

Modified: excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/FTPSource.java
==============================================================================
--- excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/FTPSource.java	(original)
+++ excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/FTPSource.java	Mon Sep 27 20:04:22 2004
@@ -40,7 +40,7 @@
 public class FTPSource extends URLSource implements ModifiableSource
 {   
     private boolean m_isAscii;
-	
+    
     public FTPSource()
     {
         super();
@@ -189,6 +189,8 @@
 
     /**
      * Return an {@link OutputStream} to write to.
+     *
+     * The returned stream must be closed or cancelled by the calling code.
      */
     public OutputStream getOutputStream() throws IOException
     {

Modified: excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/FileSource.java
==============================================================================
--- excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/FileSource.java	(original)
+++ excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/FileSource.java	Mon Sep 27 20:04:22 2004
@@ -299,6 +299,8 @@
      * simultaneous writes. The real file is updated atomically when the output
      * stream is closed.
      *
+     * The returned stream must be closed or cancelled by the calling code.
+     *
      * @return a stream to write to
      * @throws ConcurrentModificationException if another thread is currently
      *         writing to this file.

Modified: excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/HTTPClientSource.java
==============================================================================
--- excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/HTTPClientSource.java	(original)
+++ excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/HTTPClientSource.java	Mon Sep 27 20:04:22 2004
@@ -667,6 +667,8 @@
      * returned actually references a temporary local file, which will
      * be written to the server upon closing.
      *
+     * The returned stream must be closed or cancelled by the calling code.
+     *
      * @return an {@link OutputStream} instance
      * @exception IOException if an error occurs
      */

Modified: excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/ResourceSource.java
==============================================================================
--- excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/ResourceSource.java	(original)
+++ excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/ResourceSource.java	Mon Sep 27 20:04:22 2004
@@ -89,6 +89,8 @@
 
     /**
      * Return an <code>InputStream</code> object to read from the source.
+     *
+     * The returned stream must be closed by the calling code.
      */
     public InputStream getInputStream()
         throws IOException, SourceException

Modified: excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/URLSource.java
==============================================================================
--- excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/URLSource.java	(original)
+++ excalibur/trunk/components/sourceresolve/src/java/org/apache/excalibur/source/impl/URLSource.java	Mon Sep 27 20:04:22 2004
@@ -186,6 +186,8 @@
     /**
      * Return an <code>InputStream</code> object to read from the source.
      *
+     * The returned stream must be closed by the calling code.
+     *
      * @throws SourceException if file not found or
      *         HTTP location does not exist.
      * @throws IOException if I/O error occured.
@@ -302,19 +304,19 @@
      */
     protected String getUserInfo() 
     {
-	    if (m_url == null) return null;
-	    String ui = m_url.getUserInfo();
-	    if (ui == null) return null;
-	
-	    try 
+        if (m_url == null) return null;
+        String ui = m_url.getUserInfo();
+        if (ui == null) return null;
+    
+        try 
         {
-	        ui = URLDecoder.decode(ui,"UTF-8");
-	    } 
+         ui = URLDecoder.decode(ui,"UTF-8");
+        } 
         catch (UnsupportedEncodingException e)
         {
-	        // Platform does not support UTF-8. This should never happen.
-	        // e.printStackTrace();
-	    }
-	    return ui;
+         // Platform does not support UTF-8. This should never happen.
+         // e.printStackTrace();
+        }
+        return ui;
     }
 }

---------------------------------------------------------------------
To unsubscribe, e-mail: scm-unsubscribe@excalibur.apache.org
For additional commands, e-mail: scm-help@excalibur.apache.org