Encodes and decodes to and from Base64 notation.
Homepage: http://iharder.net/base64.
Example:
String encoded = Base64.encode( myByteArray );
byte[] myByteArray = Base64.decode( encoded );
The options parameter, which appears in a few places, is used to pass several pieces of information to the encoder. In the "higher level" methods such as encodeBytes( bytes, options ) the options parameter can be used to indicate such things as first gzipping the bytes before encoding them, not inserting linefeeds, and encoding using the URL-safe and Ordered dialects.
Note, according to RFC3548, Section 2.1, implementations should not add line feeds unless explicitly told to do so. I've got Base64 set to this behavior now, although earlier versions broke lines by default.
The constants defined in Base64 can be OR-ed together to combine options, so you might make a call like this:
String encoded = Base64.encodeBytes( mybytes, Base64.GZIP | Base64.DO_BREAK_LINES );
to compress the data before encoding it and then making the output have newline characters.
Also...
String encoded = Base64.encodeBytes( crazyString.getBytes() );
Change Log:
encodeFromFile where estimated buffer size
was wrong for files of size 31, 34, and 37 bytes.decodeToObject(java.lang.String, int, java.lang.ClassLoader)
method.encodeBytesToBytes(byte[]) family of methods (and not
using the gzip options which uses a different mechanism with streams and stuff).encodeBytesToBytes(byte[], int, int, int) and some
similar helper methods to be more efficient with memory by not returning a
String but just a byte array.I am placing this code in the Public Domain. Do with it as you will. This software comes with no guarantees or warranties but with plenty of well-wishing instead! Please visit http://iharder.net/base64 periodically to check for updates or to contribute improvements.
| Modifier and Type | Class and Description |
|---|---|
| public static class | Base64.InputStream
A |
| public static class | Base64.OutputStream
A |
| Modifier and Type | Field and Description |
|---|---|
| private static final byte[] | _ORDERED_ALPHABET
I don't get the point of this technique, but someone requested it, and it is described here: http://www.faqs.org/qa/rfcc-1940.html. |
| private static final byte[] | _ORDERED_DECODABET
Used in decoding the "ordered" dialect of Base64. |
| private static final byte[] | _STANDARD_ALPHABET
The 64 valid Base64 values. |
| private static final byte[] | _STANDARD_DECODABET
Translates a Base64 value to either its 6-bit reconstruction value or a negative number indicating some other meaning. |
| private static final byte[] | _URL_SAFE_ALPHABET
Used in the URL- and Filename-safe dialect described in Section 4 of RFC3548: http://www.faqs.org/rfcs/rfc3548.html. |
| private static final byte[] | _URL_SAFE_DECODABET
Used in decoding URL- and Filename-safe dialects of Base64. |
| public static final int | DECODE
Specify decoding in first bit. |
| public static final int | DO_BREAK_LINES
Do break lines when encoding. |
| public static final int | ENCODE
Specify encoding in first bit. |
| private static final byte | EQUALS_SIGN
The equals sign (=) as a byte. |
| private static final byte | |
| public static final int | GUNZIP
Specify that data should be gunzipped. |
| public static final int | GZIP
Specify that data should be gzip-compressed in second bit. |
| private static final int | MAX_LINE_LENGTH
Maximum line length (76) of Base64 output. |
| private static final byte | NEW_LINE
The new line character (\n) as a byte. |
| public static final int | NO_OPTIONS
No options specified. |
| public static final int | ORDERED
Encode using the special "ordered" dialect of Base64 described here: http://www.faqs.org/qa/rfcc-1940.html. |
| private static final String | PREFERRED_ENCODING
Preferred encoding. |
| public static final int | URL_SAFE
Encode using Base64-like encoding that is URL- and Filename-safe as described in Section 4 of RFC3548: http://www.faqs.org/rfcs/rfc3548.html. |
| private static final byte |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static byte[] | Returns: decoded dataThe Base64 encoded data source)Low-level access to decoding ASCII characters in the form of a byte array. |
| public static byte[] | Returns: decoded dataThe Base64 encoded data source, int The offset of where to begin decoding off, int The length of characters to decode len, int Can specify options such as alphabet type to use options)Low-level access to decoding ASCII characters in the form of a byte array. |
| public static byte[] | |
| public static byte[] | |
| private static int | Returns: the number of decoded bytes convertedthe array to convert source, int the index where conversion begins srcOffset, byte[] the array to hold the conversion destination, int the index where output will be put destOffset, int alphabet type is pulled from this (standard, url-safe, ordered) options)Decodes four bytes from array source and writes the resulting bytes (up to three of them) to destination. |
| public static void | encode(ByteBuffer
input buffer raw, ByteBuffer output buffer encoded)Performs Base64 encoding on the |
| public static void | encode(ByteBuffer
input buffer raw, CharBuffer output buffer encoded)Performs Base64 encoding on the |
| private static byte[] | Returns: four byte array in Base64 notation.A reusable byte array to reduce array instantiation b4, byte[] the array to convert threeBytes, int the number of significant bytes in your array numSigBytes, int options)Encodes up to the first three bytes of array threeBytes and returns a four-byte array in Base64 notation. |
| private static byte[] | Returns: the destination arraythe array to convert source, int the index where conversion begins srcOffset, int the number of significant bytes in your array numSigBytes, byte[] the array to hold the conversion destination, int the index where output will be put destOffset, int options)Encodes up to three bytes of the array source and writes the resulting four Base64 bytes to destination. |
| public static String | Returns: The data in Base64-encoded formThe data to convert source)Encodes a byte array into Base64 notation. |
| public static String | Returns: The Base64-encoded data as a StringThe data to convert source, int Specified options options)Encodes a byte array into Base64 notation. |
| public static String | Returns: The Base64-encoded data as a StringThe data to convert source, int Offset in array where conversion should begin off, int Length of data to convert len)Encodes a byte array into Base64 notation. |
| public static String | Returns: The Base64-encoded data as a StringThe data to convert source, int Offset in array where conversion should begin off, int Length of data to convert len, int Specified options options)Encodes a byte array into Base64 notation. |
| public static byte[] | Returns: The Base64-encoded data as a byte[] (of ASCII characters)The data to convert source)Similar to |
| public static byte[] | Returns: The Base64-encoded data as a StringThe data to convert source, int Offset in array where conversion should begin off, int Length of data to convert len, int Specified options options)Similar to |
| public static String | Returns: The Base64-encoded objectThe object to encode serializableObject)Serializes an object and returns the Base64-encoded version of that serialized object. |
| public static String | Returns: The Base64-encoded objectThe object to encode serializableObject, int Specified options options)Serializes an object and returns the Base64-encoded version of that serialized object. |
| private static final byte[] | getAlphabet(int options)
Returns one of the _SOMETHING_ALPHABET byte arrays depending on the options specified. |
| private static final byte[] | getDecodabet(int options)
Returns one of the _SOMETHING_DECODABET byte arrays depending on the options specified. |
| _ORDERED_ALPHABET | back to summary |
|---|---|
| private static final byte[] _ORDERED_ALPHABET I don't get the point of this technique, but someone requested it, and it is described here: http://www.faqs.org/qa/rfcc-1940.html. | |
| _ORDERED_DECODABET | back to summary |
|---|---|
| private static final byte[] _ORDERED_DECODABET Used in decoding the "ordered" dialect of Base64. | |
| _STANDARD_ALPHABET | back to summary |
|---|---|
| private static final byte[] _STANDARD_ALPHABET The 64 valid Base64 values. | |
| _STANDARD_DECODABET | back to summary |
|---|---|
| private static final byte[] _STANDARD_DECODABET Translates a Base64 value to either its 6-bit reconstruction value or a negative number indicating some other meaning. | |
| _URL_SAFE_ALPHABET | back to summary |
|---|---|
| private static final byte[] _URL_SAFE_ALPHABET Used in the URL- and Filename-safe dialect described in Section 4 of RFC3548: http://www.faqs.org/rfcs/rfc3548.html. Notice that the last two bytes become "hyphen" and "underscore" instead of "plus" and "slash." | |
| _URL_SAFE_DECODABET | back to summary |
|---|---|
| private static final byte[] _URL_SAFE_DECODABET Used in decoding URL- and Filename-safe dialects of Base64. | |
| DECODE | back to summary |
|---|---|
| public static final int DECODE Specify decoding in first bit. Value is zero. | |
| DO_BREAK_LINES | back to summary |
|---|---|
| public static final int DO_BREAK_LINES Do break lines when encoding. Value is 8. | |
| ENCODE | back to summary |
|---|---|
| public static final int ENCODE Specify encoding in first bit. Value is one. | |
| EQUALS_SIGN | back to summary |
|---|---|
| private static final byte EQUALS_SIGN The equals sign (=) as a byte. | |
| EQUALS_SIGN_ENC | back to summary |
|---|---|
| private static final byte EQUALS_SIGN_ENC | |
| GUNZIP | back to summary |
|---|---|
| public static final int GUNZIP Specify that data should be gunzipped. | |
| GZIP | back to summary |
|---|---|
| public static final int GZIP Specify that data should be gzip-compressed in second bit. Value is two. | |
| MAX_LINE_LENGTH | back to summary |
|---|---|
| private static final int MAX_LINE_LENGTH Maximum line length (76) of Base64 output. | |
| NEW_LINE | back to summary |
|---|---|
| private static final byte NEW_LINE The new line character (\n) as a byte. | |
| NO_OPTIONS | back to summary |
|---|---|
| public static final int NO_OPTIONS No options specified. Value is zero. | |
| ORDERED | back to summary |
|---|---|
| public static final int ORDERED Encode using the special "ordered" dialect of Base64 described here: http://www.faqs.org/qa/rfcc-1940.html. | |
| PREFERRED_ENCODING | back to summary |
|---|---|
| private static final String PREFERRED_ENCODING Preferred encoding. | |
| URL_SAFE | back to summary |
|---|---|
| public static final int URL_SAFE Encode using Base64-like encoding that is URL- and Filename-safe as described in Section 4 of RFC3548: http://www.faqs.org/rfcs/rfc3548.html. It is important to note that data encoded this way is not officially valid Base64, or at the very least should not be called Base64 without also specifying that it was encoded using the URL- and Filename-safe dialect. | |
| WHITE_SPACE_ENC | back to summary |
|---|---|
| private static final byte WHITE_SPACE_ENC | |
| Base64 | back to summary |
|---|---|
| private Base64() Defeats instantiation. | |
| decode | back to summary |
|---|---|
| public static byte[] decode(byte[] source) throws IOException Low-level access to decoding ASCII characters in the form of a byte array. Ignores GUNZIP option, if it's set. This is not generally a recommended method, although it is used internally as part of the decoding process. Special case: if len = 0, an empty array is returned. Still, if you need more speed and reduced memory footprint (and aren't gzipping), consider this method.
| |
| decode | back to summary |
|---|---|
| public static byte[] decode(byte[] source, int off, int len, int options) throws IOException Low-level access to decoding ASCII characters in the form of a byte array. Ignores GUNZIP option, if it's set. This is not generally a recommended method, although it is used internally as part of the decoding process. Special case: if len = 0, an empty array is returned. Still, if you need more speed and reduced memory footprint (and aren't gzipping), consider this method.
| |
| decode | back to summary |
|---|---|
| public static byte[] decode(String s) throws IOException Decodes data from Base64 notation, automatically detecting gzip-compressed data and decompressing it.
| |
| decode | back to summary |
|---|---|
| public static byte[] decode(String s, int options) throws IOException Decodes data from Base64 notation, automatically detecting gzip-compressed data and decompressing it.
| |
| decode4to3 | back to summary |
|---|---|
| private static int decode4to3(byte[] source, int srcOffset, byte[] destination, int destOffset, int options) Decodes four bytes from array source and writes the resulting bytes (up to three of them) to destination. The source and destination arrays can be manipulated anywhere along their length by specifying srcOffset and destOffset. This method does not check to make sure your arrays are large enough to accomodate srcOffset + 4 for the source array or destOffset + 3 for the destination array. This method returns the actual number of bytes that were converted from the Base64 encoding. This is the lowest level of the decoding methods with all possible parameters.
| |
| encode | back to summary |
|---|---|
| public static void encode(ByteBuffer raw, ByteBuffer encoded) Performs Base64 encoding on the
| |
| encode | back to summary |
|---|---|
| public static void encode(ByteBuffer raw, CharBuffer encoded) Performs Base64 encoding on the
| |
| encode3to4 | back to summary |
|---|---|
| private static byte[] encode3to4(byte[] b4, byte[] threeBytes, int numSigBytes, int options) Encodes up to the first three bytes of array threeBytes and returns a four-byte array in Base64 notation. The actual number of significant bytes in your array is given by numSigBytes. The array threeBytes needs only be as big as numSigBytes. Code can reuse a byte array by passing a four-byte array as b4.
| |
| encode3to4 | back to summary |
|---|---|
| private static byte[] encode3to4(byte[] source, int srcOffset, int numSigBytes, byte[] destination, int destOffset, int options) Encodes up to three bytes of the array source and writes the resulting four Base64 bytes to destination. The source and destination arrays can be manipulated anywhere along their length by specifying srcOffset and destOffset. This method does not check to make sure your arrays are large enough to accommodate srcOffset + 3 for the source array or destOffset + 4 for the destination array. The actual number of significant bytes in your array is given by numSigBytes. This is the lowest level of the encoding methods with all possible parameters.
| |
| encodeBytes | back to summary |
|---|---|
| public static String encodeBytes(byte[] source) Encodes a byte array into Base64 notation. Does not GZip-compress data.
| |
| encodeBytes | back to summary |
|---|---|
| public static String encodeBytes(byte[] source, int options) throws IOException Encodes a byte array into Base64 notation. Example options:
GZIP: gzip-compresses object before encoding it.
DO_BREAK_LINES: break lines at 76 characters
Note: Technically, without line break your encoding may become non-compliant (see rfc2045 and rfc4648).
Example:
Example: As of v 2.3, if there is an error with the GZIP stream, the method will throw an java.io.IOException. This is new to v2.3! In earlier versions, it just returned a null value, but in retrospect that's a pretty poor way to handle it.
| |
| encodeBytes | back to summary |
|---|---|
| public static String encodeBytes(byte[] source, int off, int len) Encodes a byte array into Base64 notation. Does not GZip-compress data. As of v 2.3, if there is an error, the method will throw an java.io.IOException. This is new to v2.3! In earlier versions, it just returned a null value, but in retrospect that's a pretty poor way to handle it.
| |
| encodeBytes | back to summary |
|---|---|
| public static String encodeBytes(byte[] source, int off, int len, int options) throws IOException Encodes a byte array into Base64 notation. Example options:
GZIP: gzip-compresses object before encoding it.
DO_BREAK_LINES: break lines at 76 characters
Note: Technically, this makes your encoding non-compliant.
Example:
Example: As of v 2.3, if there is an error with the GZIP stream, the method will throw an java.io.IOException. This is new to v2.3! In earlier versions, it just returned a null value, but in retrospect that's a pretty poor way to handle it.
| |
| encodeBytesToBytes | back to summary |
|---|---|
| public static byte[] encodeBytesToBytes(byte[] source) Similar to
| |
| encodeBytesToBytes | back to summary |
|---|---|
| public static byte[] encodeBytesToBytes(byte[] source, int off, int len, int options) throws IOException Similar to
| |
| encodeObject | back to summary |
|---|---|
| public static String encodeObject(Serializable serializableObject) throws IOException Serializes an object and returns the Base64-encoded version of that serialized object. As of v 2.3, if the object cannot be serialized or there is another error, the method will throw an java.io.IOException. This is new to v2.3! In earlier versions, it just returned a null value, but in retrospect that's a pretty poor way to handle it. The object is not GZip-compressed before being encoded.
| |
| encodeObject | back to summary |
|---|---|
| public static String encodeObject(Serializable serializableObject, int options) throws IOException Serializes an object and returns the Base64-encoded version of that serialized object. As of v 2.3, if the object cannot be serialized or there is another error, the method will throw an java.io.IOException. This is new to v2.3! In earlier versions, it just returned a null value, but in retrospect that's a pretty poor way to handle it. The object is not GZip-compressed before being encoded.Example options: GZIP: gzip-compresses object before encoding it. DO_BREAK_LINES: break lines at 76 characters
Example:
Example:
| |
| getAlphabet | back to summary |
|---|---|
| private static final byte[] getAlphabet(int options) Returns one of the _SOMETHING_ALPHABET byte arrays depending on the options specified. It's possible, though silly, to specify ORDERED and URLSAFE in which case one of them will be picked, though there is no guarantee as to which one will be picked. | |
| getDecodabet | back to summary |
|---|---|
| private static final byte[] getDecodabet(int options) Returns one of the _SOMETHING_DECODABET byte arrays depending on the options specified. It's possible, though silly, to specify ORDERED and URL_SAFE in which case one of them will be picked, though there is no guarantee as to which one will be picked. | |
Base64.InputStream will read data from another
java.io.InputStream, given in the constructor,
and encode/decode to/from Base64 notation on the fly.
Base64
| Modifier and Type | Field and Description |
|---|---|
| private boolean | |
| private byte[] | |
| private int | |
| private byte[] | |
| private boolean | |
| private int | |
| private int | |
| private int | |
| private int |
| Access | Constructor and Description |
|---|---|
| public | InputStream(InputStream
the java.io.InputStream from which to read data. in)Constructs a |
| public | InputStream(InputStream
the java.io.InputStream from which to read data. in, int Specified options options)Constructs a |
| Modifier and Type | Method and Description |
|---|---|
| public int | Returns: next byteOverrides java. |
| public int | Returns: bytes read into array or -1 if end of stream is encountered.array to hold values dest, int offset for array off, int max number of bytes to read into array len)Overrides java. read() repeatedly until the end of stream
is reached or len bytes are read.
|
| breakLines | back to summary |
|---|---|
| private boolean breakLines | |
| buffer | back to summary |
|---|---|
| private byte[] buffer | |
| bufferLength | back to summary |
|---|---|
| private int bufferLength | |
| decodabet | back to summary |
|---|---|
| private byte[] decodabet | |
| encode | back to summary |
|---|---|
| private boolean encode | |
| lineLength | back to summary |
|---|---|
| private int lineLength | |
| numSigBytes | back to summary |
|---|---|
| private int numSigBytes | |
| options | back to summary |
|---|---|
| private int options | |
| position | back to summary |
|---|---|
| private int position | |
| InputStream | back to summary |
|---|---|
| public InputStream(InputStream in) Constructs a
| |
| InputStream | back to summary |
|---|---|
| public InputStream(InputStream in, int options) Constructs a Valid options:
ENCODE or DECODE: Encode or Decode as data is read.
DO_BREAK_LINES: break lines at 76 characters
(only meaningful when encoding)
Example:
| |
| read | back to summary |
|---|---|
| public int read() throws IOException Overrides java. Reads enough of the input stream to convert to/from Base64 and returns the next byte.
| |
| read | back to summary |
|---|---|
| public int read(byte[] dest, int off, int len) throws IOException Overrides java. Calls
| |
Base64.OutputStream will write data to another
java.io.OutputStream, given in the constructor,
and encode/decode to/from Base64 notation on the fly.
Base64
| Modifier and Type | Field and Description |
|---|---|
| private byte[] | |
| private boolean | |
| private byte[] | |
| private int | |
| private byte[] | |
| private boolean | |
| private int | |
| private int | |
| private int | |
| private boolean |
| Access | Constructor and Description |
|---|---|
| public | OutputStream(OutputStream
the java.io.OutputStream to which data will be written. out)Constructs a |
| public | OutputStream(OutputStream
the java.io.OutputStream to which data will be written. out, int Specified options. options)Constructs a |
| Modifier and Type | Method and Description |
|---|---|
| public void | close()
Overrides java. Implements java. |
| public void | |
| public void | |
| public void | |
| public void | write(int
the byte to write theByte)Overrides java. |
| public void | write(byte[]
array from which to read bytes theBytes, int offset for array off, int max number of bytes to read into array len)Overrides java. write(int) repeatedly until len
bytes are written.
|
| b4 | back to summary |
|---|---|
| private byte[] b4 | |
| breakLines | back to summary |
|---|---|
| private boolean breakLines | |
| buffer | back to summary |
|---|---|
| private byte[] buffer | |
| bufferLength | back to summary |
|---|---|
| private int bufferLength | |
| decodabet | back to summary |
|---|---|
| private byte[] decodabet | |
| encode | back to summary |
|---|---|
| private boolean encode | |
| lineLength | back to summary |
|---|---|
| private int lineLength | |
| options | back to summary |
|---|---|
| private int options | |
| position | back to summary |
|---|---|
| private int position | |
| suspendEncoding | back to summary |
|---|---|
| private boolean suspendEncoding | |
| OutputStream | back to summary |
|---|---|
| public OutputStream(OutputStream out) Constructs a
| |
| OutputStream | back to summary |
|---|---|
| public OutputStream(OutputStream out, int options) Constructs a Valid options:
ENCODE or DECODE: Encode or Decode as data is read.
DO_BREAK_LINES: don't break lines at 76 characters
(only meaningful when encoding)
Example:
| |
| close | back to summary |
|---|---|
| public void close() throws IOException Overrides java. Implements java. Flushes and closes (I think, in the superclass) the stream.
| |
| flushBase64 | back to summary |
|---|---|
| public void flushBase64() throws IOException Method added by PHIL. [Thanks, PHIL. -Rob] This pads the buffer without closing the stream.
| |
| resumeEncoding | back to summary |
|---|---|
| public void resumeEncoding() Resumes encoding of the stream. May be helpful if you need to embed a piece of base64-encoded data in a stream.
| |
| suspendEncoding | back to summary |
|---|---|
| public void suspendEncoding() throws IOException Suspends encoding of the stream. May be helpful if you need to embed a piece of base64-encoded data in a stream.
| |
| write | back to summary |
|---|---|
| public void write(int theByte) throws IOException Overrides java. Writes the byte to the output stream after converting to/from Base64 notation. When encoding, bytes are buffered three at a time before the output stream actually gets a write() call. When decoding, bytes are buffered four at a time.
| |
| write | back to summary |
|---|---|
| public void write(byte[] theBytes, int off, int len) throws IOException Overrides java. Calls
| |