Top Description Inners Fields Constructors Methods
io.undertow.websockets.core.protocol.version07

pack-priv Class Base64

extends Object
Class Inheritance
Static Imports
org.wildfly.common.Assert.checkNotNullParamWithNullPointerException, .Assert.checkMinimumParameter, org.xnio.IoUtils.safeClose

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:

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.

Authors
Robert Harder, [email protected]
Version
2.3.7

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
Base64.InputStream

A Base64.InputStream will read data from another java.io.InputStream, given in the constructor, and encode/decode to/from Base64 notation on the fly.

public static class
Base64.OutputStream

A Base64.OutputStream will write data to another java.io.OutputStream, given in the constructor, and encode/decode to/from Base64 notation on the fly.

Field Summary

Modifier and TypeField 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

Constructor Summary

AccessConstructor and Description
private
Base64()

Defeats instantiation.

Method Summary

Modifier and TypeMethod and Description
public static byte[]

Returns:

decoded data
decode
(byte[]
The Base64 encoded data
source
)

Low-level access to decoding ASCII characters in the form of a byte array.

public static byte[]

Returns:

decoded data
decode
(byte[]
The 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[]

Returns:

the decoded data
decode
(String
the string to decode
s
)

Decodes data from Base64 notation, automatically detecting gzip-compressed data and decompressing it.

public static byte[]

Returns:

the decoded data
decode
(String
the string to decode
s
,
int
encode options such as URL_SAFE
options
)

Decodes data from Base64 notation, automatically detecting gzip-compressed data and decompressing it.

private static int

Returns:

the number of decoded bytes converted
decode4to3
(byte[]
the 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 array
decodeFromFile
(String
Filename 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 object
decodeToObject
(String
The Base64 data to decode
encodedObject
)

Attempts to decode Base64 data and deserialize a Java Object within.

public static Object

Returns:

The decoded and deserialized object
decodeToObject
(String
The 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 raw ByteBuffer, writing it to the encoded ByteBuffer.

public static void
encode(ByteBuffer
input buffer
raw
,
CharBuffer
output buffer
encoded
)

Performs Base64 encoding on the raw ByteBuffer, writing it to the encoded CharBuffer.

private static byte[]

Returns:

four byte array in Base64 notation.
encode3to4
(byte[]
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 array
encode3to4
(byte[]
the 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 form
encodeBytes
(byte[]
The data to convert
source
)

Encodes a byte array into Base64 notation.

public static String

Returns:

The Base64-encoded data as a String
encodeBytes
(byte[]
The 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 String
encodeBytes
(byte[]
The 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 String
encodeBytes
(byte[]
The 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)
encodeBytesToBytes
(byte[]
The data to convert
source
)

Similar to encodeBytes(byte[]) but returns a byte array instead of instantiating a String.

public static byte[]

Returns:

The Base64-encoded data as a String
encodeBytesToBytes
(byte[]
The 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 encodeBytes(byte[], int, int, int) but returns a byte array instead of instantiating a String.

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 string
encodeFromFile
(String
Filename for reading binary data
filename
)

Convenience method for reading a binary file and base64-encoding it.

public static String

Returns:

The Base64-encoded object
encodeObject
(Serializable
The object to encode
serializableObject
)

Serializes an object and returns the Base64-encoded version of that serialized object.

public static String

Returns:

The Base64-encoded object
encodeObject
(Serializable
The 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.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait