18 package org.apache.commons.codec.binary;
98 @SuppressWarnings(
"boxing")
100 public String toString() {
101 return String.format(
"%s[buffer=%s, currentLinePos=%s, eof=%s, ibitWorkArea=%s, lbitWorkArea=%s, " +
102 "modulus=%s, pos=%s, readPos=%s]", this.getClass().getSimpleName(), buffer, currentLinePos, eof,
103 ibitWorkArea, lbitWorkArea, modulus, pos, readPos);
112 static final int EOF = -1;
182 protected BaseNCodec(
final int unencodedBlockSize,
final int encodedBlockSize,
183 final int lineLength,
final int chunkSeparatorLength) {
186 final boolean useChunking = lineLength > 0 && chunkSeparatorLength > 0;
187 this.lineLength = useChunking ? (lineLength /
encodedBlockSize) * encodedBlockSize : 0;
198 return context.
buffer != null;
225 if (context.
buffer == null) {
231 System.arraycopy(context.
buffer, 0, b, 0, context.
buffer.length);
244 if ((context.
buffer == null) || (context.
buffer.length < context.
pos + size)){
267 if (context.
buffer != null) {
268 final int len = Math.min(
available(context), bAvail);
269 System.arraycopy(context.
buffer, context.
readPos, b, bPos, len);
276 return context.
eof ? EOF : 0;
287 switch (byteToCheck) {
310 if (!(obj instanceof byte[])) {
311 throw new EncoderException(
"Parameter supplied to Base-N encode is not a byte[]");
313 return encode((byte[]) obj);
352 if (obj instanceof byte[]) {
353 return decode((byte[]) obj);
354 }
else if (obj instanceof String) {
355 return decode((String) obj);
357 throw new DecoderException(
"Parameter supplied to Base-N decode is not a byte[] or a String");
368 public byte[]
decode(
final String pArray) {
380 public byte[]
decode(
final byte[] pArray) {
381 if (pArray == null || pArray.length == 0) {
385 decode(pArray, 0, pArray.length, context);
386 decode(pArray, 0, EOF, context);
387 final byte[]
result =
new byte[context.
pos];
400 public byte[]
encode(
final byte[] pArray) {
401 if (pArray == null || pArray.length == 0) {
405 encode(pArray, 0, pArray.length, context);
406 encode(pArray, 0, EOF, context);
407 final byte[] buf =
new byte[context.
pos - context.
readPos];
413 abstract void encode(byte[] pArray,
int i,
int length, Context context);
416 abstract void decode(byte[] pArray,
int i,
int length, Context context);
438 public boolean isInAlphabet(
final byte[] arrayOctet,
final boolean allowWSPad) {
439 for (
int i = 0; i < arrayOctet.length; i++) {
441 (!allowWSPad || (arrayOctet[i] != PAD) && !
isWhiteSpace(arrayOctet[i]))) {
471 if (arrayOctet == null) {
474 for (
final byte element : arrayOctet) {
493 long len = ((pArray.length + unencodedBlockSize-1) / unencodedBlockSize) * (long) encodedBlockSize;
494 if (lineLength > 0) {
Object decode(final Object obj)
int available(final Context context)
boolean isInAlphabet(final byte[] arrayOctet, final boolean allowWSPad)
String encodeToString(final byte[] pArray)
static final int DEFAULT_BUFFER_SIZE
long getEncodedLength(final byte[] pArray)
int readResults(final byte[] b, final int bPos, final int bAvail, final Context context)
byte[] resizeBuffer(final Context context)
BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength)
boolean hasData(final Context context)
boolean containsAlphabetOrPad(final byte[] arrayOctet)
static final int DEFAULT_BUFFER_RESIZE_FACTOR
final int unencodedBlockSize
static final int MASK_8BITS
int getDefaultBufferSize()
static boolean isWhiteSpace(final byte byteToCheck)
static final int PEM_CHUNK_SIZE
static final byte PAD_DEFAULT
Object encode(final Object obj)
byte[] encode(final byte[] pArray)
final int chunkSeparatorLength
abstract boolean isInAlphabet(byte value)
byte[] decode(final String pArray)
static String newStringUtf8(final byte[] bytes)
static byte[] getBytesUtf8(final String string)
boolean isInAlphabet(final String basen)
byte[] ensureBufferSize(final int size, final Context context)
static final int MIME_CHUNK_SIZE
String encodeAsString(final byte[] pArray)
byte[] decode(final byte[] pArray)
final int encodedBlockSize