cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jo...@apache.org
Subject svn commit: r638211 - in /cocoon/trunk/core/cocoon-core/src: changes/ main/java/org/apache/cocoon/servlet/multipart/
Date Tue, 18 Mar 2008 04:41:29 GMT
Author: joerg
Date: Mon Mar 17 21:41:23 2008
New Revision: 638211

URL: http://svn.apache.org/viewvc?rev=638211&view=rev
Log:
Close streams properly after copying Parts (MultipartParser). Allow to access InputStream
of PartInMemory multiple times.

Modified:
    cocoon/trunk/core/cocoon-core/src/changes/changes.xml
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/multipart/MultipartParser.java
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/multipart/Part.java
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/multipart/PartInMemory.java

Modified: cocoon/trunk/core/cocoon-core/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/changes/changes.xml?rev=638211&r1=638210&r2=638211&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/changes/changes.xml (original)
+++ cocoon/trunk/core/cocoon-core/src/changes/changes.xml Mon Mar 17 21:41:23 2008
@@ -29,6 +29,10 @@
 
   <body>
     <release version="2.2.0" date="2007-??-??" description="unreleased">
+      <action dev="joerg" type="fix">
+        Close streams properly after copying Parts (MultipartParser). Allow to access InputStream
of PartInMemory
+        multiple times.
+      </action>
       <action dev="joerg" type="add" fixes-bug="COCOON-2149" due-to="Ellis Pritchard"
due-to-email="ellis@nukinetics.com">
         Add capability to IncludeTransformer to strip the root element from included XML
content.
       </action>

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/multipart/MultipartParser.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/multipart/MultipartParser.java?rev=638211&r1=638210&r2=638211&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/multipart/MultipartParser.java
(original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/multipart/MultipartParser.java
Mon Mar 17 21:41:23 2008
@@ -17,7 +17,6 @@
 package org.apache.cocoon.servlet.multipart;
 
 import java.io.BufferedInputStream;
-import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
@@ -115,7 +114,6 @@
         TokenStream stream = new TokenStream(pushbackStream);
 
         parseMultiPart(stream, getBoundary(contentType));
-
     }
 
     public Hashtable getParts(int contentLength, String contentType, InputStream requestStream)
@@ -252,7 +250,6 @@
         byte[] buf = new byte[FILE_BUFFER_SIZE];
         OutputStream out;
         File file = null;
-        boolean canceled = false;
 
         if (oversized) {
             out = new NullOutputStream();
@@ -327,11 +324,11 @@
         }
 
         String name = (String)headers.get("name");
-        if (oversized || canceled) {
+        if (oversized) {
             this.parts.put(name, new RejectedPart(headers, length, this.contentLength, this.maxUploadSize));
         } else if (file == null) {
             byte[] bytes = ((ByteArrayOutputStream) out).toByteArray();
-            this.parts.put(name, new PartInMemory(headers, new ByteArrayInputStream(bytes),
bytes.length));
+            this.parts.put(name, new PartInMemory(headers, bytes));
         } else {
             this.parts.put(name, new PartOnDisk(headers, file));
         }
@@ -401,9 +398,6 @@
 
     /**
      * Get boundary from contentheader
-     *
-     * @param hdr
-     *
      */
     private String getBoundary(String hdr) {
 
@@ -438,4 +432,5 @@
 
         return new String(bos.toByteArray(), this.characterEncoding);
     }
+
 }

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/multipart/Part.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/multipart/Part.java?rev=638211&r1=638210&r2=638211&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/multipart/Part.java
(original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/multipart/Part.java
Mon Mar 17 21:41:23 2008
@@ -124,10 +124,16 @@
      */
     public void copyToSource(ModifiableSource source) throws IOException {
         InputStream is = getInputStream();
-        OutputStream os = source.getOutputStream();
-        IOUtils.copy(is, os);
-        is.close();
-        os.close();
+        try {
+            OutputStream os = source.getOutputStream();
+            try {
+                IOUtils.copy(is, os);
+            } finally {
+                os.close();
+            }
+        } finally {
+            is.close();
+        }
     }
     
     /**
@@ -139,10 +145,16 @@
      */
     public void copyToFile(String filename) throws IOException {
         InputStream is = getInputStream();
-        OutputStream os = new FileOutputStream(filename);
-        IOUtils.copy(is, os);
-        is.close();
-        os.close();
+        try {
+            OutputStream os = new FileOutputStream(filename);
+            try {
+                IOUtils.copy(is, os);
+            } finally {
+                os.close();
+            }
+        } finally {
+            is.close();
+        }
     }
     
     /**

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/multipart/PartInMemory.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/multipart/PartInMemory.java?rev=638211&r1=638210&r2=638211&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/multipart/PartInMemory.java
(original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/servlet/multipart/PartInMemory.java
Mon Mar 17 21:41:23 2008
@@ -16,6 +16,7 @@
  */
 package org.apache.cocoon.servlet.multipart;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Map;
@@ -27,21 +28,16 @@
  */
 public class PartInMemory extends Part {
 
-    private InputStream in;
-
+    private byte[] bytes;
     private int size;
 
     /**
      * Constructor PartInMemory
-     *
-     * @param headers
-     * @param in
-     * @param size
      */
-    public PartInMemory(Map headers, InputStream in, int size) {
+    public PartInMemory(Map headers, byte[] bytes) {
         super(headers);
-        this.in = in;
-        this.size = size;
+        this.bytes = bytes;
+        this.size = bytes.length;
     }
 
     /**
@@ -64,17 +60,18 @@
      * @throws IOException
      */
     public InputStream getInputStream() throws IOException {
-        if (this.in != null) {
-            return this.in;
+        if (this.bytes != null) {
+            return new ByteArrayInputStream(this.bytes);
         } else {
             throw new IllegalStateException("This part has already been disposed.");
         }
     }
-    
+
     /**
      * Clean the byte array content buffer holding part data
      */
     public void dispose() {
-        this.in = null;
+        this.bytes = null;
     }
+
 }



Mime
View raw message