JavaXT

Base64 Class

Encodes and decodes to and from Base64 notation. The code was written by Robert Harder (v2.1):
http://iharder.net/base64

Constructors

There are no constructors. You can call the methods directly.

Methods

decode( String s ) returns byte[]
Decodes data from Base64 notation, automatically detecting gzip-compressed data and decompressing it.
sthe String to decode
decode( byte[] source, int off, int len ) returns byte[]
Very low-level access to decoding ASCII characters in the form of a byte array. Does not support automatically gunzipping or any other "fancy" features.
sourceThe Base64 encoded data
offThe offset of where to begin decoding
lenThe length of characters to decode
decodeFromFile( String filename ) returns byte[]
Convenience method for reading a base64-encoded file and decoding it.
filenameFilename for reading encoded data
decodeToFile( String dataToDecode, String filename ) returns boolean
Convenience method for decoding data to a file.
dataToDecodeBase64-encoded data as a String
filenameFilename for saving decoded data
decodeToObject( String encodedObject ) returns Object
Attempts to decode Base64 data and deserialize a Java Object within. Returns null if there was an error.
encodedObjectThe Base64 data to decode
encodeBytes( byte[] source ) returns String
Encodes a byte array into Base64 notation. Does not GZip-compress data.
sourceThe data to convert
encodeBytes( byte[] source, int options ) returns String
Encodes a byte array into Base64 notation.

Valid options:

   GZIP: gzip-compresses object before encoding it.
   DONT_BREAK_LINES: don't break lines at 76 characters
     Note: Technically, this makes your encoding non-compliant.
 

Example: encodeBytes( myData, Base64.GZIP ) or

Example: encodeBytes( myData, Base64.GZIP | Base64.DONT_BREAK_LINES )
sourceThe data to convert
optionsSpecified options

encodeBytes( byte[] source, int off, int len ) returns String
Encodes a byte array into Base64 notation. Does not GZip-compress data.
sourceThe data to convert
offOffset in array where conversion should begin
lenLength of data to convert
encodeBytes( byte[] source, int off, int len, int options ) returns String
Encodes a byte array into Base64 notation.

Valid options:

   GZIP: gzip-compresses object before encoding it.
   DONT_BREAK_LINES: don't break lines at 76 characters
     Note: Technically, this makes your encoding non-compliant.
 

Example: encodeBytes( myData, Base64.GZIP ) or

Example: encodeBytes( myData, Base64.GZIP | Base64.DONT_BREAK_LINES )
sourceThe data to convert
offOffset in array where conversion should begin
lenLength of data to convert
optionsSpecified options

encodeFromFile( String filename ) returns String
Convenience method for reading a binary file and base64-encoding it.
filenameFilename for reading binary data
encodeObject( java.io.Serializable serializableObject ) returns String
Serializes an object and returns the Base64-encoded version of that serialized object. If the object cannot be serialized or there is another error, the method will return null. The object is not GZip-compressed before being encoded.
serializableObjectThe object to encode
encodeObject( java.io.Serializable serializableObject, int options ) returns String
Serializes an object and returns the Base64-encoded version of that serialized object. If the object cannot be serialized or there is another error, the method will return null.

Valid options:

   GZIP: gzip-compresses object before encoding it.
   DONT_BREAK_LINES: don't break lines at 76 characters
     Note: Technically, this makes your encoding non-compliant.
 

Example: encodeObject( myObj, Base64.GZIP ) or

Example: encodeObject( myObj, Base64.GZIP | Base64.DONT_BREAK_LINES )
serializableObjectThe object to encode
optionsSpecified options

encodeToFile( byte[] dataToEncode, String filename ) returns boolean
Convenience method for encoding data to a file.
dataToEncodebyte array of data to encode in base64 form
filenameFilename for saving encoded data