You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2010/10/15 21:02:43 UTC

svn commit: r1023071 - in /cxf/branches/2.2.x-fixes: ./ common/common/src/main/java/org/apache/cxf/helpers/ rt/core/src/main/java/org/apache/cxf/attachment/ rt/core/src/test/java/org/apache/cxf/attachment/

Author: dkulp
Date: Fri Oct 15 19:02:43 2010
New Revision: 1023071

URL: http://svn.apache.org/viewvc?rev=1023071&view=rev
Log:
Merged revisions 1023069 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/branches/2.3.x-fixes

................
  r1023069 | dkulp | 2010-10-15 14:55:00 -0400 (Fri, 15 Oct 2010) | 9 lines
  
  Merged revisions 1023068 via svnmerge from 
  https://svn.apache.org/repos/asf/cxf/trunk
  
  ........
    r1023068 | dkulp | 2010-10-15 14:53:40 -0400 (Fri, 15 Oct 2010) | 1 line
    
    [CXF-3068] Make sure the streams don't return 0 for anything.
  ........
................

Modified:
    cxf/branches/2.2.x-fixes/   (props changed)
    cxf/branches/2.2.x-fixes/common/common/src/main/java/org/apache/cxf/helpers/IOUtils.java
    cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/attachment/MimeBodyPartInputStream.java
    cxf/branches/2.2.x-fixes/rt/core/src/test/java/org/apache/cxf/attachment/AttachmentDeserializerTest.java

Propchange: cxf/branches/2.2.x-fixes/
            ('svn:mergeinfo' removed)

Propchange: cxf/branches/2.2.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.2.x-fixes/common/common/src/main/java/org/apache/cxf/helpers/IOUtils.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/common/common/src/main/java/org/apache/cxf/helpers/IOUtils.java?rev=1023071&r1=1023070&r2=1023071&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/common/common/src/main/java/org/apache/cxf/helpers/IOUtils.java (original)
+++ cxf/branches/2.2.x-fixes/common/common/src/main/java/org/apache/cxf/helpers/IOUtils.java Fri Oct 15 19:02:43 2010
@@ -134,6 +134,9 @@ public final class IOUtils {
         n = input.read(buffer);
         int total = 0;
         while (-1 != n) {
+            if (n == 0) {
+                throw new IOException("0 bytes read in violation of InputStream.read(byte[])");
+            }
             output.write(buffer, 0, n);
             total += n;
             n = input.read(buffer);
@@ -147,6 +150,9 @@ public final class IOUtils {
         int n = 0;
         n = input.read(buffer);
         while (-1 != n) {
+            if (n == 0) {
+                throw new IOException("0 bytes read in violation of InputStream.read(byte[])");
+            }
             output.write(buffer, 0, n);
             n = input.read(buffer);
         }
@@ -169,6 +175,9 @@ public final class IOUtils {
         int n = 0;
         n = input.read(buffer);
         while (-1 != n) {
+            if (n == 0) {
+                throw new IOException("0 bytes read in violation of InputStream.read(byte[])");
+            }
             buf.append(newStringFromBytes(buffer, 0, n));
             n = input.read(buffer);
         }
@@ -183,6 +192,9 @@ public final class IOUtils {
         int n = 0;
         n = input.read(buffer);
         while (-1 != n) {
+            if (n == 0) {
+                throw new IOException("0 bytes read in violation of InputStream.read(byte[])");
+            }
             buf.append(new String(buffer, 0, n));
             n = input.read(buffer);
         }

Modified: cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/attachment/MimeBodyPartInputStream.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/attachment/MimeBodyPartInputStream.java?rev=1023071&r1=1023070&r2=1023071&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/attachment/MimeBodyPartInputStream.java (original)
+++ cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/attachment/MimeBodyPartInputStream.java Fri Oct 15 19:02:43 2010
@@ -70,9 +70,20 @@ public class MimeBodyPartInputStream ext
         if (len > pbAmount) {
             len = pbAmount;  //can only pushback that much so make sure we can
         }
-        if (len > 0) {
-            len = inStream.read(b, off, len);
+        int read = 0;
+        int idx = 0;
+        while (read >= 0 && idx < len && idx < (boundary.length * 2)) {
+            //make sure we read enough to detect the boundary
+            read = inStream.read(b, off + idx, len - idx);
+            if (read != -1) {
+                idx += read;
+            }
+        }
+        if (read == -1 && idx == 0) {
+            return -1;
         }
+        len = idx;
+        
         int i = processBuffer(b, off, len);
         if (bufferCreated && i > 0) {
             // read more than we need, push it back
@@ -86,6 +97,7 @@ public class MimeBodyPartInputStream ext
         } else if (i == 0 && boundaryFound) {
             return -1;
         }
+        
         return i;
     }
 

Modified: cxf/branches/2.2.x-fixes/rt/core/src/test/java/org/apache/cxf/attachment/AttachmentDeserializerTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/core/src/test/java/org/apache/cxf/attachment/AttachmentDeserializerTest.java?rev=1023071&r1=1023070&r2=1023071&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/core/src/test/java/org/apache/cxf/attachment/AttachmentDeserializerTest.java (original)
+++ cxf/branches/2.2.x-fixes/rt/core/src/test/java/org/apache/cxf/attachment/AttachmentDeserializerTest.java Fri Oct 15 19:02:43 2010
@@ -20,6 +20,7 @@ package org.apache.cxf.attachment;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.PushbackInputStream;
 import java.util.Collection;
@@ -396,5 +397,64 @@ public class AttachmentDeserializerTest 
         message.getAttachments().size();
 
     }
+    @Test
+    public void testDoesntReturnZero() throws Exception {
+        String contentType = "multipart/mixed;boundary=----=_Part_1";
+        byte[] messageBytes = (
+                  "------=_Part_1\n\n"
+                + "JJJJ\n"
+                + "------=_Part_1"
+                + "\n\nContent-Transfer-Encoding: binary\n\n"
+                + "ABCD1\r\n"
+                + "------=_Part_1"
+                + "\n\nContent-Transfer-Encoding: binary\n\n"
+                + "ABCD2\r\n"
+                + "------=_Part_1"
+                + "\n\nContent-Transfer-Encoding: binary\n\n"
+                + "ABCD3\r\n"
+                + "------=_Part_1--").getBytes("UTF-8");
+        ByteArrayInputStream in = new ByteArrayInputStream(messageBytes) {
+            public int read(byte[] b, int off, int len) {
+                return super.read(b, off, len >= 2 ? 2 : len); 
+            }
+        };
+        
+        Message message = new MessageImpl();
+        message.put(Message.CONTENT_TYPE, contentType);
+        message.setContent(InputStream.class, in);
+        message.put(AttachmentDeserializer.ATTACHMENT_DIRECTORY, System
+                .getProperty("java.io.tmpdir"));
+        message.put(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD, String
+                .valueOf(AttachmentDeserializer.THRESHOLD));
+
+
+        AttachmentDeserializer ad 
+            = new AttachmentDeserializer(message, 
+                                         Collections.singletonList("multipart/mixed"));
+
+        ad.initializeAttachments();
+        
+        String s = getString(message.getContent(InputStream.class));
+        assertEquals("JJJJ", s.trim());
+        int count = 1;
+        for (Attachment a : message.getAttachments()) {
+            s = getString(a.getDataHandler().getInputStream());
+            assertEquals("ABCD" + count++, s);
+        }
+    }
+    
+    private String getString(InputStream ins) throws Exception {
+        ByteArrayOutputStream bout = new ByteArrayOutputStream(100);
+        byte b[] = new byte[100];
+        int i = ins.read(b);
+        while (i > 0) {
+            bout.write(b, 0 , i);
+            i = ins.read(b);
+        }
+        if (i == 0) {
+            throw new IOException("Should not be 0");
+        }
+        return bout.toString();
+    }
 
 }
\ No newline at end of file