Top Description Inners Fields Constructors Methods
org.apache.tomcat.util.codec.binary

public abstract Class BaseNCodec

extends Object
Class Inheritance
Known Direct Subclasses
org.apache.tomcat.util.codec.binary.Base64
Imports
java.util.Arrays, org.apache.tomcat.util.buf.HexUtils, org.apache.tomcat.util.res.StringManager

Abstract superclass for Base-N encoders and decoders.

This class is thread-safe.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
BaseNCodec.Context

Holds thread context so classes can be thread-safe.

Field Summary

Modifier and TypeField and Description
pack-priv static final byte[]
CHUNK_SEPARATOR

Chunk separator per RFC 2045 section 2.1.

private final int
chunkSeparatorLength

Size of chunk separator.

private static final int
private static final int
DEFAULT_BUFFER_SIZE

Defines the default buffer size - currently 128 - must be large enough for at least one encoded block+separator

private final int
encodedBlockSize

Number of bytes in each full block of encoded data, e.g. 3 for Base64 and 8 for Base32

pack-priv static final int
EOF

EOF

protected final int
lineLength

Chunksize for encoding.

protected static final int
MASK_8BITS

Mask used to extract 8 bits, used in decoding bytes

private static final int
MAX_BUFFER_SIZE

The maximum size buffer to allocate.

public static final int
MIME_CHUNK_SIZE

MIME chunk size per RFC 2045 section 6.8.

protected final byte
pad

Pad byte.

protected static final byte
PAD_DEFAULT

Byte used to pad output.

protected static final StringManager
private final int
unencodedBlockSize

Number of bytes in each full block of unencoded data, e.g. 4 for Base64 and 5 for Base32

Constructor Summary

AccessConstructor and Description
protected
BaseNCodec(final int
the size of an unencoded block (e.g. Base64 = 3)
unencodedBlockSize
,
final int
the size of an encoded block (e.g. Base64 = 4)
encodedBlockSize
,
final int
if > 0, use chunking with a length lineLength
lineLength
,
final int
the chunk separator length, if relevant
chunkSeparatorLength
)

Note lineLength is rounded down to the nearest multiple of the encoded block size.

protected
BaseNCodec(final int
the size of an unencoded block (e.g. Base64 = 3)
unencodedBlockSize
,
final int
the size of an encoded block (e.g. Base64 = 4)
encodedBlockSize
,
final int
if > 0, use chunking with a length lineLength
lineLength
,
final int
the chunk separator length, if relevant
chunkSeparatorLength
,
final byte
byte used as padding byte.
pad
)

Note lineLength is rounded down to the nearest multiple of the encoded block size.

Method Summary

Modifier and TypeMethod and Description
pack-priv int

Returns:

The amount of buffered data available for reading.
available
(final BaseNCodec.Context
the context to be used
context
)

Returns the amount of buffered data available for reading.

protected boolean

Returns:

true if any byte is a valid character in the alphabet or PAD; false otherwise
containsAlphabetOrPad
(final byte[]
byte array to test
arrayOctet
)

Tests a given byte array to see if it contains any characters within the alphabet or PAD.

private static int

Returns:

the capacity
createPositiveCapacity
(final int
the minimum capacity
minCapacity
)

Create a positive capacity at least as large the minimum required capacity.

public byte[]

Returns:

a byte array containing binary data
decode
(final byte[]
A byte array containing Base-N character data
pArray
)

Decodes a byte[] containing characters in the Base-N alphabet.

public byte[]
decode(final byte[] pArray, final int off, final int len)

pack-priv abstract void
decode(byte[] pArray, int i, int length, BaseNCodec.Context context)

public byte[]

Returns:

a byte array containing binary data
decode
(final String
A String containing Base-N character data
pArray
)

Decodes a String containing characters in the Base-N alphabet.

public byte[]

Returns:

A byte array containing only the base N alphabetic character data
encode
(final byte[]
a byte array containing binary data
pArray
)

Encodes a byte[] containing binary data, into a byte[] containing characters in the alphabet.

public byte[]

Returns:

A byte array containing only the base N alphabetic character data
encode
(final byte[]
a byte array containing binary data
pArray
,
final int
initial offset of the subarray.
offset
,
final int
length of the subarray.
length
)

Encodes a byte[] containing binary data, into a byte[] containing characters in the alphabet.

pack-priv abstract void
encode(byte[] pArray, int i, int length, BaseNCodec.Context context)

public String

Returns:

String containing only character data in the appropriate alphabet.
encodeAsString
(final byte[]
a byte array containing binary data
pArray
)

Encodes a byte[] containing binary data, into a String containing characters in the appropriate alphabet.

protected byte[]

Returns:

the buffer
ensureBufferSize
(final int
minimum spare space required
size
,
final BaseNCodec.Context
the context to be used
context
)

Ensure that the buffer has room for size bytes

protected int

Returns:

the default buffer size.
getDefaultBufferSize
()

Get the default buffer size.

public long

Returns:

amount of space needed to encode the supplied array. Returns a long since a max-len array will require > Integer.MAX_VALUE
getEncodedLength
(final byte[]
byte[] array which will later be encoded
pArray
)

Calculates the amount of space needed to encode the supplied array.

pack-priv boolean

Returns:

true if there is data still available for reading.
hasData
(final BaseNCodec.Context
the context to be used
context
)

Returns true if this object has buffered data for reading.

protected abstract boolean

Returns:

true if the value is defined in the current alphabet, false otherwise.
isInAlphabet
(byte
The value to test
value
)

Returns whether or not the octet is in the current alphabet.

pack-priv int

Returns:

The number of bytes successfully extracted into the provided byte[] array.
readResults
(final byte[]
byte[] array to extract the buffered data into.
b
,
final int
position in byte[] array to start extraction at.
bPos
,
final int
amount of bytes we're allowed to extract. We may extract fewer (if fewer are available).
bAvail
,
final BaseNCodec.Context
the context to be used
context
)

Extracts buffered data into the provided byte[] array, starting at position bPos, up to a maximum of bAvail bytes.

private static byte[]

Returns:

the resized byte[] buffer
resizeBuffer
(final BaseNCodec.Context
the context to be used
context
,
final int
the minimum required capacity
minCapacity
)

Increases our buffer by the DEFAULT_BUFFER_RESIZE_FACTOR.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CHUNK_SEPARATORback to summary
pack-priv static final byte[] CHUNK_SEPARATOR

Chunk separator per RFC 2045 section 2.1.

See Also
RFC 2045 section 2.1
chunkSeparatorLengthback to summary
private final int chunkSeparatorLength

Size of chunk separator. Not used unless lineLength > 0.

DEFAULT_BUFFER_RESIZE_FACTORback to summary
private static final int DEFAULT_BUFFER_RESIZE_FACTOR
DEFAULT_BUFFER_SIZEback to summary
private static final int DEFAULT_BUFFER_SIZE

Defines the default buffer size - currently 128 - must be large enough for at least one encoded block+separator

encodedBlockSizeback to summary
private final int encodedBlockSize

Number of bytes in each full block of encoded data, e.g. 3 for Base64 and 8 for Base32

EOFback to summary
pack-priv static final int EOF

EOF

Since
1.7
lineLengthback to summary
protected final int lineLength

Chunksize for encoding. Not used when decoding. A value of zero or less implies no chunking of the encoded data. Rounded down to the nearest multiple of encodedBlockSize.

MASK_8BITSback to summary
protected static final int MASK_8BITS

Mask used to extract 8 bits, used in decoding bytes

MAX_BUFFER_SIZEback to summary
private static final int MAX_BUFFER_SIZE

The maximum size buffer to allocate.

This is set to the same size used in the JDK java.util.ArrayList:

Some VMs reserve some header words in an array. Attempts to allocate larger arrays may result in OutOfMemoryError: Requested array size exceeds VM limit.
MIME_CHUNK_SIZEback to summary
public static final int MIME_CHUNK_SIZE

MIME chunk size per RFC 2045 section 6.8.

The 76 character limit does not count the trailing CRLF, but counts all other characters, including any equal signs.

See Also
RFC 2045 section 6.8
padback to summary
protected final byte pad

Pad byte. Instance variable just in case it needs to vary later.

PAD_DEFAULTback to summary
protected static final byte PAD_DEFAULT

Byte used to pad output.

smback to summary
protected static final StringManager sm
unencodedBlockSizeback to summary
private final int unencodedBlockSize

Number of bytes in each full block of unencoded data, e.g. 4 for Base64 and 5 for Base32

Constructor Detail

BaseNCodecback to summary
protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength)

Note lineLength is rounded down to the nearest multiple of the encoded block size. If chunkSeparatorLength is zero, then chunking is disabled.

Parameters
unencodedBlockSize:int

the size of an unencoded block (e.g. Base64 = 3)

encodedBlockSize:int

the size of an encoded block (e.g. Base64 = 4)

lineLength:int

if > 0, use chunking with a length lineLength

chunkSeparatorLength:int

the chunk separator length, if relevant

BaseNCodecback to summary
protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength, final byte pad)

Note lineLength is rounded down to the nearest multiple of the encoded block size. If chunkSeparatorLength is zero, then chunking is disabled.

Parameters
unencodedBlockSize:int

the size of an unencoded block (e.g. Base64 = 3)

encodedBlockSize:int

the size of an encoded block (e.g. Base64 = 4)

lineLength:int

if > 0, use chunking with a length lineLength

chunkSeparatorLength:int

the chunk separator length, if relevant

pad:byte

byte used as padding byte.

Method Detail

availableback to summary
pack-priv int available(final BaseNCodec.Context context)

Returns the amount of buffered data available for reading.

Parameters
context:BaseNCodec.Context

the context to be used

Returns:int

The amount of buffered data available for reading.

containsAlphabetOrPadback to summary
protected boolean containsAlphabetOrPad(final byte[] arrayOctet)

Tests a given byte array to see if it contains any characters within the alphabet or PAD. Intended for use in checking line-ending arrays

Parameters
arrayOctet:byte[]

byte array to test

Returns:boolean

true if any byte is a valid character in the alphabet or PAD; false otherwise

createPositiveCapacityback to summary
private static int createPositiveCapacity(final int minCapacity)

Create a positive capacity at least as large the minimum required capacity. If the minimum capacity is negative then this throws an OutOfMemoryError as no array can be allocated.

Parameters
minCapacity:int

the minimum capacity

Returns:int

the capacity

Exceptions
OutOfMemoryError:
if the minCapacity is negative
decodeback to summary
public byte[] decode(final byte[] pArray)

Decodes a byte[] containing characters in the Base-N alphabet.

Parameters
pArray:byte[]

A byte array containing Base-N character data

Returns:byte[]

a byte array containing binary data

decodeback to summary
public byte[] decode(final byte[] pArray, final int off, final int len)
decodeback to summary
pack-priv abstract void decode(byte[] pArray, int i, int length, BaseNCodec.Context context)
decodeback to summary
public byte[] decode(final String pArray)

Decodes a String containing characters in the Base-N alphabet.

Parameters
pArray:String

A String containing Base-N character data

Returns:byte[]

a byte array containing binary data

encodeback to summary
public byte[] encode(final byte[] pArray)

Encodes a byte[] containing binary data, into a byte[] containing characters in the alphabet.

Parameters
pArray:byte[]

a byte array containing binary data

Returns:byte[]

A byte array containing only the base N alphabetic character data

encodeback to summary
public byte[] encode(final byte[] pArray, final int offset, final int length)

Encodes a byte[] containing binary data, into a byte[] containing characters in the alphabet.

Parameters
pArray:byte[]

a byte array containing binary data

offset:int

initial offset of the subarray.

length:int

length of the subarray.

Returns:byte[]

A byte array containing only the base N alphabetic character data

Since
1.11
encodeback to summary
pack-priv abstract void encode(byte[] pArray, int i, int length, BaseNCodec.Context context)
encodeAsStringback to summary
public String encodeAsString(final byte[] pArray)

Encodes a byte[] containing binary data, into a String containing characters in the appropriate alphabet. Uses UTF8 encoding.

Parameters
pArray:byte[]

a byte array containing binary data

Returns:String

String containing only character data in the appropriate alphabet.

Since
1.5
ensureBufferSizeback to summary
protected byte[] ensureBufferSize(final int size, final BaseNCodec.Context context)

Ensure that the buffer has room for size bytes

Parameters
size:int

minimum spare space required

context:BaseNCodec.Context

the context to be used

Returns:byte[]

the buffer

getDefaultBufferSizeback to summary
protected int getDefaultBufferSize()

Get the default buffer size. Can be overridden.

Returns:int

the default buffer size.

getEncodedLengthback to summary
public long getEncodedLength(final byte[] pArray)

Calculates the amount of space needed to encode the supplied array.

Parameters
pArray:byte[]

byte[] array which will later be encoded

Returns:long

amount of space needed to encode the supplied array. Returns a long since a max-len array will require > Integer.MAX_VALUE

hasDataback to summary
pack-priv boolean hasData(final BaseNCodec.Context context)

Returns true if this object has buffered data for reading.

Parameters
context:BaseNCodec.Context

the context to be used

Returns:boolean

true if there is data still available for reading.

isInAlphabetback to summary
protected abstract boolean isInAlphabet(byte value)

Returns whether or not the octet is in the current alphabet. Does not allow whitespace or pad.

Parameters
value:byte

The value to test

Returns:boolean

true if the value is defined in the current alphabet, false otherwise.

readResultsback to summary
pack-priv int readResults(final byte[] b, final int bPos, final int bAvail, final BaseNCodec.Context context)

Extracts buffered data into the provided byte[] array, starting at position bPos, up to a maximum of bAvail bytes. Returns how many bytes were actually extracted.

Package protected for access from I/O streams.

Parameters
b:byte[]

byte[] array to extract the buffered data into.

bPos:int

position in byte[] array to start extraction at.

bAvail:int

amount of bytes we're allowed to extract. We may extract fewer (if fewer are available).

context:BaseNCodec.Context

the context to be used

Returns:int

The number of bytes successfully extracted into the provided byte[] array.

resizeBufferback to summary
private static byte[] resizeBuffer(final BaseNCodec.Context context, final int minCapacity)

Increases our buffer by the DEFAULT_BUFFER_RESIZE_FACTOR.

Parameters
context:BaseNCodec.Context

the context to be used

minCapacity:int

the minimum required capacity

Returns:byte[]

the resized byte[] buffer

Exceptions
OutOfMemoryError:
if the minCapacity is negative
org.apache.tomcat.util.codec.binary back to summary

pack-priv Class BaseNCodec.Context

extends Object
Class Inheritance

Holds thread context so classes can be thread-safe. This class is not itself thread-safe; each thread must allocate its own copy.
Since
1.7

Field Summary

Modifier and TypeField and Description
pack-priv byte[]
buffer

Buffer for streaming.

pack-priv int
currentLinePos

Variable tracks how many characters have been written to the current line.

pack-priv boolean
eof

Boolean flag to indicate the EOF has been reached.

pack-priv int
ibitWorkArea

Placeholder for the bytes we're dealing with for our based logic.

pack-priv int
modulus

Writes to the buffer only occur after every 3/5 reads when encoding, and every 4/8 reads when decoding.

pack-priv int
pos

Position where next character should be written in the buffer.

pack-priv int
readPos

Position where next character should be read from the buffer.

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

a String useful for debugging.
toString
()

Overrides java.lang.Object.toString.

Returns a String useful for debugging (especially within a debugger.)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

bufferback to summary
pack-priv byte[] buffer

Buffer for streaming.

currentLinePosback to summary
pack-priv int currentLinePos

Variable tracks how many characters have been written to the current line. Only used when encoding. We use it to make sure each encoded line never goes beyond lineLength (if lineLength > 0).

eofback to summary
pack-priv boolean eof

Boolean flag to indicate the EOF has been reached. Once EOF has been reached, this object becomes useless, and must be thrown away.

ibitWorkAreaback to summary
pack-priv int ibitWorkArea

Placeholder for the bytes we're dealing with for our based logic. Bitwise operations store and extract the encoding or decoding from this variable.

modulusback to summary
pack-priv int modulus

Writes to the buffer only occur after every 3/5 reads when encoding, and every 4/8 reads when decoding. This variable helps track that.

posback to summary
pack-priv int pos

Position where next character should be written in the buffer.

readPosback to summary
pack-priv int readPos

Position where next character should be read from the buffer.

Constructor Detail

Contextback to summary
pack-priv Context()

Method Detail

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a String useful for debugging (especially within a debugger.)

Returns:String

a String useful for debugging.

Annotations
@SuppressWarnings:boxing
@Override