18 package org.apache.commons.codec.binary;
20 import java.math.BigInteger;
81 'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
82 'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
83 'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
84 'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
85 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'+',
'/'
94 'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
95 'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
96 'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
97 'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
98 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'-',
'_'
113 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
114 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
115 -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63, 52, 53, 54,
116 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4,
117 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
118 24, 25, -1, -1, -1, -1, 63, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34,
119 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
270 super(BYTES_PER_UNENCODED_BLOCK, BYTES_PER_ENCODED_BLOCK,
272 lineSeparator == null ? 0 : lineSeparator.length);
275 if (lineSeparator != null) {
278 throw new IllegalArgumentException(
"lineSeparator must not contain base64 characters: [" + sep +
"]");
281 this.encodeSize = BYTES_PER_ENCODED_BLOCK + lineSeparator.length;
282 this.lineSeparator =
new byte[lineSeparator.length];
283 System.arraycopy(lineSeparator, 0, this.lineSeparator, 0, lineSeparator.length);
286 this.lineSeparator = null;
290 this.lineSeparator = null;
292 this.decodeSize = this.encodeSize - 1;
328 void encode(
final byte[] in,
int inPos,
final int inAvail,
final Context context) {
340 final int savedPos = context.
pos;
346 buffer[context.
pos++] = encodeTable[(context.
ibitWorkArea >> 2) & MASK_6BITS];
348 buffer[context.
pos++] = encodeTable[(context.
ibitWorkArea << 4) & MASK_6BITS];
350 if (encodeTable == STANDARD_ENCODE_TABLE) {
351 buffer[context.
pos++] =
PAD;
352 buffer[context.
pos++] =
PAD;
357 buffer[context.
pos++] = encodeTable[(context.
ibitWorkArea >> 10) & MASK_6BITS];
358 buffer[context.
pos++] = encodeTable[(context.
ibitWorkArea >> 4) & MASK_6BITS];
359 buffer[context.
pos++] = encodeTable[(context.
ibitWorkArea << 2) & MASK_6BITS];
361 if (encodeTable == STANDARD_ENCODE_TABLE) {
362 buffer[context.
pos++] =
PAD;
366 throw new IllegalStateException(
"Impossible modulus "+context.
modulus);
371 System.arraycopy(lineSeparator, 0, buffer, context.
pos, lineSeparator.length);
372 context.
pos += lineSeparator.length;
375 for (
int i = 0; i < inAvail; i++) {
377 context.
modulus = (context.
modulus+1) % BYTES_PER_UNENCODED_BLOCK;
384 buffer[context.
pos++] = encodeTable[(context.
ibitWorkArea >> 18) & MASK_6BITS];
385 buffer[context.
pos++] = encodeTable[(context.
ibitWorkArea >> 12) & MASK_6BITS];
386 buffer[context.
pos++] = encodeTable[(context.
ibitWorkArea >> 6) & MASK_6BITS];
390 System.arraycopy(lineSeparator, 0, buffer, context.
pos, lineSeparator.length);
391 context.
pos += lineSeparator.length;
425 void decode(
final byte[] in,
int inPos,
final int inAvail,
final Context context) {
432 for (
int i = 0; i < inAvail; i++) {
434 final byte b = in[inPos++];
440 if (b >= 0 && b < DECODE_TABLE.length) {
441 final int result = DECODE_TABLE[b];
478 throw new IllegalStateException(
"Impossible modulus "+context.
modulus);
507 return octet ==
PAD_DEFAULT || (octet >= 0 && octet < DECODE_TABLE.length && DECODE_TABLE[octet] != -1);
520 public static boolean isBase64(
final String base64) {
534 public static boolean isBase64(
final byte[] arrayOctet) {
535 for (
int i = 0; i < arrayOctet.length; i++) {
617 public static byte[]
encodeBase64(
final byte[] binaryData,
final boolean isChunked) {
636 public static byte[]
encodeBase64(
final byte[] binaryData,
final boolean isChunked,
final boolean urlSafe) {
637 return encodeBase64(binaryData, isChunked, urlSafe, Integer.MAX_VALUE);
657 public static byte[]
encodeBase64(
final byte[] binaryData,
final boolean isChunked,
658 final boolean urlSafe,
final int maxResultSize) {
659 if (binaryData == null || binaryData.length == 0) {
665 final Base64 b64 = isChunked ?
new Base64(urlSafe) :
new Base64(0, CHUNK_SEPARATOR, urlSafe);
667 if (len > maxResultSize) {
668 throw new IllegalArgumentException(
"Input array too big, the output array would be bigger (" +
670 ") than the specified maximum size of " +
674 return b64.
encode(binaryData);
686 return new Base64().decode(base64String);
697 return new Base64().decode(base64Data);
726 if (bigInt == null) {
727 throw new NullPointerException(
"encodeInteger called with null parameter");
740 int bitlen = bigInt.bitLength();
742 bitlen = ((bitlen + 7) >> 3) << 3;
743 final byte[] bigBytes = bigInt.toByteArray();
745 if (((bigInt.bitLength() % 8) != 0) && (((bigInt.bitLength() / 8) + 1) == (bitlen / 8))) {
750 int len = bigBytes.length;
753 if ((bigInt.bitLength() % 8) == 0) {
757 final int startDst = bitlen / 8 - len;
758 final byte[] resizedBytes =
new byte[bitlen / 8];
759 System.arraycopy(bigBytes, startSrc, resizedBytes, startDst, len);
772 return octet >= 0 && octet < decodeTable.length && decodeTable[octet] != -1;
Base64(final int lineLength)
static final int BYTES_PER_UNENCODED_BLOCK
long getEncodedLength(final byte[] pArray)
static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe, final int maxResultSize)
static String encodeBase64String(final byte[] binaryData)
static final int MASK_6BITS
void decode(final byte[] in, int inPos, final int inAvail, final Context context)
Base64(final int lineLength, final byte[] lineSeparator, final boolean urlSafe)
static final byte[] CHUNK_SEPARATOR
static boolean isBase64(final byte[] arrayOctet)
static final byte[] STANDARD_ENCODE_TABLE
static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked)
static byte[] encodeInteger(final BigInteger bigInt)
static byte[] decodeBase64(final byte[] base64Data)
static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe)
boolean containsAlphabetOrPad(final byte[] arrayOctet)
Base64(final int lineLength, final byte[] lineSeparator)
static final int MASK_8BITS
static final byte[] DECODE_TABLE
static BigInteger decodeInteger(final byte[] pArray)
static byte[] toIntegerBytes(final BigInteger bigInt)
static boolean isWhiteSpace(final byte byteToCheck)
final byte[] lineSeparator
static byte[] decodeBase64(final String base64String)
static final byte PAD_DEFAULT
static final int BYTES_PER_ENCODED_BLOCK
static boolean isArrayByteBase64(final byte[] arrayOctet)
boolean isInAlphabet(final byte octet)
static final int BITS_PER_ENCODED_BYTE
static byte[] encodeBase64Chunked(final byte[] binaryData)
static String newStringUtf8(final byte[] bytes)
static byte[] getBytesUtf8(final String string)
static boolean isBase64(final String base64)
static byte[] encodeBase64URLSafe(final byte[] binaryData)
byte[] ensureBufferSize(final int size, final Context context)
static String encodeBase64URLSafeString(final byte[] binaryData)
static boolean isBase64(final byte octet)
void encode(final byte[] in, int inPos, final int inAvail, final Context context)
static final int MIME_CHUNK_SIZE
static byte[] encodeBase64(final byte[] binaryData)
Base64(final boolean urlSafe)
static final byte[] URL_SAFE_ENCODE_TABLE