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 2018/03/21 19:01:07 UTC

[cxf] 01/02: [CXF-7684] Fix problem of attachement blocks not divisible by 3

This is an automated email from the ASF dual-hosted git repository.

dkulp pushed a commit to branch 3.1.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit 4d5091241093301c2402e728c843641c8c8b344f
Author: Daniel Kulp <dk...@apache.org>
AuthorDate: Wed Mar 21 14:59:14 2018 -0400

    [CXF-7684] Fix problem of attachement blocks not divisible by 3
    
    (cherry picked from commit ca72cc5f5746e0fd620d85cc56b14f1f4df424ca)
---
 .../cxf/attachment/AttachmentSerializer.java       | 29 ++++++++++++++++++----
 1 file changed, 24 insertions(+), 5 deletions(-)

diff --git a/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java b/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java
index 66e7b28..36367fa 100644
--- a/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java
+++ b/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java
@@ -296,8 +296,9 @@ public class AttachmentSerializer {
 
     private int encodeBase64(InputStream input, OutputStream output, int bufferSize) throws IOException {
         int avail = input.available();
-        if (avail > 262144) {
-            avail = 262144;
+        if (avail > 262143) {
+            //must be divisible by 3
+            avail = 262143;
         }
         if (avail > bufferSize) {
             bufferSize = avail;
@@ -306,13 +307,31 @@ public class AttachmentSerializer {
         int n = 0;
         n = input.read(buffer);
         int total = 0;
+        int left = 0;
         while (-1 != n) {
             if (n == 0) {
                 throw new IOException("0 bytes read in violation of InputStream.read(byte[])");
             }
-            Base64Utility.encodeAndStream(buffer, 0, n, output);
-            total += n;
-            n = input.read(buffer);
+            //make sure n is divisible by 3
+            left = n % 3;
+            n -= left;
+            if (n > 0) {
+                Base64Utility.encodeAndStream(buffer, 0, n, output);
+                total += n;
+            }
+            if (left != 0) {
+                for (int x = 0; x < left; ++x) {
+                    buffer[x] = buffer[n + x];
+                }
+                n = input.read(buffer, left, buffer.length - left);
+                if (n == -1) {
+                    // we've hit the end, but still have stuff left, write it out
+                    Base64Utility.encodeAndStream(buffer, 0, left, output);
+                    total += left;
+                }
+            } else {
+                n = input.read(buffer);
+            }
         }
         return total;
     }

-- 
To stop receiving notification emails like this one, please contact
dkulp@apache.org.