8004212: java.util.Base64 methods decodeArray and decodeBuffer should return the number of bytes written

To return the length instead of position

Reviewed-by: alanb
This commit is contained in:
Xueming Shen 2012-12-01 11:36:25 -08:00
parent 351dbc4683
commit e78a1eb083
2 changed files with 56 additions and 7 deletions
jdk
src/share/classes/java/util
test/java/util/Base64

@ -901,7 +901,7 @@ public class Base64 {
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 @@ public class Base64 {
}
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 @@ public class Base64 {
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 @@ public class Base64 {
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 @@ public class Base64 {
}
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 @@ public class Base64 {
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");

@ -22,7 +22,7 @@
*/
/**
* @test 4235519
* @test 4235519 8004212
* @summary tests java.util.Base64
*/
@ -106,6 +106,9 @@ public class TestBase64 {
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 @@ public class TestBase64 {
} 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 {