Repository: cxf Updated Branches: refs/heads/master 0652be3b0 -> a10b113d9
[CXF-5823] Update to Base64Utility to support multi-chunk encoding correctly Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/0bb4d0c5 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/0bb4d0c5 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/0bb4d0c5 Branch: refs/heads/master Commit: 0bb4d0c5605d7146e75b335316451008ebac2fcc Parents: 35b9209 Author: Sergey Beryozkin <[email protected]> Authored: Wed Jun 25 16:45:15 2014 +0100 Committer: Sergey Beryozkin <[email protected]> Committed: Wed Jun 25 16:45:15 2014 +0100 ---------------------------------------------------------------------- .../apache/cxf/common/util/Base64Utility.java | 4 ++-- .../cxf/common/util/Base64UtilityTest.java | 21 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/0bb4d0c5/core/src/main/java/org/apache/cxf/common/util/Base64Utility.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/common/util/Base64Utility.java b/core/src/main/java/org/apache/cxf/common/util/Base64Utility.java index cefd45c..c134fc8 100644 --- a/core/src/main/java/org/apache/cxf/common/util/Base64Utility.java +++ b/core/src/main/java/org/apache/cxf/common/util/Base64Utility.java @@ -232,7 +232,7 @@ public final class Base64Utility { // If not a multiple of 3 octets then a final padded 4 char // slot is needed. // - if ((l - o) % 3 == 0) { + if (l % 3 == 0) { out = new char[l / 3 * 4]; } else { out = new char[l / 3 * 4 + 4]; @@ -240,7 +240,7 @@ public final class Base64Utility { int rindex = o; int windex = 0; - int rest = l - o; + int rest = l; while (rest >= 3) { int i = ((id[rindex] & 0xff) << 16) http://git-wip-us.apache.org/repos/asf/cxf/blob/0bb4d0c5/core/src/test/java/org/apache/cxf/common/util/Base64UtilityTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/cxf/common/util/Base64UtilityTest.java b/core/src/test/java/org/apache/cxf/common/util/Base64UtilityTest.java index 86fe9a7..fc6cff1 100644 --- a/core/src/test/java/org/apache/cxf/common/util/Base64UtilityTest.java +++ b/core/src/test/java/org/apache/cxf/common/util/Base64UtilityTest.java @@ -23,6 +23,7 @@ import java.io.ByteArrayOutputStream; import java.io.StringWriter; import org.apache.cxf.helpers.IOUtils; + import org.junit.Assert; import org.junit.Test; @@ -40,6 +41,26 @@ public class Base64UtilityTest extends Assert { } @Test + public void testEncodeMulltipleChunks() throws Exception { + final String text = "The true sign of intelligence is not knowledge but imagination."; + byte[] bytes = text.getBytes("UTF-8"); + // multiple of 3 octets + assertEquals(63, bytes.length); + String s1 = new String(Base64Utility.encodeChunk(bytes, 0, bytes.length)); + + StringBuilder sb = new StringBuilder(); + int off = 0; + for (; off + 21 < bytes.length; off += 21) { + sb.append(Base64Utility.encodeChunk(bytes, off, 21)); + } + if (off < bytes.length) { + sb.append(Base64Utility.encodeChunk(bytes, off, bytes.length - off)); + } + String s2 = sb.toString(); + assertEquals(s1, s2); + } + + @Test public void testEncodeDecodeChunk() throws Exception { byte bytes[] = new byte[100]; for (int x = 0; x < bytes.length; x++) {
