提交 38221a87 编写于 作者: S sherman

8004212: java.util.Base64 methods decodeArray and decodeBuffer should return...

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
上级 5fc74c7a
...@@ -901,7 +901,7 @@ public class Base64 { ...@@ -901,7 +901,7 @@ public class Base64 {
shiftto -= 6; shiftto -= 6;
if (shiftto < 0) { if (shiftto < 0) {
if (dl < dp + 3) if (dl < dp + 3)
return dp; return dp - dp0;
da[dp++] = (byte)(bits >> 16); da[dp++] = (byte)(bits >> 16);
da[dp++] = (byte)(bits >> 8); da[dp++] = (byte)(bits >> 8);
da[dp++] = (byte)(bits); da[dp++] = (byte)(bits);
...@@ -912,7 +912,7 @@ public class Base64 { ...@@ -912,7 +912,7 @@ public class Base64 {
} }
if (shiftto == 6) { if (shiftto == 6) {
if (dl - dp < 1) if (dl - dp < 1)
return dp; return dp - dp0;
if (padding && (sp + 1 != sl || sa[sp++] != '=')) if (padding && (sp + 1 != sl || sa[sp++] != '='))
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Input buffer has wrong 4-byte ending unit"); "Input buffer has wrong 4-byte ending unit");
...@@ -920,7 +920,7 @@ public class Base64 { ...@@ -920,7 +920,7 @@ public class Base64 {
mark = sp; mark = sp;
} else if (shiftto == 0) { } else if (shiftto == 0) {
if (dl - dp < 2) if (dl - dp < 2)
return dp; return dp - dp0;
if (padding && sp != sl) if (padding && sp != sl)
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Input buffer has wrong 4-byte ending unit"); "Input buffer has wrong 4-byte ending unit");
...@@ -969,7 +969,7 @@ public class Base64 { ...@@ -969,7 +969,7 @@ public class Base64 {
shiftto -= 6; shiftto -= 6;
if (shiftto < 0) { if (shiftto < 0) {
if (dl < dp + 3) if (dl < dp + 3)
return dp; return dp - dp0;
dst.put(dp++, (byte)(bits >> 16)); dst.put(dp++, (byte)(bits >> 16));
dst.put(dp++, (byte)(bits >> 8)); dst.put(dp++, (byte)(bits >> 8));
dst.put(dp++, (byte)(bits)); dst.put(dp++, (byte)(bits));
...@@ -980,7 +980,7 @@ public class Base64 { ...@@ -980,7 +980,7 @@ public class Base64 {
} }
if (shiftto == 6) { if (shiftto == 6) {
if (dl - dp < 1) if (dl - dp < 1)
return dp; return dp - dp0;
if (padding && (sp + 1 != sl || src.get(sp++) != '=')) if (padding && (sp + 1 != sl || src.get(sp++) != '='))
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Input buffer has wrong 4-byte ending unit"); "Input buffer has wrong 4-byte ending unit");
...@@ -988,7 +988,7 @@ public class Base64 { ...@@ -988,7 +988,7 @@ public class Base64 {
mark = sp; mark = sp;
} else if (shiftto == 0) { } else if (shiftto == 0) {
if (dl - dp < 2) if (dl - dp < 2)
return dp; return dp - dp0;
if (padding && sp != sl) if (padding && sp != sl)
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Input buffer has wrong 4-byte ending unit"); "Input buffer has wrong 4-byte ending unit");
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
*/ */
/** /**
* @test 4235519 * @test 4235519 8004212
* @summary tests java.util.Base64 * @summary tests java.util.Base64
*/ */
...@@ -106,6 +106,9 @@ public class TestBase64 { ...@@ -106,6 +106,9 @@ public class TestBase64 {
Base64.getDecoder().decode(ByteBuffer.wrap(decoded), ByteBuffer.allocate(1024)); }}); Base64.getDecoder().decode(ByteBuffer.wrap(decoded), ByteBuffer.allocate(1024)); }});
checkIAE(new Runnable() { public void run() { checkIAE(new Runnable() { public void run() {
Base64.getDecoder().decode(ByteBuffer.wrap(decoded), ByteBuffer.allocateDirect(1024)); }}); 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(); private static sun.misc.BASE64Encoder sunmisc = new sun.misc.BASE64Encoder();
...@@ -351,6 +354,52 @@ public class TestBase64 { ...@@ -351,6 +354,52 @@ public class TestBase64 {
} catch (IllegalArgumentException iae) {} } 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) private static final void testEncode(Base64.Encoder enc, ByteBuffer bin, byte[] expected)
throws Throwable { throws Throwable {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册