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(String, int, 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.
A |
| public static class | Base64.
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 | DONT_GUNZIP
Specify that gzipped data should not be automatically gunzipped. |
| 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 | 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. |
| 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 | decodeFileToFile(String
Input file infile, String Output file outfile)Reads infile and decodes it to outfile. |
| public static byte[] | Returns: decoded byte arrayFilename for reading encoded data filename)Convenience method for reading a base64-encoded file and decoding it. |
| public static void | decodeToFile(String
Base64-encoded data as a string dataToDecode, String Filename for saving decoded data filename)Convenience method for decoding data to a file. |
| public static Object | Returns: The decoded and deserialized objectThe Base64 data to decode encodedObject)Attempts to decode Base64 data and deserialize a Java Object within. |
| public static Object | Returns: The decoded and deserialized objectThe Base64 data to decode encodedObject, int Various parameters related to decoding options, final ClassLoader Optional class loader to use in deserializing classes. loader)Attempts to decode Base64 data and deserialize a Java Object within. |
| 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 void | encodeFileToFile(String
Input file infile, String Output file outfile)Reads infile and encodes it to outfile. |
| public static String | Returns: base64-encoded stringFilename for reading binary data filename)Convenience method for reading a binary file and base64-encoding it. |
| 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. |
| public static void | encodeToFile(byte[]
byte array of data to encode in base64 form dataToEncode, String Filename for saving encoded data filename)Convenience method for encoding data to a file. |
| private static byte[] | getAlphabet(int options)
Returns one of the _SOMETHING_ALPHABET byte arrays depending on the options specified. |
| private static byte[] | getDecodabet(int options)
Returns one of the _SOMETHING_DECODABET byte arrays depending on the options specified. |