Base64public class Base64 extends Object Utilities for encoding and decoding the Base64 representation of
binary data. See RFCs 2045 and 3548. |
Fields Summary |
---|
public static final int | DEFAULTDefault values for encoder/decoder flags. | public static final int | NO_PADDINGEncoder flag bit to omit the padding '=' characters at the end
of the output (if any). | public static final int | NO_WRAPEncoder flag bit to omit all line terminators (i.e., the output
will be on one long line). | public static final int | CRLFEncoder flag bit to indicate lines should be terminated with a
CRLF pair instead of just an LF. Has no effect if {@code
NO_WRAP} is specified as well. | public static final int | URL_SAFEEncoder/decoder flag bit to indicate using the "URL and
filename safe" variant of Base64 (see RFC 3548 section 4) where
{@code -} and {@code _} are used in place of {@code +} and
{@code /}. | public static final int | NO_CLOSEFlag to pass to {@link Base64OutputStream} to indicate that it
should not close the output stream it is wrapping when it
itself is closed. |
Constructors Summary |
---|
private Base64()
|
Methods Summary |
---|
public static byte[] | decode(java.lang.String str, int flags)Decode the Base64-encoded data in input and return the data in
a new byte array.
The padding '=' characters at the end are considered optional, but
if any are present, there must be the correct number of them.
// --------------------------------------------------------
// decoding
// --------------------------------------------------------
return decode(str.getBytes(), flags);
| public static byte[] | decode(byte[] input, int flags)Decode the Base64-encoded data in input and return the data in
a new byte array.
The padding '=' characters at the end are considered optional, but
if any are present, there must be the correct number of them.
return decode(input, 0, input.length, flags);
| public static byte[] | decode(byte[] input, int offset, int len, int flags)Decode the Base64-encoded data in input and return the data in
a new byte array.
The padding '=' characters at the end are considered optional, but
if any are present, there must be the correct number of them.
// Allocate space for the most data the input could represent.
// (It could contain less if it contains whitespace, etc.)
Decoder decoder = new Decoder(flags, new byte[len*3/4]);
if (!decoder.process(input, offset, len, true)) {
throw new IllegalArgumentException("bad base-64");
}
// Maybe we got lucky and allocated exactly enough output space.
if (decoder.op == decoder.output.length) {
return decoder.output;
}
// Need to shorten the array, so allocate a new one of the
// right size and copy.
byte[] temp = new byte[decoder.op];
System.arraycopy(decoder.output, 0, temp, 0, decoder.op);
return temp;
| public static byte[] | encode(byte[] input, int flags)Base64-encode the given data and return a newly allocated
byte[] with the result.
return encode(input, 0, input.length, flags);
| public static byte[] | encode(byte[] input, int offset, int len, int flags)Base64-encode the given data and return a newly allocated
byte[] with the result.
Encoder encoder = new Encoder(flags, null);
// Compute the exact length of the array we will produce.
int output_len = len / 3 * 4;
// Account for the tail of the data and the padding bytes, if any.
if (encoder.do_padding) {
if (len % 3 > 0) {
output_len += 4;
}
} else {
switch (len % 3) {
case 0: break;
case 1: output_len += 2; break;
case 2: output_len += 3; break;
}
}
// Account for the newlines, if any.
if (encoder.do_newline && len > 0) {
output_len += (((len-1) / (3 * Encoder.LINE_GROUPS)) + 1) *
(encoder.do_cr ? 2 : 1);
}
encoder.output = new byte[output_len];
encoder.process(input, offset, len, true);
assert encoder.op == output_len;
return encoder.output;
| public static java.lang.String | encodeToString(byte[] input, int flags)Base64-encode the given data and return a newly allocated
String with the result.
try {
return new String(encode(input, flags), "US-ASCII");
} catch (UnsupportedEncodingException e) {
// US-ASCII is guaranteed to be available.
throw new AssertionError(e);
}
| public static java.lang.String | encodeToString(byte[] input, int offset, int len, int flags)Base64-encode the given data and return a newly allocated
String with the result.
try {
return new String(encode(input, offset, len, flags), "US-ASCII");
} catch (UnsupportedEncodingException e) {
// US-ASCII is guaranteed to be available.
throw new AssertionError(e);
}
|
|