Class BaseNCodec
java.lang.Object
org.jose4j.base64url.internal.apache.commons.codec.binary.BaseNCodec
- Direct Known Subclasses:
Base64
Abstract superclass for Base-N encoders and decoders.
This class is thread-safe.
- Version:
- $Id: BaseNCodec.java 1465182 2013-04-06 04:03:12Z ggregory $
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static classHolds thread context so classes can be thread-safe. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final intSize of chunk separator.private static final intprivate static final intDefines the default buffer size - currently 8192 - must be large enough for at least one encoded block+separatorprivate final intNumber of bytes in each full block of encoded data, e.g.(package private) static final intEOFprotected final intChunksize for encoding.protected static final intMask used to extract 8 bits, used in decoding bytesstatic final intMIME chunk size per RFC 2045 section 6.8.protected final byteprotected static final byteByte used to pad output.static final intPEM chunk size per RFC 1421 section 4.3.2.4.private final intNumber of bytes in each full block of unencoded data, e.g. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedBaseNCodec(int unencodedBlockSize, int encodedBlockSize, int lineLength, int chunkSeparatorLength) NotelineLengthis rounded down to the nearest multiple ofencodedBlockSizeIfchunkSeparatorLengthis zero, then chunking is disabled. -
Method Summary
Modifier and TypeMethodDescription(package private) intavailable(BaseNCodec.Context context) Returns the amount of buffered data available for reading.protected booleancontainsAlphabetOrPad(byte[] arrayOctet) Tests a given byte array to see if it contains any characters within the alphabet or PAD.byte[]decode(byte[] pArray) Decodes a byte[] containing characters in the Base-N alphabet.(package private) abstract voiddecode(byte[] pArray, int i, int length, BaseNCodec.Context context) byte[]Decodes a String containing characters in the Base-N alphabet.byte[]encode(byte[] pArray) Encodes a byte[] containing binary data, into a byte[] containing characters in the alphabet.(package private) abstract voidencode(byte[] pArray, int i, int length, BaseNCodec.Context context) encodeAsString(byte[] pArray) Encodes a byte[] containing binary data, into a String containing characters in the appropriate alphabet.encodeToString(byte[] pArray) Encodes a byte[] containing binary data, into a String containing characters in the Base-N alphabet.protected byte[]ensureBufferSize(int size, BaseNCodec.Context context) Ensure that the buffer has room forsizebytesprotected intGet the default buffer size.longgetEncodedLength(byte[] pArray) Calculates the amount of space needed to encode the supplied array.(package private) booleanhasData(BaseNCodec.Context context) Returns true if this object has buffered data for reading.protected abstract booleanisInAlphabet(byte value) Returns whether or not theoctetis in the current alphabet.booleanisInAlphabet(byte[] arrayOctet, boolean allowWSPad) Tests a given byte array to see if it contains only valid characters within the alphabet.booleanisInAlphabet(String basen) Tests a given String to see if it contains only valid characters within the alphabet.protected static booleanisWhiteSpace(byte byteToCheck) Checks if a byte value is whitespace or not.(package private) intreadResults(byte[] b, int bPos, int bAvail, BaseNCodec.Context context) Extracts buffered data into the provided byte[] array, starting at position bPos, up to a maximum of bAvail bytes.private byte[]resizeBuffer(BaseNCodec.Context context) Increases our buffer by theDEFAULT_BUFFER_RESIZE_FACTOR.
-
Field Details
-
EOF
static final int EOFEOF- Since:
- 1.7
- See Also:
-
MIME_CHUNK_SIZE
public static final int MIME_CHUNK_SIZEMIME 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:
-
PEM_CHUNK_SIZE
public static final int PEM_CHUNK_SIZEPEM chunk size per RFC 1421 section 4.3.2.4.The 64 character limit does not count the trailing CRLF, but counts all other characters, including any equal signs.
-
DEFAULT_BUFFER_RESIZE_FACTOR
private static final int DEFAULT_BUFFER_RESIZE_FACTOR- See Also:
-
DEFAULT_BUFFER_SIZE
private static final int DEFAULT_BUFFER_SIZEDefines the default buffer size - currently 8192 - must be large enough for at least one encoded block+separator- See Also:
-
MASK_8BITS
protected static final int MASK_8BITSMask used to extract 8 bits, used in decoding bytes- See Also:
-
PAD_DEFAULT
protected static final byte PAD_DEFAULTByte used to pad output.- See Also:
-
PAD
protected final byte PAD- See Also:
-
unencodedBlockSize
private final int unencodedBlockSizeNumber of bytes in each full block of unencoded data, e.g. 4 for Base64 and 5 for Base32 -
encodedBlockSize
private final int encodedBlockSizeNumber of bytes in each full block of encoded data, e.g. 3 for Base64 and 8 for Base32 -
lineLength
protected final int lineLengthChunksize for encoding. Not used when decoding. A value of zero or less implies no chunking of the encoded data. Rounded down to nearest multiple of encodedBlockSize. -
chunkSeparatorLength
private final int chunkSeparatorLengthSize of chunk separator. Not used unlesslineLength> 0.
-
-
Constructor Details
-
BaseNCodec
protected BaseNCodec(int unencodedBlockSize, int encodedBlockSize, int lineLength, int chunkSeparatorLength) NotelineLengthis rounded down to the nearest multiple ofencodedBlockSizeIfchunkSeparatorLengthis zero, then chunking is disabled.- Parameters:
unencodedBlockSize- the size of an unencoded block (e.g. Base64 = 3)encodedBlockSize- the size of an encoded block (e.g. Base64 = 4)lineLength- if > 0, use chunking with a lengthlineLengthchunkSeparatorLength- the chunk separator length, if relevant
-
-
Method Details
-
hasData
Returns true if this object has buffered data for reading.- Parameters:
context- the context to be used- Returns:
- true if there is data still available for reading.
-
available
Returns the amount of buffered data available for reading.- Parameters:
context- the context to be used- Returns:
- The amount of buffered data available for reading.
-
getDefaultBufferSize
protected int getDefaultBufferSize()Get the default buffer size. Can be overridden.- Returns:
DEFAULT_BUFFER_SIZE
-
resizeBuffer
Increases our buffer by theDEFAULT_BUFFER_RESIZE_FACTOR.- Parameters:
context- the context to be used
-
ensureBufferSize
Ensure that the buffer has room forsizebytes- Parameters:
size- minimum spare space requiredcontext- the context to be used- Returns:
- bytes
-
readResults
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[] array to extract the buffered data into.bPos- position in byte[] array to start extraction at.bAvail- amount of bytes we're allowed to extract. We may extract fewer (if fewer are available).context- the context to be used- Returns:
- The number of bytes successfully extracted into the provided byte[] array.
-
isWhiteSpace
protected static boolean isWhiteSpace(byte byteToCheck) Checks if a byte value is whitespace or not. Whitespace is taken to mean: space, tab, CR, LF- Parameters:
byteToCheck- the byte to check- Returns:
- true if byte is whitespace, false otherwise
-
encodeToString
Encodes a byte[] containing binary data, into a String containing characters in the Base-N alphabet. Uses UTF8 encoding.- Parameters:
pArray- a byte array containing binary data- Returns:
- A String containing only Base-N character data
-
encodeAsString
Encodes a byte[] containing binary data, into a String containing characters in the appropriate alphabet. Uses UTF8 encoding.- Parameters:
pArray- a byte array containing binary data- Returns:
- String containing only character data in the appropriate alphabet.
-
decode
Decodes a String containing characters in the Base-N alphabet.- Parameters:
pArray- A String containing Base-N character data- Returns:
- a byte array containing binary data
-
decode
public byte[] decode(byte[] pArray) Decodes a byte[] containing characters in the Base-N alphabet.- Parameters:
pArray- A byte array containing Base-N character data- Returns:
- a byte array containing binary data
-
encode
public byte[] encode(byte[] pArray) Encodes a byte[] containing binary data, into a byte[] containing characters in the alphabet.- Parameters:
pArray- a byte array containing binary data- Returns:
- A byte array containing only the basen alphabetic character data
-
encode
-
decode
-
isInAlphabet
protected abstract boolean isInAlphabet(byte value) Returns whether or not theoctetis in the current alphabet. Does not allow whitespace or pad.- Parameters:
value- The value to test- Returns:
trueif the value is defined in the current alphabet,falseotherwise.
-
isInAlphabet
public boolean isInAlphabet(byte[] arrayOctet, boolean allowWSPad) Tests a given byte array to see if it contains only valid characters within the alphabet. The method optionally treats whitespace and pad as valid.- Parameters:
arrayOctet- byte array to testallowWSPad- iftrue, then whitespace and PAD are also allowed- Returns:
trueif all bytes are valid characters in the alphabet or if the byte array is empty;false, otherwise
-
isInAlphabet
Tests a given String to see if it contains only valid characters within the alphabet. The method treats whitespace and PAD as valid.- Parameters:
basen- String to test- Returns:
trueif all characters in the String are valid characters in the alphabet or if the String is empty;false, otherwise- See Also:
-
containsAlphabetOrPad
protected boolean containsAlphabetOrPad(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 array to test- Returns:
trueif any byte is a valid character in the alphabet or PAD;falseotherwise
-
getEncodedLength
public long getEncodedLength(byte[] pArray) Calculates the amount of space needed to encode the supplied array.- Parameters:
pArray- byte[] array which will later be encoded- Returns:
- amount of space needed to encoded the supplied array. Returns a long since a max-len array will require larger than Integer.MAX_VALUE
-