You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-dev@ws.apache.org by ve...@apache.org on 2008/12/06 22:35:57 UTC

svn commit: r724042 - in /webservices/commons/trunk/modules/axiom/modules: axiom-api/src/main/java/org/apache/axiom/om/util/TextHelper.java axiom-tests/src/test/java/org/apache/axiom/om/util/TextHelperTest.java

Author: veithen
Date: Sat Dec  6 13:35:56 2008
New Revision: 724042

URL: http://svn.apache.org/viewvc?rev=724042&view=rev
Log:
Fixed multiple issues in TextHelper#toStringBuffer(InputStream, StringBuffer):
* Added a comment explaining why the byte buffer has length 1023.
* Changed the code so that the byte buffer is allocated only once, not at every iteration in the loop.
* InputStream#read is not required to fill the buffer, even if the end of the stream has not yet been reached. If the number of bytes provided by the input stream is not a multiple of 3, this causes corruption of the Base64 encoding. Fixed the code to always fill the byte buffer entirely (except if the end of stream is reached). This solved WSCOMMONS-101. 
* The code used InputStream#available in an incorrect way: available() == 0 doesn't mean that the end of the stream has been reached! Fixed the code so that the end of stream is detected correctly.

Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/TextHelper.java
    webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/TextHelperTest.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/TextHelper.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/TextHelper.java?rev=724042&r1=724041&r2=724042&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/TextHelper.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/TextHelper.java Sat Dec  6 13:35:56 2008
@@ -58,8 +58,6 @@
      * @throws IOException
      */
     public static void toStringBuffer(InputStream inStream, StringBuffer buffer) throws IOException {
-        byte[] data;
-        
         int avail = inStream.available();
         
         // The Base64 will increase the size by 1.33 + some additional 
@@ -69,15 +67,25 @@
             buffer.ensureCapacity((int) (avail* 1.35) + buffer.length());
         }
         
-        
+        // The size of the buffer must be a multiple of 3. Otherwise usage of the
+        // stateless Base64 class would produce filler characters inside the Base64
+        // encoded text.
+        byte[] data = new byte[1023];
+        boolean eos = false;
         do {
-            data = new byte[1023];
-            int len;
-            while ((len = inStream.read(data)) > 0) {
-                Base64.encode(data, 0, len, buffer);
-            }
-        } while (inStream.available() > 0);
-        return;
+            int len = 0;
+            do {
+                // Always fill the buffer entirely (unless the end of the stream has
+                // been reached); see above.
+                int read = inStream.read(data, len, data.length-len);
+                if (read == -1) {
+                    eos = true;
+                    break;
+                }
+                len += read;
+            } while (len < data.length);
+            Base64.encode(data, 0, len, buffer);
+        } while (!eos);
     }
     
     /**

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/TextHelperTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/TextHelperTest.java?rev=724042&r1=724041&r2=724042&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/TextHelperTest.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/TextHelperTest.java Sat Dec  6 13:35:56 2008
@@ -28,11 +28,13 @@
 import javax.activation.FileDataSource;
 
 import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.InputStream;
+import java.io.SequenceInputStream;
 
 /**
  * Validate TextHelper code
@@ -86,6 +88,18 @@
     }
     
     /**
+     * Regression test for WSCOMMONS-101.
+     * 
+     * @throws Exception
+     */
+    public void test_toString2() throws Exception {
+        InputStream in = new SequenceInputStream(
+                new ByteArrayInputStream("aa".getBytes()),
+                new ByteArrayInputStream("a".getBytes()));
+        assertEquals("YWFh", TextHelper.toString(in));
+    }
+    
+    /**
      * Test the InputStream -> BASE64 StringBuffer code
      * @throws Exception
      */