Top Description Inners Fields Constructors Methods
org.keycloak.common.util

public Class Base64

extends Object
Class Inheritance
Imports
java.io.IOException

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
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

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
decode options such as URL_SAFE or GUNZIP
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
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 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.

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.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

_ORDERED_ALPHABETback 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_DECODABETback to summary
private static final byte[] _ORDERED_DECODABET

Used in decoding the "ordered" dialect of Base64.

_STANDARD_ALPHABETback to summary
private static final byte[] _STANDARD_ALPHABET

The 64 valid Base64 values.

_STANDARD_DECODABETback 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_ALPHABETback 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_DECODABETback to summary
private static final byte[] _URL_SAFE_DECODABET

Used in decoding URL- and Filename-safe dialects of Base64.

DECODEback to summary
public static final int DECODE

Specify decoding in first bit. Value is zero.

DO_BREAK_LINESback to summary
public static final int DO_BREAK_LINES

Do break lines when encoding. Value is 8.

ENCODEback to summary
public static final int ENCODE

Specify encoding in first bit. Value is one.

EQUALS_SIGNback to summary
private static final byte EQUALS_SIGN

The equals sign (=) as a byte.

EQUALS_SIGN_ENCback to summary
private static final byte EQUALS_SIGN_ENC
GUNZIPback to summary
public static final int GUNZIP

Specify that data should be gunzipped.

GZIPback to summary
public static final int GZIP

Specify that data should be gzip-compressed in second bit. Value is two.

MAX_LINE_LENGTHback to summary
private static final int MAX_LINE_LENGTH

Maximum line length (76) of Base64 output.

NEW_LINEback to summary
private static final byte NEW_LINE

The new line character (\n) as a byte.

NO_OPTIONSback to summary
public static final int NO_OPTIONS

No options specified. Value is zero.

ORDEREDback 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_ENCODINGback to summary
private static final String PREFERRED_ENCODING

Preferred encoding.

URL_SAFEback 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_ENCback to summary
private static final byte WHITE_SPACE_ENC

Constructor Detail

Base64back to summary
private Base64()

Defeats instantiation.

Method Detail

decodeback 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.

Parameters
source:byte[]

The Base64 encoded data

Returns:byte[]

decoded data

Since
2.3.1
decodeback 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.

Parameters
source:byte[]

The Base64 encoded data

off:int

The offset of where to begin decoding

len:int

The length of characters to decode

options:int

Can specify options such as alphabet type to use

Returns:byte[]

decoded data

Exceptions
IOException:
If bogus characters exist in source data
Since
1.3
decodeback to summary
public static byte[] decode(String s) throws IOException

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

Parameters
s:String

the string to decode

Returns:byte[]

the decoded data

Exceptions
IOException:
If there is a problem
Since
1.4
decodeback 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.

Parameters
s:String

the string to decode

options:int

decode options such as URL_SAFE or GUNZIP

Returns:byte[]

the decoded data

Exceptions
IOException:
if there is an error (invalid character in source string or gunzip error)
NullPointerException:
if s is null
Since
1.4
decode4to3back 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.

Parameters
source:byte[]

the array to convert

srcOffset:int

the index where conversion begins

destination:byte[]

the array to hold the conversion

destOffset:int

the index where output will be put

options:int

alphabet type is pulled from this (standard, url-safe, ordered)

Returns:int

the number of decoded bytes converted

Exceptions
NullPointerException:
if source or destination arrays are null
IllegalArgumentException:
if srcOffset or destOffset are invalid or there is not enough room in the array.
Since
1.3
encodeback to summary
public static void encode(ByteBuffer raw, ByteBuffer encoded)

Performs Base64 encoding on the raw ByteBuffer, writing it to the encoded ByteBuffer. This is an experimental feature. Currently it does not pass along any options (such as DO_BREAK_LINES or GZIP).

Parameters
raw:ByteBuffer

input buffer

encoded:ByteBuffer

output buffer

Since
2.3
encodeback to summary
public static void encode(ByteBuffer raw, CharBuffer encoded)

Performs Base64 encoding on the raw ByteBuffer, writing it to the encoded CharBuffer. This is an experimental feature. Currently it does not pass along any options (such as DO_BREAK_LINES or GZIP.

Parameters
raw:ByteBuffer

input buffer

encoded:CharBuffer

output buffer

Since
2.3
encode3to4back 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.

Parameters
b4:byte[]

A reusable byte array to reduce array instantiation

threeBytes:byte[]

the array to convert

numSigBytes:int

the number of significant bytes in your array

Returns:byte[]

four byte array in Base64 notation.

Since
1.5.1
encode3to4back 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.

Parameters
source:byte[]

the array to convert

srcOffset:int

the index where conversion begins

numSigBytes:int

the number of significant bytes in your array

destination:byte[]

the array to hold the conversion

destOffset:int

the index where output will be put

Returns:byte[]

the destination array

Since
1.3
encodeBytesback to summary
public static String encodeBytes(byte[] source)

Encodes a byte array into Base64 notation. Does not GZip-compress data.

Parameters
source:byte[]

The data to convert

Returns:String

The data in Base64-encoded form

Exceptions
NullPointerException:
if source array is null
Since
1.4
encodeBytesback 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: encodeBytes( myData, Base64.GZIP ) or

Example: encodeBytes( myData, Base64.GZIP | Base64.DO_BREAK_LINES )

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.

Parameters
source:byte[]

The data to convert

options:int

Specified options

Returns:String

The Base64-encoded data as a String

Exceptions
IOException:
if there is an error
NullPointerException:
if source array is null
Since
2.0
See Also
Base64#GZIP, Base64#DO_BREAK_LINES
encodeBytesback 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.

Parameters
source:byte[]

The data to convert

off:int

Offset in array where conversion should begin

len:int

Length of data to convert

Returns:String

The Base64-encoded data as a String

Exceptions
NullPointerException:
if source array is null
IllegalArgumentException:
if source array, offset, or length are invalid
Since
1.4
encodeBytesback 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: encodeBytes( myData, Base64.GZIP ) or

Example: encodeBytes( myData, Base64.GZIP | Base64.DO_BREAK_LINES )

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.

Parameters
source:byte[]

The data to convert

off:int

Offset in array where conversion should begin

len:int

Length of data to convert

options:int

Specified options

Returns:String

The Base64-encoded data as a String

Exceptions
IOException:
if there is an error
NullPointerException:
if source array is null
IllegalArgumentException:
if source array, offset, or length are invalid
Since
2.0
See Also
Base64#GZIP, Base64#DO_BREAK_LINES
encodeBytesToBytesback to summary
public static byte[] encodeBytesToBytes(byte[] source)

Similar to encodeBytes(byte[]) but returns a byte array instead of instantiating a String. This is more efficient if you're working with I/O streams and have large data sets to encode.

Parameters
source:byte[]

The data to convert

Returns:byte[]

The Base64-encoded data as a byte[] (of ASCII characters)

Exceptions
NullPointerException:
if source array is null
Since
2.3.1
encodeBytesToBytesback to summary
public static byte[] encodeBytesToBytes(byte[] source, int off, int len, int options) throws IOException

Similar to encodeBytes(byte[], int, int, int) but returns a byte array instead of instantiating a String. This is more efficient if you're working with I/O streams and have large data sets to encode.

Parameters
source:byte[]

The data to convert

off:int

Offset in array where conversion should begin

len:int

Length of data to convert

options:int

Specified options

Returns:byte[]

The Base64-encoded data as a String

Exceptions
IOException:
if there is an error
NullPointerException:
if source array is null
IllegalArgumentException:
if source array, offset, or length are invalid
Since
2.3.1
See Also
Base64#GZIP, Base64#DO_BREAK_LINES
encodeObjectback 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.
Parameters
serializableObject:Serializable

The object to encode

Returns:String

The Base64-encoded object

Exceptions
IOException:
if there is an error
NullPointerException:
if serializedObject is null
Since
1.4
encodeObjectback 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: encodeObject( myObj, Base64.GZIP ) or

Example: encodeObject( myObj, Base64.GZIP | Base64.DO_BREAK_LINES )

Parameters
serializableObject:Serializable

The object to encode

options:int

Specified options

Returns:String

The Base64-encoded object

Exceptions
IOException:
if there is an error
Since
2.0
See Also
Base64#GZIP, Base64#DO_BREAK_LINES
getAlphabetback 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.

getDecodabetback 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.

org.keycloak.common.util back to summary

public Class Base64.InputStream

extends FilterInputStream
Class Inheritance

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.
Since
1.3
See Also
Base64

Field Summary

Modifier and TypeField and Description
private boolean
private byte[]
private int
private byte[]
private boolean
private int
private int
private int
private int
Inherited from java.io.FilterInputStream:
in

Constructor Summary

AccessConstructor and Description
public
InputStream(InputStream
the java.io.InputStream from which to read data.
in
)

Constructs a Base64.InputStream in DECODE mode.

public
InputStream(InputStream
the java.io.InputStream from which to read data.
in
,
int
Specified options
options
)

Constructs a Base64.InputStream in either ENCODE or DECODE mode.

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

next byte
read
()

Overrides java.io.FilterInputStream.read.

Reads enough of the input stream to convert to/from Base64 and returns the next byte.
public int

Returns:

bytes read into array or -1 if end of stream is encountered.
read
(byte[]
array to hold values
dest
,
int
offset for array
off
,
int
max number of bytes to read into array
len
)

Overrides java.io.FilterInputStream.read.

Calls read() repeatedly until the end of stream is reached or len bytes are read.
Inherited from java.io.FilterInputStream:
availableclosemarkmarkSupportedreadresetskip

Field Detail

breakLinesback to summary
private boolean breakLines
bufferback to summary
private byte[] buffer
bufferLengthback to summary
private int bufferLength
decodabetback to summary
private byte[] decodabet
encodeback to summary
private boolean encode
lineLengthback to summary
private int lineLength
numSigBytesback to summary
private int numSigBytes
optionsback to summary
private int options
positionback to summary
private int position

Constructor Detail

InputStreamback to summary
public InputStream(InputStream in)

Constructs a Base64.InputStream in DECODE mode.

Parameters
in:InputStream

the java.io.InputStream from which to read data.

Since
1.3
InputStreamback to summary
public InputStream(InputStream in, int options)

Constructs a Base64.InputStream in either ENCODE or DECODE mode.

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: new Base64.InputStream( in, Base64.DECODE )

Parameters
in:InputStream

the java.io.InputStream from which to read data.

options:int

Specified options

Since
2.0
See Also
Base64#ENCODE, Base64#DECODE, Base64#DO_BREAK_LINES

Method Detail

readback to summary
public int read() throws IOException

Overrides java.io.FilterInputStream.read.

Reads enough of the input stream to convert to/from Base64 and returns the next byte.

Returns:int

next byte

Annotations
@Override
Exceptions
IOException:

Doc from java.io.FilterInputStream.read.

if an I/O error occurs.

Since
1.3
readback to summary
public int read(byte[] dest, int off, int len) throws IOException

Overrides java.io.FilterInputStream.read.

Calls read() repeatedly until the end of stream is reached or len bytes are read. Returns number of bytes read into array or -1 if end of stream is encountered.

Parameters
dest:byte[]

array to hold values

off:int

offset for array

len:int

max number of bytes to read into array

Returns:int

bytes read into array or -1 if end of stream is encountered.

Annotations
@Override
Exceptions
IOException:

Doc from java.io.FilterInputStream.read.

if an I/O error occurs.

Since
1.3
org.keycloak.common.util back to summary

public Class Base64.OutputStream

extends FilterOutputStream
Class Inheritance

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.
Since
1.3
See Also
Base64

Field Summary

Modifier and TypeField and Description
private byte[]
private boolean
private byte[]
private int
private byte[]
private boolean
private int
private int
private int
private boolean
Inherited from java.io.FilterOutputStream:
out

Constructor Summary

AccessConstructor and Description
public
OutputStream(OutputStream
the java.io.OutputStream to which data will be written.
out
)

Constructs a Base64.OutputStream in ENCODE mode.

public
OutputStream(OutputStream
the java.io.OutputStream to which data will be written.
out
,
int
Specified options.
options
)

Constructs a Base64.OutputStream in either ENCODE or DECODE mode.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides java.io.FilterOutputStream.close.

Implements java.io.Closeable.close.

Flushes and closes (I think, in the superclass) the stream.
public void
flushBase64()

Method added by PHIL.

public void
resumeEncoding()

Resumes encoding of the stream.

public void
suspendEncoding()

Suspends encoding of the stream.

public void
write(int
the byte to write
theByte
)

Overrides java.io.FilterOutputStream.write.

Writes the byte to the output stream after converting to/from Base64 notation.
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.io.FilterOutputStream.write.

Calls write(int) repeatedly until len bytes are written.
Inherited from java.io.FilterOutputStream:
flushwrite

Field Detail

b4back to summary
private byte[] b4
breakLinesback to summary
private boolean breakLines
bufferback to summary
private byte[] buffer
bufferLengthback to summary
private int bufferLength
decodabetback to summary
private byte[] decodabet
encodeback to summary
private boolean encode
lineLengthback to summary
private int lineLength
optionsback to summary
private int options
positionback to summary
private int position
suspendEncodingback to summary
private boolean suspendEncoding

Constructor Detail

OutputStreamback to summary
public OutputStream(OutputStream out)

Constructs a Base64.OutputStream in ENCODE mode.

Parameters
out:OutputStream

the java.io.OutputStream to which data will be written.

Since
1.3
OutputStreamback to summary
public OutputStream(OutputStream out, int options)

Constructs a Base64.OutputStream in either ENCODE or DECODE mode.

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: new Base64.OutputStream( out, Base64.ENCODE )

Parameters
out:OutputStream

the java.io.OutputStream to which data will be written.

options:int

Specified options.

Since
1.3
See Also
Base64#ENCODE, Base64#DECODE, Base64#DO_BREAK_LINES

Method Detail

closeback to summary
public void close() throws IOException

Overrides java.io.FilterOutputStream.close.

Implements java.io.Closeable.close.

Flushes and closes (I think, in the superclass) the stream.

Annotations
@Override
Exceptions
IOException:

Doc from java.io.FilterOutputStream.close.

if an I/O error occurs.

Since
1.3
flushBase64back to summary
public void flushBase64() throws IOException

Method added by PHIL. [Thanks, PHIL. -Rob] This pads the buffer without closing the stream.

Exceptions
IOException:
if there's an error.
resumeEncodingback 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.

Since
1.5.1
suspendEncodingback 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.

Exceptions
IOException:
if there's an error flushing
Since
1.5.1
writeback to summary
public void write(int theByte) throws IOException

Overrides java.io.FilterOutputStream.write.

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.

Parameters
theByte:int

the byte to write

Annotations
@Override
Exceptions
IOException:

Doc from java.io.FilterOutputStream.write.

if an I/O error occurs.

Since
1.3
writeback to summary
public void write(byte[] theBytes, int off, int len) throws IOException

Overrides java.io.FilterOutputStream.write.

Calls write(int) repeatedly until len bytes are written.

Parameters
theBytes:byte[]

array from which to read bytes

off:int

offset for array

len:int

max number of bytes to read into array

Annotations
@Override
Exceptions
IOException:

Doc from java.io.FilterOutputStream.write.

if an I/O error occurs.

Since
1.3