--- old/src/share/classes/java/util/Base64.java 2013-11-08 14:17:23.000000000 -0800 +++ new/src/share/classes/java/util/Base64.java 2013-11-08 14:17:23.000000000 -0800 @@ -50,7 +50,7 @@ * *
Uses the "URL and Filename safe Base64 Alphabet" as specified - * in Table 2 of RFC 4648 for encoding and decoding. The +* in Table 2 of RFC 4648 for encoding and decoding. The * encoder does not add any line feed (line separator) character. * The decoder rejects data that contains characters outside the * base64 alphabet.
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) { @@ -476,13 +502,17 @@ da[dp++] = (byte)base64[b0 >> 2]; if (sp == sl) { da[dp++] = (byte)base64[(b0 << 4) & 0x3f]; - da[dp++] = '='; - da[dp++] = '='; + if (doPadding) { + da[dp++] = '='; + da[dp++] = '='; + } } else { int b1 = sa[sp++] & 0xff; da[dp++] = (byte)base64[(b0 << 4) & 0x3f | (b1 >> 4)]; da[dp++] = (byte)base64[(b1 << 2) & 0x3f]; - da[dp++] = '='; + if (doPadding) { + da[dp++] = '='; + } } } return dp - dp00 + bytesOut; @@ -548,13 +578,17 @@ dst.put(dp++, (byte)base64[b0 >> 2]); if (sp == src.limit()) { dst.put(dp++, (byte)base64[(b0 << 4) & 0x3f]); - dst.put(dp++, (byte)'='); - dst.put(dp++, (byte)'='); + if (doPadding) { + dst.put(dp++, (byte)'='); + dst.put(dp++, (byte)'='); + } } else { int b1 = src.get(sp++) & 0xff; dst.put(dp++, (byte)base64[(b0 << 4) & 0x3f | (b1 >> 4)]); dst.put(dp++, (byte)base64[(b1 << 2) & 0x3f]); - dst.put(dp++, (byte)'='); + if (doPadding) { + dst.put(dp++, (byte)'='); + } } } return dp - dp00 + bytesOut; @@ -597,13 +631,17 @@ dst[dp++] = (byte)base64[b0 >> 2]; if (sp == end) { dst[dp++] = (byte)base64[(b0 << 4) & 0x3f]; - dst[dp++] = '='; - dst[dp++] = '='; + if (doPadding) { + dst[dp++] = '='; + dst[dp++] = '='; + } } else { int b1 = src[sp++] & 0xff; dst[dp++] = (byte)base64[(b0 << 4) & 0x3f | (b1 >> 4)]; dst[dp++] = (byte)base64[(b1 << 2) & 0x3f]; - dst[dp++] = '='; + if (doPadding) { + dst[dp++] = '='; + } } } return dp; @@ -1149,14 +1187,16 @@ private final char[] base64; // byte->base64 mapping private final byte[] newline; // line separator, if needed private final int linemax; + private final boolean doPadding;// whether or not to pad private int linepos = 0; - EncOutputStream(OutputStream os, - char[] base64, byte[] newline, int linemax) { + EncOutputStream(OutputStream os, char[] base64, + byte[] newline, int linemax, boolean doPadding) { super(os); this.base64 = base64; this.newline = newline; this.linemax = linemax; + this.doPadding = doPadding; } @Override @@ -1228,14 +1268,18 @@ checkNewline(); out.write(base64[b0 >> 2]); out.write(base64[(b0 << 4) & 0x3f]); - out.write('='); - out.write('='); + if (doPadding) { + out.write('='); + out.write('='); + } } else if (leftover == 2) { checkNewline(); out.write(base64[b0 >> 2]); out.write(base64[(b0 << 4) & 0x3f | (b1 >> 4)]); out.write(base64[(b1 << 2) & 0x3f]); - out.write('='); + if (doPadding) { + out.write('='); + } } leftover = 0; out.close();