提交 d5eb9e71 编写于 作者: S sherman

8026330: java.util.Base64 urlEncoder should omit padding

Summary: to add Encoder.withoutPadding()
Reviewed-by: alanb
上级 01f0a918
...@@ -138,7 +138,7 @@ public class Base64 { ...@@ -138,7 +138,7 @@ public class Base64 {
if (lineLength <= 0) { if (lineLength <= 0) {
return Encoder.RFC4648; return Encoder.RFC4648;
} }
return new Encoder(false, lineSeparator, lineLength >> 2 << 2); return new Encoder(false, lineSeparator, lineLength >> 2 << 2, true);
} }
/** /**
...@@ -192,11 +192,13 @@ public class Base64 { ...@@ -192,11 +192,13 @@ public class Base64 {
private final byte[] newline; private final byte[] newline;
private final int linemax; private final int linemax;
private final boolean isURL; private final boolean isURL;
private final boolean doPadding;
private Encoder(boolean isURL, byte[] newline, int linemax) { private Encoder(boolean isURL, byte[] newline, int linemax, boolean doPadding) {
this.isURL = isURL; this.isURL = isURL;
this.newline = newline; this.newline = newline;
this.linemax = linemax; this.linemax = linemax;
this.doPadding = doPadding;
} }
/** /**
...@@ -228,9 +230,22 @@ public class Base64 { ...@@ -228,9 +230,22 @@ public class Base64 {
private static final int MIMELINEMAX = 76; private static final int MIMELINEMAX = 76;
private static final byte[] CRLF = new byte[] {'\r', '\n'}; private static final byte[] CRLF = new byte[] {'\r', '\n'};
static final Encoder RFC4648 = new Encoder(false, null, -1); static final Encoder RFC4648 = new Encoder(false, null, -1, true);
static final Encoder RFC4648_URLSAFE = new Encoder(true, null, -1); static final Encoder RFC4648_URLSAFE = new Encoder(true, null, -1, true);
static final Encoder RFC2045 = new Encoder(false, CRLF, MIMELINEMAX); static final Encoder RFC2045 = new Encoder(false, CRLF, MIMELINEMAX, true);
private final int outLength(int srclen) {
int len = 0;
if (doPadding) {
len = 4 * ((srclen + 2) / 3);
} else {
int n = srclen % 3;
len = 4 * (srclen / 3) + (n == 0 ? 0 : n + 1);
}
if (linemax > 0) // line separators
len += (len - 1) / linemax * newline.length;
return len;
}
/** /**
* Encodes all bytes from the specified byte array into a newly-allocated * Encodes all bytes from the specified byte array into a newly-allocated
...@@ -243,9 +258,7 @@ public class Base64 { ...@@ -243,9 +258,7 @@ public class Base64 {
* encoded bytes. * encoded bytes.
*/ */
public byte[] encode(byte[] src) { public byte[] encode(byte[] src) {
int len = 4 * ((src.length + 2) / 3); // dst array size int len = outLength(src.length); // dst array size
if (linemax > 0) // line separators
len += (len - 1) / linemax * newline.length;
byte[] dst = new byte[len]; byte[] dst = new byte[len];
int ret = encode0(src, 0, src.length, dst); int ret = encode0(src, 0, src.length, dst);
if (ret != dst.length) if (ret != dst.length)
...@@ -273,10 +286,7 @@ public class Base64 { ...@@ -273,10 +286,7 @@ public class Base64 {
* space for encoding all input bytes. * space for encoding all input bytes.
*/ */
public int encode(byte[] src, byte[] dst) { public int encode(byte[] src, byte[] dst) {
int len = 4 * ((src.length + 2) / 3); // dst array size int len = outLength(src.length); // dst array size
if (linemax > 0) {
len += (len - 1) / linemax * newline.length;
}
if (dst.length < len) if (dst.length < len)
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Output byte array is too small for encoding all input bytes"); "Output byte array is too small for encoding all input bytes");
...@@ -321,9 +331,7 @@ public class Base64 { ...@@ -321,9 +331,7 @@ public class Base64 {
* @return A newly-allocated byte buffer containing the encoded bytes. * @return A newly-allocated byte buffer containing the encoded bytes.
*/ */
public ByteBuffer encode(ByteBuffer buffer) { public ByteBuffer encode(ByteBuffer buffer) {
int len = 4 * ((buffer.remaining() + 2) / 3); int len = outLength(buffer.remaining());
if (linemax > 0)
len += (len - 1) / linemax * newline.length;
byte[] dst = new byte[len]; byte[] dst = new byte[len];
int ret = 0; int ret = 0;
if (buffer.hasArray()) { if (buffer.hasArray()) {
...@@ -415,7 +423,25 @@ public class Base64 { ...@@ -415,7 +423,25 @@ public class Base64 {
public OutputStream wrap(OutputStream os) { public OutputStream wrap(OutputStream os) {
Objects.requireNonNull(os); Objects.requireNonNull(os);
return new EncOutputStream(os, isURL ? toBase64URL : toBase64, return new EncOutputStream(os, isURL ? toBase64URL : toBase64,
newline, linemax); newline, linemax, doPadding);
}
/**
* Returns an encoder instance that encodes equivalently to this one,
* but without adding any padding character at the end of the encoded
* byte data.
*
* <p> The encoding scheme of this encoder instance is unaffected by
* this invocation. The returned encoder instance should be used for
* non-padding encoding operation.
*
* @return an equivalent encoder that encodes without adding any
* padding character at the end
*/
public Encoder withoutPadding() {
if (!doPadding)
return this;
return new Encoder(isURL, newline, linemax, false);
} }
private int encodeArray(ByteBuffer src, ByteBuffer dst, int bytesOut) { private int encodeArray(ByteBuffer src, ByteBuffer dst, int bytesOut) {
...@@ -476,15 +502,19 @@ public class Base64 { ...@@ -476,15 +502,19 @@ public class Base64 {
da[dp++] = (byte)base64[b0 >> 2]; da[dp++] = (byte)base64[b0 >> 2];
if (sp == sl) { if (sp == sl) {
da[dp++] = (byte)base64[(b0 << 4) & 0x3f]; da[dp++] = (byte)base64[(b0 << 4) & 0x3f];
if (doPadding) {
da[dp++] = '='; da[dp++] = '=';
da[dp++] = '='; da[dp++] = '=';
}
} else { } else {
int b1 = sa[sp++] & 0xff; int b1 = sa[sp++] & 0xff;
da[dp++] = (byte)base64[(b0 << 4) & 0x3f | (b1 >> 4)]; da[dp++] = (byte)base64[(b0 << 4) & 0x3f | (b1 >> 4)];
da[dp++] = (byte)base64[(b1 << 2) & 0x3f]; da[dp++] = (byte)base64[(b1 << 2) & 0x3f];
if (doPadding) {
da[dp++] = '='; da[dp++] = '=';
} }
} }
}
return dp - dp00 + bytesOut; return dp - dp00 + bytesOut;
} finally { } finally {
src.position(sp - src.arrayOffset()); src.position(sp - src.arrayOffset());
...@@ -548,15 +578,19 @@ public class Base64 { ...@@ -548,15 +578,19 @@ public class Base64 {
dst.put(dp++, (byte)base64[b0 >> 2]); dst.put(dp++, (byte)base64[b0 >> 2]);
if (sp == src.limit()) { if (sp == src.limit()) {
dst.put(dp++, (byte)base64[(b0 << 4) & 0x3f]); dst.put(dp++, (byte)base64[(b0 << 4) & 0x3f]);
if (doPadding) {
dst.put(dp++, (byte)'='); dst.put(dp++, (byte)'=');
dst.put(dp++, (byte)'='); dst.put(dp++, (byte)'=');
}
} else { } else {
int b1 = src.get(sp++) & 0xff; int b1 = src.get(sp++) & 0xff;
dst.put(dp++, (byte)base64[(b0 << 4) & 0x3f | (b1 >> 4)]); dst.put(dp++, (byte)base64[(b0 << 4) & 0x3f | (b1 >> 4)]);
dst.put(dp++, (byte)base64[(b1 << 2) & 0x3f]); dst.put(dp++, (byte)base64[(b1 << 2) & 0x3f]);
if (doPadding) {
dst.put(dp++, (byte)'='); dst.put(dp++, (byte)'=');
} }
} }
}
return dp - dp00 + bytesOut; return dp - dp00 + bytesOut;
} finally { } finally {
src.position(sp); src.position(sp);
...@@ -597,15 +631,19 @@ public class Base64 { ...@@ -597,15 +631,19 @@ public class Base64 {
dst[dp++] = (byte)base64[b0 >> 2]; dst[dp++] = (byte)base64[b0 >> 2];
if (sp == end) { if (sp == end) {
dst[dp++] = (byte)base64[(b0 << 4) & 0x3f]; dst[dp++] = (byte)base64[(b0 << 4) & 0x3f];
if (doPadding) {
dst[dp++] = '='; dst[dp++] = '=';
dst[dp++] = '='; dst[dp++] = '=';
}
} else { } else {
int b1 = src[sp++] & 0xff; int b1 = src[sp++] & 0xff;
dst[dp++] = (byte)base64[(b0 << 4) & 0x3f | (b1 >> 4)]; dst[dp++] = (byte)base64[(b0 << 4) & 0x3f | (b1 >> 4)];
dst[dp++] = (byte)base64[(b1 << 2) & 0x3f]; dst[dp++] = (byte)base64[(b1 << 2) & 0x3f];
if (doPadding) {
dst[dp++] = '='; dst[dp++] = '=';
} }
} }
}
return dp; return dp;
} }
} }
...@@ -1149,14 +1187,16 @@ public class Base64 { ...@@ -1149,14 +1187,16 @@ public class Base64 {
private final char[] base64; // byte->base64 mapping private final char[] base64; // byte->base64 mapping
private final byte[] newline; // line separator, if needed private final byte[] newline; // line separator, if needed
private final int linemax; private final int linemax;
private final boolean doPadding;// whether or not to pad
private int linepos = 0; private int linepos = 0;
EncOutputStream(OutputStream os, EncOutputStream(OutputStream os, char[] base64,
char[] base64, byte[] newline, int linemax) { byte[] newline, int linemax, boolean doPadding) {
super(os); super(os);
this.base64 = base64; this.base64 = base64;
this.newline = newline; this.newline = newline;
this.linemax = linemax; this.linemax = linemax;
this.doPadding = doPadding;
} }
@Override @Override
...@@ -1228,15 +1268,19 @@ public class Base64 { ...@@ -1228,15 +1268,19 @@ public class Base64 {
checkNewline(); checkNewline();
out.write(base64[b0 >> 2]); out.write(base64[b0 >> 2]);
out.write(base64[(b0 << 4) & 0x3f]); out.write(base64[(b0 << 4) & 0x3f]);
if (doPadding) {
out.write('='); out.write('=');
out.write('='); out.write('=');
}
} else if (leftover == 2) { } else if (leftover == 2) {
checkNewline(); checkNewline();
out.write(base64[b0 >> 2]); out.write(base64[b0 >> 2]);
out.write(base64[(b0 << 4) & 0x3f | (b1 >> 4)]); out.write(base64[(b0 << 4) & 0x3f | (b1 >> 4)]);
out.write(base64[(b1 << 2) & 0x3f]); out.write(base64[(b1 << 2) & 0x3f]);
if (doPadding) {
out.write('='); out.write('=');
} }
}
leftover = 0; leftover = 0;
out.close(); out.close();
} }
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
/** /**
* @test 4235519 8004212 8005394 8007298 8006295 8006315 8006530 8007379 8008925 * @test 4235519 8004212 8005394 8007298 8006295 8006315 8006530 8007379 8008925
* 8014217 8025003 * 8014217 8025003 8026330
* @summary tests java.util.Base64 * @summary tests java.util.Base64
*/ */
...@@ -47,12 +47,9 @@ public class TestBase64 { ...@@ -47,12 +47,9 @@ public class TestBase64 {
numBytes = Integer.parseInt(args[1]); numBytes = Integer.parseInt(args[1]);
} }
test(Base64.getEncoder(), Base64.getDecoder(), test(Base64.getEncoder(), Base64.getDecoder(), numRuns, numBytes);
numRuns, numBytes); test(Base64.getUrlEncoder(), Base64.getUrlDecoder(), numRuns, numBytes);
test(Base64.getUrlEncoder(), Base64.getUrlDecoder(), test(Base64.getMimeEncoder(), Base64.getMimeDecoder(), numRuns, numBytes);
numRuns, numBytes);
test(Base64.getMimeEncoder(), Base64.getMimeDecoder(),
numRuns, numBytes);
Random rnd = new java.util.Random(); Random rnd = new java.util.Random();
byte[] nl_1 = new byte[] {'\n'}; byte[] nl_1 = new byte[] {'\n'};
...@@ -142,6 +139,10 @@ public class TestBase64 { ...@@ -142,6 +139,10 @@ public class TestBase64 {
enc.encode(new byte[0]); enc.encode(new byte[0]);
dec.decode(new byte[0]); dec.decode(new byte[0]);
for (boolean withoutPadding : new boolean[] { false, true}) {
if (withoutPadding) {
enc = enc.withoutPadding();
}
for (int i=0; i<numRuns; i++) { for (int i=0; i<numRuns; i++) {
for (int j=1; j<numBytes; j++) { for (int j=1; j<numBytes; j++) {
byte[] orig = new byte[j]; byte[] orig = new byte[j];
...@@ -153,13 +154,18 @@ public class TestBase64 { ...@@ -153,13 +154,18 @@ public class TestBase64 {
checkEqual(orig, decoded, checkEqual(orig, decoded,
"Base64 array encoding/decoding failed!"); "Base64 array encoding/decoding failed!");
if (withoutPadding) {
if (encoded[encoded.length - 1] == '=')
throw new RuntimeException(
"Base64 enc.encode().withoutPadding() has padding!");
}
// compare to sun.misc.BASE64Encoder // compare to sun.misc.BASE64Encoder
byte[] encoded2 = sunmisc.encode(orig).getBytes("ASCII"); byte[] encoded2 = sunmisc.encode(orig).getBytes("ASCII");
checkEqual(normalize(encoded), if (!withoutPadding) { // don't test for withoutPadding()
normalize(encoded2), checkEqual(normalize(encoded), normalize(encoded2),
"Base64 enc.encode() does not match sun.misc.base64!"); "Base64 enc.encode() does not match sun.misc.base64!");
}
// remove padding '=' to test non-padding decoding case // remove padding '=' to test non-padding decoding case
if (encoded[encoded.length -2] == '=') if (encoded[encoded.length -2] == '=')
encoded2 = Arrays.copyOf(encoded, encoded.length -2); encoded2 = Arrays.copyOf(encoded, encoded.length -2);
...@@ -304,6 +310,7 @@ public class TestBase64 { ...@@ -304,6 +310,7 @@ public class TestBase64 {
} }
} }
} }
}
private static final byte[] ba_null = null; private static final byte[] ba_null = null;
private static final String str_null = null; private static final String str_null = null;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册