You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2016/03/26 17:28:50 UTC

svn commit: r1736698 - in /webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments: CountingInputStream.java MIMEMessage.java PartImpl.java PartInputStream.java ReadOnceInputStreamWrapper.java

Author: veithen
Date: Sat Mar 26 16:28:50 2016
New Revision: 1736698

URL: http://svn.apache.org/viewvc?rev=1736698&view=rev
Log:
Avoid using DetachableInputStream and instead always use the blob factory specified for the part.

Added:
    webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/CountingInputStream.java   (with props)
    webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartInputStream.java
      - copied, changed from r1734616, webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/ReadOnceInputStreamWrapper.java
Removed:
    webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/ReadOnceInputStreamWrapper.java
Modified:
    webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/MIMEMessage.java
    webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartImpl.java

Added: webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/CountingInputStream.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/CountingInputStream.java?rev=1736698&view=auto
==============================================================================
--- webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/CountingInputStream.java (added)
+++ webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/CountingInputStream.java Sat Mar 26 16:28:50 2016
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axiom.attachments;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+final class CountingInputStream extends InputStream {
+    private final InputStream parent;
+    private long count;
+
+    CountingInputStream(InputStream parent) {
+        this.parent = parent;
+    }
+
+    long getCount() {
+        return count;
+    }
+
+    public int read() throws IOException {
+        int b = parent.read();
+        if (b != -1) {
+            count++;
+        }
+        return b;
+    }
+
+    public int read(byte[] b) throws IOException {
+        int read = parent.read(b);
+        if (read != -1) {
+            count += read;
+        }
+        return read;
+    }
+
+    public int read(byte[] b, int off, int len) throws IOException {
+        int read = parent.read(b, off, len);
+        if (read != -1) {
+            count += read;
+        }
+        return read;
+    }
+
+    public long skip(long n) throws IOException {
+        long skipped = parent.skip(n);
+        count += skipped;
+        return skipped;
+    }
+
+    public int available() throws IOException {
+        return parent.available();
+    }
+
+    public void close() throws IOException {
+        parent.close();
+    }
+}

Propchange: webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/CountingInputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/MIMEMessage.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/MIMEMessage.java?rev=1736698&r1=1736697&r2=1736698&view=diff
==============================================================================
--- webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/MIMEMessage.java (original)
+++ webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/MIMEMessage.java Sat Mar 26 16:28:50 2016
@@ -37,7 +37,6 @@ import org.apache.axiom.blob.WritableBlo
 import org.apache.axiom.mime.ContentType;
 import org.apache.axiom.mime.Header;
 import org.apache.axiom.om.OMException;
-import org.apache.axiom.om.util.DetachableInputStream;
 import org.apache.axiom.util.UIDGenerator;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -66,7 +65,7 @@ class MIMEMessage extends AttachmentsDel
     
     private final int contentLength; // Content Length
 
-    private final DetachableInputStream filterIS;
+    private final CountingInputStream filterIS;
 
     private final MimeTokenStream parser;
     
@@ -116,7 +115,7 @@ class MIMEMessage extends AttachmentsDel
         // so that we can retrieve it later.
         InputStream is = inStream;
         if (contentLength <= 0) {
-            filterIS = new DetachableInputStream(inStream);
+            filterIS = new CountingInputStream(inStream);
             is = filterIS;
         } else {
             filterIS = null;
@@ -277,7 +276,7 @@ class MIMEMessage extends AttachmentsDel
             // Ensure all parts are read
             fetchAllParts();
             // Now get the count from the filter
-            return filterIS.length();
+            return filterIS.getCount();
         }
     }
     

Modified: webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartImpl.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartImpl.java?rev=1736698&r1=1736697&r2=1736698&view=diff
==============================================================================
--- webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartImpl.java (original)
+++ webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartImpl.java Sat Mar 26 16:28:50 2016
@@ -20,14 +20,12 @@
 package org.apache.axiom.attachments;
 
 import org.apache.axiom.attachments.Part;
-import org.apache.axiom.blob.MemoryBlob;
 import org.apache.axiom.blob.OverflowableBlob;
 import org.apache.axiom.blob.WritableBlob;
 import org.apache.axiom.blob.WritableBlobFactory;
 import org.apache.axiom.ext.io.StreamCopyException;
 import org.apache.axiom.mime.Header;
 import org.apache.axiom.om.OMException;
-import org.apache.axiom.om.util.DetachableInputStream;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.james.mime4j.MimeException;
@@ -90,7 +88,7 @@ final class PartImpl implements Part {
     
     private final DataHandler dataHandler;
     
-    private DetachableInputStream detachableInputStream;
+    private PartInputStream partInputStream;
     
     /**
      * The actual parts are constructed with the PartFactory.
@@ -209,11 +207,11 @@ final class PartImpl implements Part {
             case STATE_STREAMING:
                 // If the stream is still open, buffer the remaining content
                 try {
-                    detachableInputStream.detach();
+                    partInputStream.detach();
                 } catch (IOException ex) {
                     throw new OMException(ex);
                 }
-                detachableInputStream = null;
+                partInputStream = null;
                 moveToNextPart();
                 state = STATE_DISCARDED;
         }
@@ -242,16 +240,14 @@ final class PartImpl implements Part {
         if (!preserve && state == STATE_UNREAD) {
             checkParserState(parser.getState(), EntityState.T_BODY);
             state = STATE_STREAMING;
-            detachableInputStream = new DetachableInputStream(getDecodedInputStream());
-            return detachableInputStream;
+            partInputStream = new PartInputStream(getDecodedInputStream(), blobFactory);
+            return partInputStream;
         } else {
             WritableBlob content = getContent();
             if (preserve) {
                 return content.getInputStream();
-            } else if (content instanceof MemoryBlob) {
-                return ((MemoryBlob)content).readOnce();
             } else {
-                return new ReadOnceInputStreamWrapper(this, content.getInputStream());
+                return new PartInputStream(content);
             }
         }
     }

Copied: webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartInputStream.java (from r1734616, webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/ReadOnceInputStreamWrapper.java)
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartInputStream.java?p2=webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartInputStream.java&p1=webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/ReadOnceInputStreamWrapper.java&r1=1734616&r2=1736698&rev=1736698&view=diff
==============================================================================
--- webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/ReadOnceInputStreamWrapper.java (original)
+++ webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/PartInputStream.java Sat Mar 26 16:28:50 2016
@@ -21,17 +21,44 @@ package org.apache.axiom.attachments;
 import java.io.IOException;
 import java.io.InputStream;
 
-/**
- * Input stream wrapper that automatically calls {@link PartImpl#releaseContent()} when the content
- * has been consumed.
- */
-class ReadOnceInputStreamWrapper extends InputStream {
-    private final PartImpl part;
+import org.apache.axiom.blob.MemoryBlob;
+import org.apache.axiom.blob.WritableBlob;
+import org.apache.axiom.blob.WritableBlobFactory;
+
+final class PartInputStream extends InputStream {
+    private WritableBlob content;
     private InputStream in;
+    private WritableBlobFactory blobFactory;
+    
+    PartInputStream(WritableBlob content) throws IOException {
+        this.content = content;
+        in = getInputStream(content);
+    }
     
-    ReadOnceInputStreamWrapper(PartImpl part, InputStream in) {
-        this.part = part;
+    PartInputStream(InputStream in, WritableBlobFactory blobFactory) {
         this.in = in;
+        this.blobFactory = blobFactory;
+    }
+    
+    private static InputStream getInputStream(WritableBlob content) throws IOException {
+        if (content instanceof MemoryBlob) {
+            return ((MemoryBlob)content).readOnce();
+        } else {
+            return content.getInputStream();
+        }
+    }
+    
+    void detach() throws IOException {
+        if (blobFactory == null) {
+            throw new IllegalStateException();
+        }
+        if (in != null) {
+            WritableBlob content = blobFactory.createBlob();
+            content.readFrom(in);
+            this.content = content;
+            in = getInputStream(content);
+        }
+        blobFactory = null;
     }
     
     public int available() throws IOException {
@@ -81,8 +108,11 @@ class ReadOnceInputStreamWrapper extends
     public void close() throws IOException {
         if (in != null) {
             in.close();
-            part.releaseContent();
             in = null;
         }
+        if (content != null) {
+            content.release();
+            content = null;
+        }
     }
 }