Top Description Fields Constructors Methods
com.thoughtworks.xstream.core.util

public Class Base64Encoder

extends Object
implements StringCodec
Class Inheritance
All Implemented Interfaces
com.thoughtworks.xstream.core.StringCodec
Imports
java.io.ByteArrayOutputStream, .IOException, .Reader, .StringReader, com.thoughtworks.xstream.core.StringCodec

Encodes binary data to plain text as Base64.

Despite there being a gazillion other Base64 implementations out there, this has been written as part of XStream as it forms a core part but is too trivial to warrant an extra dependency. Recent Java Runtimes (since Java 6) provide an own Base64 codec though.

By default it will not insert line breaks to support Base64 values also as attribute values. However, the standard as described in RFC 1521, section 5.2 requires line breaks, allowing other Base64 tools to manipulate the data. You can configure the Base64Encoder to be RFC compliant.

Author
Joe Walnes

Field Summary

Modifier and TypeField and Description
private final boolean
private static final int[]
private static final char[]

Constructor Summary

AccessConstructor and Description
public
Base64Encoder()

Constructs a Base64Encoder.

public
Base64Encoder(final boolean
flag to insert line breaks
lineBreaks
)

Constructs a Base64Encoder.

Method Summary

Modifier and TypeMethod and Description
pack-priv int
computeResultingStringSize(final byte[] input)

public byte[]
decode(final String
the encoded string
input
)

Implements com.thoughtworks.xstream.core.StringCodec.decode.

Decode the provided encoded string.
public String
encode(final byte[]
the data to encode
input
)

Implements com.thoughtworks.xstream.core.StringCodec.encode.

Encode the provided data.
private int
mapCharToInt(final Reader input)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

lineBreaksback to summary
private final boolean lineBreaks
REVERSE_MAPPINGback to summary
private static final int[] REVERSE_MAPPING
SIXTY_FOUR_CHARSback to summary
private static final char[] SIXTY_FOUR_CHARS

Constructor Detail

Base64Encoderback to summary
public Base64Encoder()

Constructs a Base64Encoder.

The encoder will not insert any line breaks.

Since
1.4.11
Base64Encoderback to summary
public Base64Encoder(final boolean lineBreaks)

Constructs a Base64Encoder.

Parameters
lineBreaks:boolean

flag to insert line breaks

Since
1.4.11

Method Detail

computeResultingStringSizeback to summary
pack-priv int computeResultingStringSize(final byte[] input)
decodeback to summary
public byte[] decode(final String input)

Implements com.thoughtworks.xstream.core.StringCodec.decode.

Doc from com.thoughtworks.xstream.core.StringCodec.decode.

Decode the provided encoded string.

Parameters
input:String

the encoded string

Returns:byte[]

the decoded data

encodeback to summary
public String encode(final byte[] input)

Implements com.thoughtworks.xstream.core.StringCodec.encode.

Doc from com.thoughtworks.xstream.core.StringCodec.encode.

Encode the provided data.

Parameters
input:byte[]

the data to encode

Returns:String

the data encoded as string

mapCharToIntback to summary
private int mapCharToInt(final Reader input) throws IOException