8004212: java.util.Base64 methods decodeArray and decodeBuffer should return the number of bytes written
authorsherman
Sat, 01 Dec 2012 11:36:25 -0800
changeset 14696 c1fd8cfb31ea
parent 14695 7fb1616f52f3
child 14697 6ed46ffc2d33
8004212: java.util.Base64 methods decodeArray and decodeBuffer should return the number of bytes written Summary: to return the length instead of position Reviewed-by: alanb
jdk/src/share/classes/java/util/Base64.java
jdk/test/java/util/Base64/TestBase64.java
--- a/jdk/src/share/classes/java/util/Base64.java	Fri Nov 30 16:33:50 2012 -0800
+++ b/jdk/src/share/classes/java/util/Base64.java	Sat Dec 01 11:36:25 2012 -0800
@@ -901,7 +901,7 @@
                     shiftto -= 6;
                     if (shiftto < 0) {
                         if (dl < dp + 3)
-                            return dp;
+                            return dp - dp0;
                         da[dp++] = (byte)(bits >> 16);
                         da[dp++] = (byte)(bits >>  8);
                         da[dp++] = (byte)(bits);
@@ -912,7 +912,7 @@
                 }
                 if (shiftto == 6) {
                     if (dl - dp < 1)
-                        return dp;
+                        return dp - dp0;
                     if (padding && (sp + 1 != sl || sa[sp++] != '='))
                         throw new IllegalArgumentException(
                             "Input buffer has wrong 4-byte ending unit");
@@ -920,7 +920,7 @@
                     mark = sp;
                 } else if (shiftto == 0) {
                     if (dl - dp < 2)
-                        return dp;
+                        return dp - dp0;
                     if (padding && sp != sl)
                         throw new IllegalArgumentException(
                             "Input buffer has wrong 4-byte ending unit");
@@ -969,7 +969,7 @@
                     shiftto -= 6;
                     if (shiftto < 0) {
                         if (dl < dp + 3)
-                            return dp;
+                            return dp - dp0;
                         dst.put(dp++, (byte)(bits >> 16));
                         dst.put(dp++, (byte)(bits >>  8));
                         dst.put(dp++, (byte)(bits));
@@ -980,7 +980,7 @@
                 }
                 if (shiftto == 6) {
                     if (dl - dp < 1)
-                        return dp;
+                        return dp - dp0;
                     if (padding && (sp + 1 != sl || src.get(sp++) != '='))
                         throw new IllegalArgumentException(
                             "Input buffer has wrong 4-byte ending unit");
@@ -988,7 +988,7 @@
                      mark = sp;
                 } else if (shiftto == 0) {
                     if (dl - dp < 2)
-                        return dp;
+                        return dp - dp0;
                     if (padding && sp != sl)
                         throw new IllegalArgumentException(
                             "Input buffer has wrong 4-byte ending unit");
--- a/jdk/test/java/util/Base64/TestBase64.java	Fri Nov 30 16:33:50 2012 -0800
+++ b/jdk/test/java/util/Base64/TestBase64.java	Sat Dec 01 11:36:25 2012 -0800
@@ -22,7 +22,7 @@
  */
 
 /**
- * @test 4235519
+ * @test 4235519 8004212
  * @summary tests java.util.Base64
  */
 
@@ -106,6 +106,9 @@
             Base64.getDecoder().decode(ByteBuffer.wrap(decoded), ByteBuffer.allocate(1024)); }});
         checkIAE(new Runnable() { public void run() {
             Base64.getDecoder().decode(ByteBuffer.wrap(decoded), ByteBuffer.allocateDirect(1024)); }});
+
+        // test return value from decode(ByteBuffer, ByteBuffer)
+        testDecBufRet();
     }
 
     private static sun.misc.BASE64Encoder sunmisc = new sun.misc.BASE64Encoder();
@@ -351,6 +354,52 @@
         } catch (IllegalArgumentException iae) {}
     }
 
+
+    private static void testDecBufRet() throws Throwable {
+        Random rnd = new java.util.Random();
+        Base64.Encoder encoder = Base64.getEncoder();
+        Base64.Decoder decoder = Base64.getDecoder();
+        //                src   pos, len  expected
+        int[][] tests = { { 6,    3,   3,   3},   // xxx xxx    -> yyyy yyyy
+                          { 6,    3,   4,   3},
+                          { 6,    3,   5,   3},
+                          { 6,    3,   6,   6},
+                          { 6,   11,   4,   3},
+                          { 6,   11,   4,   3},
+                          { 6,   11,   5,   3},
+                          { 6,   11,   6,   6},
+                          { 7,    3,   6,   6},   // xxx xxx x  -> yyyy yyyy yy==
+                          { 7,    3,   7,   7},
+                          { 7,   11,   6,   6},
+                          { 7,   11,   7,   7},
+                          { 8,    3,   6,   6},   // xxx xxx xx -> yyyy yyyy yyy=
+                          { 8,    3,   7,   6},
+                          { 8,    3,   8,   8},
+                          { 8,   13,   6,   6},
+                          { 8,   13,   7,   6},
+                          { 8,   13,   8,   8},
+
+        };
+        ByteBuffer dstBuf = ByteBuffer.allocate(100);
+        for (boolean direct : new boolean[] { false, true}) {
+            for (int[] test : tests) {
+                byte[] src = new byte[test[0]];
+                rnd.nextBytes(src);
+                ByteBuffer srcBuf = direct ? ByteBuffer.allocate(100)
+                                           : ByteBuffer.allocateDirect(100);
+                srcBuf.put(encoder.encode(src)).flip();
+                dstBuf.clear().position(test[1]).limit(test[1]+ test[2]);
+                int ret = decoder.decode(srcBuf, dstBuf);
+                if (ret != test[3]) {
+                    System.out.printf(" [%6s] src=%d, pos=%d, len=%d, expected=%d, ret=%d%n",
+                                      direct?"direct":"",
+                                      test[0], test[1], test[2], test[3], ret);
+                    throw new RuntimeException("ret != expected");
+                }
+            }
+        }
+    }
+
     private static final void testEncode(Base64.Encoder enc, ByteBuffer bin, byte[] expected)
         throws Throwable {