|
|
|
@ -20,31 +20,34 @@ package org.apache.commons.net.util; |
|
|
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
|
import java.math.BigInteger; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Provides Base64 encoding and decoding as defined by RFC 2045. |
|
|
|
|
* |
|
|
|
|
* <p> |
|
|
|
|
* This class implements section <cite>6.8. Base64 Content-Transfer-Encoding</cite> from RFC 2045 <cite>Multipurpose |
|
|
|
|
* Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies</cite> by Freed and Borenstein. |
|
|
|
|
* This class implements section <cite>6.8. Base64 Content-Transfer-Encoding</cite> from RFC 2045 |
|
|
|
|
* <cite>Multipurpose |
|
|
|
|
* Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies</cite> by Freed and |
|
|
|
|
* Borenstein. |
|
|
|
|
* </p> |
|
|
|
|
* <p> |
|
|
|
|
* The class can be parameterized in the following manner with various constructors: |
|
|
|
|
* <ul> |
|
|
|
|
* <li>URL-safe mode: Default off.</li> |
|
|
|
|
* <li>Line length: Default 76. Line length that aren't multiples of 4 will still essentially end up being multiples of |
|
|
|
|
* <li>Line length: Default 76. Line length that aren't multiples of 4 will still essentially end up |
|
|
|
|
* being multiples of |
|
|
|
|
* 4 in the encoded data. |
|
|
|
|
* <li>Line separator: Default is CRLF ("\r\n")</li> |
|
|
|
|
* </ul> |
|
|
|
|
* <p> |
|
|
|
|
* Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode |
|
|
|
|
* character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc). |
|
|
|
|
* Since this class operates directly on byte streams, and not character streams, it is hard-coded |
|
|
|
|
* to only encode/decode |
|
|
|
|
* character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, |
|
|
|
|
* Windows-1252, UTF-8, etc). |
|
|
|
|
* </p> |
|
|
|
|
* |
|
|
|
|
* @version $Id: Base64.java 1697293 2015-08-24 01:01:00Z sebb $ |
|
|
|
|
* @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a> |
|
|
|
|
* @since 2.2 |
|
|
|
|
* @version $Id: Base64.java 1697293 2015-08-24 01:01:00Z sebb $ |
|
|
|
|
*/ |
|
|
|
|
public class Base64 { |
|
|
|
|
private static final int DEFAULT_BUFFER_RESIZE_FACTOR = 2; |
|
|
|
@ -55,7 +58,8 @@ public class Base64 { |
|
|
|
|
* Chunk size per RFC 2045 section 6.8. |
|
|
|
|
* |
|
|
|
|
* <p> |
|
|
|
|
* The {@value} character limit does not count the trailing CRLF, but counts all other characters, including any |
|
|
|
|
* The {@value} character limit does not count the trailing CRLF, but counts all other characters, |
|
|
|
|
* including any |
|
|
|
|
* equal signs. |
|
|
|
|
* </p> |
|
|
|
|
* |
|
|
|
@ -68,23 +72,23 @@ public class Base64 { |
|
|
|
|
* |
|
|
|
|
* @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045 section 2.1</a> |
|
|
|
|
*/ |
|
|
|
|
private static final byte[] CHUNK_SEPARATOR = {'\r', '\n'}; |
|
|
|
|
private static final byte[] CHUNK_SEPARATOR = { '\r', '\n' }; |
|
|
|
|
|
|
|
|
|
private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* This array is a lookup table that translates 6-bit positive integer index values into their "Base64 Alphabet" |
|
|
|
|
* This array is a lookup table that translates 6-bit positive integer index values into their |
|
|
|
|
* "Base64 Alphabet" |
|
|
|
|
* equivalents as specified in Table 1 of RFC 2045. |
|
|
|
|
* |
|
|
|
|
* Thanks to "commons" project in ws.apache.org for this code. |
|
|
|
|
* http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
|
|
|
|
|
*/ |
|
|
|
|
private static final byte[] STANDARD_ENCODE_TABLE = { |
|
|
|
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
|
|
|
|
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
|
|
|
|
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', |
|
|
|
|
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
|
|
|
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' |
|
|
|
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', |
|
|
|
|
'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', |
|
|
|
|
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', |
|
|
|
|
'5', '6', '7', '8', '9', '+', '/' |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -93,11 +97,10 @@ public class Base64 { |
|
|
|
|
* This table is only used when the Base64's mode is set to URL-SAFE. |
|
|
|
|
*/ |
|
|
|
|
private static final byte[] URL_SAFE_ENCODE_TABLE = { |
|
|
|
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
|
|
|
|
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
|
|
|
|
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', |
|
|
|
|
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
|
|
|
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_' |
|
|
|
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', |
|
|
|
|
'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', |
|
|
|
|
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', |
|
|
|
|
'5', '6', '7', '8', '9', '-', '_' |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -106,24 +109,27 @@ public class Base64 { |
|
|
|
|
private static final byte PAD = '='; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* This array is a lookup table that translates Unicode characters drawn from the "Base64 Alphabet" (as specified in |
|
|
|
|
* Table 1 of RFC 2045) into their 6-bit positive integer equivalents. Characters that are not in the Base64 |
|
|
|
|
* This array is a lookup table that translates Unicode characters drawn from the "Base64 |
|
|
|
|
* Alphabet" (as specified in |
|
|
|
|
* Table 1 of RFC 2045) into their 6-bit positive integer equivalents. Characters that are not in |
|
|
|
|
* the Base64 |
|
|
|
|
* alphabet but fall within the bounds of the array are translated to -1. |
|
|
|
|
* |
|
|
|
|
* Note: '+' and '-' both decode to 62. '/' and '_' both decode to 63. This means decoder seamlessly handles both |
|
|
|
|
* URL_SAFE and STANDARD base64. (The encoder, on the other hand, needs to know ahead of time what to emit). |
|
|
|
|
* Note: '+' and '-' both decode to 62. '/' and '_' both decode to 63. This means decoder |
|
|
|
|
* seamlessly handles both |
|
|
|
|
* URL_SAFE and STANDARD base64. (The encoder, on the other hand, needs to know ahead of time what |
|
|
|
|
* to emit). |
|
|
|
|
* |
|
|
|
|
* Thanks to "commons" project in ws.apache.org for this code. |
|
|
|
|
* http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
|
|
|
|
|
*/ |
|
|
|
|
private static final byte[] DECODE_TABLE = { |
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63, 52, 53, 54, |
|
|
|
|
55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, |
|
|
|
|
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, |
|
|
|
|
24, 25, -1, -1, -1, -1, 63, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, |
|
|
|
|
35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 |
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, |
|
|
|
|
-1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, |
|
|
|
|
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, |
|
|
|
|
63, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, |
|
|
|
|
47, 48, 49, 50, 51 |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/** Mask used to extract 6 bits, used when encoding */ |
|
|
|
@ -137,14 +143,17 @@ public class Base64 { |
|
|
|
|
// some state be preserved between calls of encode() and decode().
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Encode table to use: either STANDARD or URL_SAFE. Note: the DECODE_TABLE above remains static because it is able |
|
|
|
|
* to decode both STANDARD and URL_SAFE streams, but the encodeTable must be a member variable so we can switch |
|
|
|
|
* Encode table to use: either STANDARD or URL_SAFE. Note: the DECODE_TABLE above remains static |
|
|
|
|
* because it is able |
|
|
|
|
* to decode both STANDARD and URL_SAFE streams, but the encodeTable must be a member variable so |
|
|
|
|
* we can switch |
|
|
|
|
* between the two modes. |
|
|
|
|
*/ |
|
|
|
|
private final byte[] encodeTable; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Line length for encoding. Not used when decoding. A value of zero or less implies no chunking of the base64 |
|
|
|
|
* Line length for encoding. Not used when decoding. A value of zero or less implies no chunking |
|
|
|
|
* of the base64 |
|
|
|
|
* encoded data. |
|
|
|
|
*/ |
|
|
|
|
private final int lineLength; |
|
|
|
@ -155,13 +164,15 @@ public class Base64 { |
|
|
|
|
private final byte[] lineSeparator; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Convenience variable to help us determine when our buffer is going to run out of room and needs resizing. |
|
|
|
|
* Convenience variable to help us determine when our buffer is going to run out of room and needs |
|
|
|
|
* resizing. |
|
|
|
|
* <code>decodeSize = 3 + lineSeparator.length;</code> |
|
|
|
|
*/ |
|
|
|
|
private final int decodeSize; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Convenience variable to help us determine when our buffer is going to run out of room and needs resizing. |
|
|
|
|
* Convenience variable to help us determine when our buffer is going to run out of room and needs |
|
|
|
|
* resizing. |
|
|
|
|
* <code>encodeSize = 4 + lineSeparator.length;</code> |
|
|
|
|
*/ |
|
|
|
|
private final int encodeSize; |
|
|
|
@ -182,25 +193,29 @@ public class Base64 { |
|
|
|
|
private int readPos; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Variable tracks how many characters have been written to the current line. Only used when encoding. We use it to |
|
|
|
|
* 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). |
|
|
|
|
*/ |
|
|
|
|
private int currentLinePos; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Writes to the buffer only occur after every 3 reads when encoding, an every 4 reads when decoding. This variable |
|
|
|
|
* Writes to the buffer only occur after every 3 reads when encoding, an every 4 reads when |
|
|
|
|
* decoding. This variable |
|
|
|
|
* helps track that. |
|
|
|
|
*/ |
|
|
|
|
private int modulus; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Boolean flag to indicate the EOF has been reached. Once EOF has been reached, this Base64 object becomes useless, |
|
|
|
|
* Boolean flag to indicate the EOF has been reached. Once EOF has been reached, this Base64 |
|
|
|
|
* object becomes useless, |
|
|
|
|
* and must be thrown away. |
|
|
|
|
*/ |
|
|
|
|
private boolean eof; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Place holder for the 3 bytes we're dealing with for our base64 logic. Bitwise operations store and extract the |
|
|
|
|
* Place holder for the 3 bytes we're dealing with for our base64 logic. Bitwise operations store |
|
|
|
|
* and extract the |
|
|
|
|
* base64 encoding or decoding from this variable. |
|
|
|
|
*/ |
|
|
|
|
private int x; |
|
|
|
@ -208,7 +223,8 @@ public class Base64 { |
|
|
|
|
/** |
|
|
|
|
* Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode. |
|
|
|
|
* <p> |
|
|
|
|
* When encoding the line length is 76, the line separator is CRLF, and the encoding table is STANDARD_ENCODE_TABLE. |
|
|
|
|
* When encoding the line length is 76, the line separator is CRLF, and the encoding table is |
|
|
|
|
* STANDARD_ENCODE_TABLE. |
|
|
|
|
* </p> |
|
|
|
|
* |
|
|
|
|
* <p> |
|
|
|
@ -222,15 +238,16 @@ public class Base64 { |
|
|
|
|
/** |
|
|
|
|
* Creates a Base64 codec used for decoding (all modes) and encoding in the given URL-safe mode. |
|
|
|
|
* <p> |
|
|
|
|
* When encoding the line length is 76, the line separator is CRLF, and the encoding table is STANDARD_ENCODE_TABLE. |
|
|
|
|
* When encoding the line length is 76, the line separator is CRLF, and the encoding table is |
|
|
|
|
* STANDARD_ENCODE_TABLE. |
|
|
|
|
* </p> |
|
|
|
|
* |
|
|
|
|
* <p> |
|
|
|
|
* When decoding all variants are supported. |
|
|
|
|
* </p> |
|
|
|
|
* |
|
|
|
|
* @param urlSafe |
|
|
|
|
* if <code>true</code>, URL-safe encoding is used. In most cases this should be set to |
|
|
|
|
* @param urlSafe if <code>true</code>, URL-safe encoding is used. In most cases this should be |
|
|
|
|
* set to |
|
|
|
|
* <code>false</code>. |
|
|
|
|
* @since 1.4 |
|
|
|
|
*/ |
|
|
|
@ -241,19 +258,22 @@ public class Base64 { |
|
|
|
|
/** |
|
|
|
|
* Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode. |
|
|
|
|
* <p> |
|
|
|
|
* When encoding the line length is given in the constructor, the line separator is CRLF, and the encoding table is |
|
|
|
|
* When encoding the line length is given in the constructor, the line separator is CRLF, and the |
|
|
|
|
* encoding table is |
|
|
|
|
* STANDARD_ENCODE_TABLE. |
|
|
|
|
* </p> |
|
|
|
|
* <p> |
|
|
|
|
* Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data. |
|
|
|
|
* Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in |
|
|
|
|
* the encoded data. |
|
|
|
|
* </p> |
|
|
|
|
* <p> |
|
|
|
|
* When decoding all variants are supported. |
|
|
|
|
* </p> |
|
|
|
|
* |
|
|
|
|
* @param lineLength |
|
|
|
|
* Each line of encoded data will be at most of the given length (rounded down to nearest multiple of 4). |
|
|
|
|
* If {@code lineLength <= 0}, then the output will not be divided into lines (chunks). Ignored when decoding. |
|
|
|
|
* @param lineLength Each line of encoded data will be at most of the given length (rounded down |
|
|
|
|
* to nearest multiple of 4). |
|
|
|
|
* If {@code lineLength <= 0}, then the output will not be divided into lines (chunks). Ignored |
|
|
|
|
* when decoding. |
|
|
|
|
* @since 1.4 |
|
|
|
|
*/ |
|
|
|
|
public Base64(int lineLength) { |
|
|
|
@ -263,23 +283,25 @@ public class Base64 { |
|
|
|
|
/** |
|
|
|
|
* Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode. |
|
|
|
|
* <p> |
|
|
|
|
* When encoding the line length and line separator are given in the constructor, and the encoding table is |
|
|
|
|
* When encoding the line length and line separator are given in the constructor, and the encoding |
|
|
|
|
* table is |
|
|
|
|
* STANDARD_ENCODE_TABLE. |
|
|
|
|
* </p> |
|
|
|
|
* <p> |
|
|
|
|
* Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data. |
|
|
|
|
* Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in |
|
|
|
|
* the encoded data. |
|
|
|
|
* </p> |
|
|
|
|
* <p> |
|
|
|
|
* When decoding all variants are supported. |
|
|
|
|
* </p> |
|
|
|
|
* |
|
|
|
|
* @param lineLength |
|
|
|
|
* Each line of encoded data will be at most of the given length (rounded down to nearest multiple of 4). |
|
|
|
|
* If {@code lineLength <= 0}, then the output will not be divided into lines (chunks). Ignored when decoding. |
|
|
|
|
* @param lineSeparator |
|
|
|
|
* Each line of encoded data will end with this sequence of bytes. |
|
|
|
|
* @throws IllegalArgumentException |
|
|
|
|
* Thrown when the provided lineSeparator included some base64 characters. |
|
|
|
|
* @param lineLength Each line of encoded data will be at most of the given length (rounded down |
|
|
|
|
* to nearest multiple of 4). |
|
|
|
|
* If {@code lineLength <= 0}, then the output will not be divided into lines (chunks). Ignored |
|
|
|
|
* when decoding. |
|
|
|
|
* @param lineSeparator Each line of encoded data will end with this sequence of bytes. |
|
|
|
|
* @throws IllegalArgumentException Thrown when the provided lineSeparator included some base64 |
|
|
|
|
* characters. |
|
|
|
|
* @since 1.4 |
|
|
|
|
*/ |
|
|
|
|
public Base64(int lineLength, byte[] lineSeparator) { |
|
|
|
@ -289,26 +311,28 @@ public class Base64 { |
|
|
|
|
/** |
|
|
|
|
* Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode. |
|
|
|
|
* <p> |
|
|
|
|
* When encoding the line length and line separator are given in the constructor, and the encoding table is |
|
|
|
|
* When encoding the line length and line separator are given in the constructor, and the encoding |
|
|
|
|
* table is |
|
|
|
|
* STANDARD_ENCODE_TABLE. |
|
|
|
|
* </p> |
|
|
|
|
* <p> |
|
|
|
|
* Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data. |
|
|
|
|
* Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in |
|
|
|
|
* the encoded data. |
|
|
|
|
* </p> |
|
|
|
|
* <p> |
|
|
|
|
* When decoding all variants are supported. |
|
|
|
|
* </p> |
|
|
|
|
* |
|
|
|
|
* @param lineLength |
|
|
|
|
* Each line of encoded data will be at most of the given length (rounded down to nearest multiple of 4). |
|
|
|
|
* If {@code lineLength <= 0}, then the output will not be divided into lines (chunks). Ignored when decoding. |
|
|
|
|
* @param lineSeparator |
|
|
|
|
* Each line of encoded data will end with this sequence of bytes. |
|
|
|
|
* @param urlSafe |
|
|
|
|
* Instead of emitting '+' and '/' we emit '-' and '_' respectively. urlSafe is only applied to encode |
|
|
|
|
* @param lineLength Each line of encoded data will be at most of the given length (rounded down |
|
|
|
|
* to nearest multiple of 4). |
|
|
|
|
* If {@code lineLength <= 0}, then the output will not be divided into lines (chunks). Ignored |
|
|
|
|
* when decoding. |
|
|
|
|
* @param lineSeparator Each line of encoded data will end with this sequence of bytes. |
|
|
|
|
* @param urlSafe Instead of emitting '+' and '/' we emit '-' and '_' respectively. urlSafe is |
|
|
|
|
* only applied to encode |
|
|
|
|
* operations. Decoding seamlessly handles both modes. |
|
|
|
|
* @throws IllegalArgumentException |
|
|
|
|
* The provided lineSeparator included some base64 characters. That's not going to work! |
|
|
|
|
* @throws IllegalArgumentException The provided lineSeparator included some base64 characters. |
|
|
|
|
* That's not going to work! |
|
|
|
|
* @since 1.4 |
|
|
|
|
*/ |
|
|
|
|
public Base64(int lineLength, byte[] lineSeparator, boolean urlSafe) { |
|
|
|
@ -327,7 +351,8 @@ public class Base64 { |
|
|
|
|
this.decodeSize = this.encodeSize - 1; |
|
|
|
|
if (containsBase64Byte(lineSeparator)) { |
|
|
|
|
String sep = newStringUtf8(lineSeparator); |
|
|
|
|
throw new IllegalArgumentException("lineSeperator must not contain base64 characters: [" + sep + "]"); |
|
|
|
|
throw new IllegalArgumentException( |
|
|
|
|
"lineSeperator must not contain base64 characters: [" + sep + "]"); |
|
|
|
|
} |
|
|
|
|
this.encodeTable = urlSafe ? URL_SAFE_ENCODE_TABLE : STANDARD_ENCODE_TABLE; |
|
|
|
|
} |
|
|
|
@ -374,15 +399,14 @@ public class Base64 { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Extracts buffered data into the provided byte[] array, starting at position bPos, up to a maximum of bAvail |
|
|
|
|
* 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. |
|
|
|
|
* |
|
|
|
|
* @param b |
|
|
|
|
* byte[] array to extract the buffered data into. |
|
|
|
|
* @param bPos |
|
|
|
|
* position in byte[] array to start extraction at. |
|
|
|
|
* @param bAvail |
|
|
|
|
* amount of bytes we're allowed to extract. We may extract fewer (if fewer are available). |
|
|
|
|
* @param b byte[] array to extract the buffered data into. |
|
|
|
|
* @param bPos position in byte[] array to start extraction at. |
|
|
|
|
* @param bAvail amount of bytes we're allowed to extract. We may extract fewer (if fewer are |
|
|
|
|
* available). |
|
|
|
|
* @return The number of bytes successfully extracted into the provided byte[] array. |
|
|
|
|
*/ |
|
|
|
|
int readResults(byte[] b, int bPos, int bAvail) { |
|
|
|
@ -405,15 +429,14 @@ public class Base64 { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Sets the streaming buffer. This is a small optimization where we try to buffer directly to the consumer's output |
|
|
|
|
* array for one round (if the consumer calls this method first) instead of starting our own buffer. |
|
|
|
|
* Sets the streaming buffer. This is a small optimization where we try to buffer directly to the |
|
|
|
|
* consumer's output |
|
|
|
|
* array for one round (if the consumer calls this method first) instead of starting our own |
|
|
|
|
* buffer. |
|
|
|
|
* |
|
|
|
|
* @param out |
|
|
|
|
* byte[] array to buffer directly to. |
|
|
|
|
* @param outPos |
|
|
|
|
* Position to start buffering into. |
|
|
|
|
* @param outAvail |
|
|
|
|
* Amount of bytes available for direct buffering. |
|
|
|
|
* @param out byte[] array to buffer directly to. |
|
|
|
|
* @param outPos Position to start buffering into. |
|
|
|
|
* @param outAvail Amount of bytes available for direct buffering. |
|
|
|
|
*/ |
|
|
|
|
void setInitialBuffer(byte[] out, int outPos, int outAvail) { |
|
|
|
|
// We can re-use consumer's original output array under
|
|
|
|
@ -427,8 +450,10 @@ public class Base64 { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* <p> |
|
|
|
|
* Encodes all of the provided data, starting at inPos, for inAvail bytes. Must be called at least twice: once with |
|
|
|
|
* the data to encode, and once with inAvail set to "-1" to alert encoder that EOF has been reached, so flush last |
|
|
|
|
* Encodes all of the provided data, starting at inPos, for inAvail bytes. Must be called at least |
|
|
|
|
* twice: once with |
|
|
|
|
* the data to encode, and once with inAvail set to "-1" to alert encoder that EOF has been |
|
|
|
|
* reached, so flush last |
|
|
|
|
* remaining bytes (if not multiple of 3). |
|
|
|
|
* </p> |
|
|
|
|
* <p> |
|
|
|
@ -436,12 +461,9 @@ public class Base64 { |
|
|
|
|
* http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
|
|
|
|
|
* </p> |
|
|
|
|
* |
|
|
|
|
* @param in |
|
|
|
|
* byte[] array of binary data to base64 encode. |
|
|
|
|
* @param inPos |
|
|
|
|
* Position to start reading data from. |
|
|
|
|
* @param inAvail |
|
|
|
|
* Amount of bytes available from input for encoding. |
|
|
|
|
* @param in byte[] array of binary data to base64 encode. |
|
|
|
|
* @param inPos Position to start reading data from. |
|
|
|
|
* @param inAvail Amount of bytes available from input for encoding. |
|
|
|
|
*/ |
|
|
|
|
void encode(byte[] in, int inPos, int inAvail) { |
|
|
|
|
if (eof) { |
|
|
|
@ -455,7 +477,7 @@ public class Base64 { |
|
|
|
|
resizeBuffer(); |
|
|
|
|
} |
|
|
|
|
switch (modulus) { |
|
|
|
|
case 1 : |
|
|
|
|
case 1: |
|
|
|
|
buffer[pos++] = encodeTable[(x >> 2) & MASK_6BITS]; |
|
|
|
|
buffer[pos++] = encodeTable[(x << 4) & MASK_6BITS]; |
|
|
|
|
// URL-SAFE skips the padding to further reduce size.
|
|
|
|
@ -465,7 +487,7 @@ public class Base64 { |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
case 2 : |
|
|
|
|
case 2: |
|
|
|
|
buffer[pos++] = encodeTable[(x >> 10) & MASK_6BITS]; |
|
|
|
|
buffer[pos++] = encodeTable[(x >> 4) & MASK_6BITS]; |
|
|
|
|
buffer[pos++] = encodeTable[(x << 2) & MASK_6BITS]; |
|
|
|
@ -510,13 +532,17 @@ public class Base64 { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* <p> |
|
|
|
|
* Decodes all of the provided data, starting at inPos, for inAvail bytes. Should be called at least twice: once |
|
|
|
|
* with the data to decode, and once with inAvail set to "-1" to alert decoder that EOF has been reached. The "-1" |
|
|
|
|
* Decodes all of the provided data, starting at inPos, for inAvail bytes. Should be called at |
|
|
|
|
* least twice: once |
|
|
|
|
* with the data to decode, and once with inAvail set to "-1" to alert decoder that EOF has been |
|
|
|
|
* reached. The "-1" |
|
|
|
|
* call is not necessary when decoding, but it doesn't hurt, either. |
|
|
|
|
* </p> |
|
|
|
|
* <p> |
|
|
|
|
* Ignores all non-base64 characters. This is how chunked (e.g. 76 character) data is handled, since CR and LF are |
|
|
|
|
* silently ignored, but has implications for other bytes, too. This method subscribes to the garbage-in, |
|
|
|
|
* Ignores all non-base64 characters. This is how chunked (e.g. 76 character) data is handled, |
|
|
|
|
* since CR and LF are |
|
|
|
|
* silently ignored, but has implications for other bytes, too. This method subscribes to the |
|
|
|
|
* garbage-in, |
|
|
|
|
* garbage-out philosophy: it will not check the provided data for validity. |
|
|
|
|
* </p> |
|
|
|
|
* <p> |
|
|
|
@ -524,12 +550,9 @@ public class Base64 { |
|
|
|
|
* http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
|
|
|
|
|
* </p> |
|
|
|
|
* |
|
|
|
|
* @param in |
|
|
|
|
* byte[] array of ascii data to base64 decode. |
|
|
|
|
* @param inPos |
|
|
|
|
* Position to start reading data from. |
|
|
|
|
* @param inAvail |
|
|
|
|
* Amount of bytes available from input for encoding. |
|
|
|
|
* @param in byte[] array of ascii data to base64 decode. |
|
|
|
|
* @param inPos Position to start reading data from. |
|
|
|
|
* @param inAvail Amount of bytes available from input for encoding. |
|
|
|
|
*/ |
|
|
|
|
void decode(byte[] in, int inPos, int inAvail) { |
|
|
|
|
if (eof) { |
|
|
|
@ -569,11 +592,11 @@ public class Base64 { |
|
|
|
|
if (eof && modulus != 0) { |
|
|
|
|
x = x << 6; |
|
|
|
|
switch (modulus) { |
|
|
|
|
case 2 : |
|
|
|
|
case 2: |
|
|
|
|
x = x << 6; |
|
|
|
|
buffer[pos++] = (byte) ((x >> 16) & MASK_8BITS); |
|
|
|
|
break; |
|
|
|
|
case 3 : |
|
|
|
|
case 3: |
|
|
|
|
buffer[pos++] = (byte) ((x >> 16) & MASK_8BITS); |
|
|
|
|
buffer[pos++] = (byte) ((x >> 8) & MASK_8BITS); |
|
|
|
|
break; |
|
|
|
@ -586,9 +609,9 @@ public class Base64 { |
|
|
|
|
/** |
|
|
|
|
* Returns whether or not the <code>octet</code> is in the base 64 alphabet. |
|
|
|
|
* |
|
|
|
|
* @param octet |
|
|
|
|
* The value to test |
|
|
|
|
* @return <code>true</code> if the value is defined in the the base 64 alphabet, <code>false</code> otherwise. |
|
|
|
|
* @param octet The value to test |
|
|
|
|
* @return <code>true</code> if the value is defined in the the base 64 alphabet, |
|
|
|
|
* <code>false</code> otherwise. |
|
|
|
|
* @since 1.4 |
|
|
|
|
*/ |
|
|
|
|
public static boolean isBase64(byte octet) { |
|
|
|
@ -596,12 +619,13 @@ public class Base64 { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Tests a given byte array to see if it contains only valid characters within the Base64 alphabet. Currently the |
|
|
|
|
* Tests a given byte array to see if it contains only valid characters within the Base64 |
|
|
|
|
* alphabet. Currently the |
|
|
|
|
* method treats whitespace as valid. |
|
|
|
|
* |
|
|
|
|
* @param arrayOctet |
|
|
|
|
* byte array to test |
|
|
|
|
* @return <code>true</code> if all bytes are valid characters in the Base64 alphabet or if the byte array is empty; |
|
|
|
|
* @param arrayOctet byte array to test |
|
|
|
|
* @return <code>true</code> if all bytes are valid characters in the Base64 alphabet or if the |
|
|
|
|
* byte array is empty; |
|
|
|
|
* false, otherwise |
|
|
|
|
*/ |
|
|
|
|
public static boolean isArrayByteBase64(byte[] arrayOctet) { |
|
|
|
@ -614,15 +638,15 @@ public class Base64 { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Tests a given byte array to see if it contains only valid characters within the Base64 alphabet. |
|
|
|
|
* Tests a given byte array to see if it contains only valid characters within the Base64 |
|
|
|
|
* alphabet. |
|
|
|
|
* |
|
|
|
|
* @param arrayOctet |
|
|
|
|
* byte array to test |
|
|
|
|
* @return <code>true</code> if any byte is a valid character in the Base64 alphabet; false herwise |
|
|
|
|
* @param arrayOctet byte array to test |
|
|
|
|
* @return <code>true</code> if any byte is a valid character in the Base64 alphabet; false |
|
|
|
|
* herwise |
|
|
|
|
*/ |
|
|
|
|
private static boolean containsBase64Byte(byte[] arrayOctet) { |
|
|
|
|
for (byte element : arrayOctet) |
|
|
|
|
{ |
|
|
|
|
for (byte element : arrayOctet) { |
|
|
|
|
if (isBase64(element)) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
@ -633,8 +657,7 @@ public class Base64 { |
|
|
|
|
/** |
|
|
|
|
* Encodes binary data using the base64 algorithm but does not chunk the output. |
|
|
|
|
* |
|
|
|
|
* @param binaryData |
|
|
|
|
* binary data to encode |
|
|
|
|
* @param binaryData binary data to encode |
|
|
|
|
* @return byte[] containing Base64 characters in their UTF-8 representation. |
|
|
|
|
*/ |
|
|
|
|
public static byte[] encodeBase64(byte[] binaryData) { |
|
|
|
@ -646,8 +669,7 @@ public class Base64 { |
|
|
|
|
* <p> |
|
|
|
|
* For a non-chunking version, see {@link #encodeBase64StringUnChunked(byte[])}. |
|
|
|
|
* |
|
|
|
|
* @param binaryData |
|
|
|
|
* binary data to encode |
|
|
|
|
* @param binaryData binary data to encode |
|
|
|
|
* @return String containing Base64 characters. |
|
|
|
|
* @since 1.4 |
|
|
|
|
*/ |
|
|
|
@ -660,8 +682,7 @@ public class Base64 { |
|
|
|
|
* <p> |
|
|
|
|
* For a chunking version, see {@link #encodeBase64String(byte[])}. |
|
|
|
|
* |
|
|
|
|
* @param binaryData |
|
|
|
|
* binary data to encode |
|
|
|
|
* @param binaryData binary data to encode |
|
|
|
|
* @return String containing Base64 characters. |
|
|
|
|
* @since 3.2 |
|
|
|
|
*/ |
|
|
|
@ -672,8 +693,7 @@ public class Base64 { |
|
|
|
|
/** |
|
|
|
|
* Encodes binary data using the base64 algorithm. |
|
|
|
|
* |
|
|
|
|
* @param binaryData |
|
|
|
|
* binary data to encode |
|
|
|
|
* @param binaryData binary data to encode |
|
|
|
|
* @param useChunking whether to split the output into chunks |
|
|
|
|
* @return String containing Base64 characters. |
|
|
|
|
* @since 3.2 |
|
|
|
@ -683,11 +703,11 @@ public class Base64 { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The |
|
|
|
|
* Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the |
|
|
|
|
* output. The |
|
|
|
|
* url-safe variation emits - and _ instead of + and / characters. |
|
|
|
|
* |
|
|
|
|
* @param binaryData |
|
|
|
|
* binary data to encode |
|
|
|
|
* @param binaryData binary data to encode |
|
|
|
|
* @return byte[] containing Base64 characters in their UTF-8 representation. |
|
|
|
|
* @since 1.4 |
|
|
|
|
*/ |
|
|
|
@ -696,11 +716,11 @@ public class Base64 { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The |
|
|
|
|
* Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the |
|
|
|
|
* output. The |
|
|
|
|
* url-safe variation emits - and _ instead of + and / characters. |
|
|
|
|
* |
|
|
|
|
* @param binaryData |
|
|
|
|
* binary data to encode |
|
|
|
|
* @param binaryData binary data to encode |
|
|
|
|
* @return String containing Base64 characters |
|
|
|
|
* @since 1.4 |
|
|
|
|
*/ |
|
|
|
@ -709,10 +729,10 @@ public class Base64 { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks |
|
|
|
|
* Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character |
|
|
|
|
* blocks |
|
|
|
|
* |
|
|
|
|
* @param binaryData |
|
|
|
|
* binary data to encode |
|
|
|
|
* @param binaryData binary data to encode |
|
|
|
|
* @return Base64 characters chunked in 76 character blocks |
|
|
|
|
*/ |
|
|
|
|
public static byte[] encodeBase64Chunked(byte[] binaryData) { |
|
|
|
@ -722,8 +742,7 @@ public class Base64 { |
|
|
|
|
/** |
|
|
|
|
* Decodes a String containing containing characters in the Base64 alphabet. |
|
|
|
|
* |
|
|
|
|
* @param pArray |
|
|
|
|
* A String containing Base64 character data |
|
|
|
|
* @param pArray A String containing Base64 character data |
|
|
|
|
* @return a byte array containing binary data |
|
|
|
|
* @since 1.4 |
|
|
|
|
*/ |
|
|
|
@ -742,8 +761,7 @@ public class Base64 { |
|
|
|
|
/** |
|
|
|
|
* Decodes a byte[] containing containing characters in the Base64 alphabet. |
|
|
|
|
* |
|
|
|
|
* @param pArray |
|
|
|
|
* A byte array containing Base64 character data |
|
|
|
|
* @param pArray A byte array containing Base64 character data |
|
|
|
|
* @return a byte array containing binary data |
|
|
|
|
*/ |
|
|
|
|
public byte[] decode(byte[] pArray) { |
|
|
|
@ -769,32 +787,32 @@ public class Base64 { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks. |
|
|
|
|
* Encodes binary data using the base64 algorithm, optionally chunking the output into 76 |
|
|
|
|
* character blocks. |
|
|
|
|
* |
|
|
|
|
* @param binaryData |
|
|
|
|
* Array containing binary data to encode. |
|
|
|
|
* @param isChunked |
|
|
|
|
* if <code>true</code> this encoder will chunk the base64 output into 76 character blocks |
|
|
|
|
* @param binaryData Array containing binary data to encode. |
|
|
|
|
* @param isChunked if <code>true</code> this encoder will chunk the base64 output into 76 |
|
|
|
|
* character blocks |
|
|
|
|
* @return Base64-encoded data. |
|
|
|
|
* @throws IllegalArgumentException |
|
|
|
|
* Thrown when the input array needs an output array bigger than {@link Integer#MAX_VALUE} |
|
|
|
|
* @throws IllegalArgumentException Thrown when the input array needs an output array bigger than |
|
|
|
|
* {@link Integer#MAX_VALUE} |
|
|
|
|
*/ |
|
|
|
|
public static byte[] encodeBase64(byte[] binaryData, boolean isChunked) { |
|
|
|
|
return encodeBase64(binaryData, isChunked, false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks. |
|
|
|
|
* Encodes binary data using the base64 algorithm, optionally chunking the output into 76 |
|
|
|
|
* character blocks. |
|
|
|
|
* |
|
|
|
|
* @param binaryData |
|
|
|
|
* Array containing binary data to encode. |
|
|
|
|
* @param isChunked |
|
|
|
|
* if <code>true</code> this encoder will chunk the base64 output into 76 character blocks |
|
|
|
|
* @param urlSafe |
|
|
|
|
* if <code>true</code> this encoder will emit - and _ instead of the usual + and / characters. |
|
|
|
|
* @param binaryData Array containing binary data to encode. |
|
|
|
|
* @param isChunked if <code>true</code> this encoder will chunk the base64 output into 76 |
|
|
|
|
* character blocks |
|
|
|
|
* @param urlSafe if <code>true</code> this encoder will emit - and _ instead of the usual + and / |
|
|
|
|
* characters. |
|
|
|
|
* @return Base64-encoded data. |
|
|
|
|
* @throws IllegalArgumentException |
|
|
|
|
* Thrown when the input array needs an output array bigger than {@link Integer#MAX_VALUE} |
|
|
|
|
* @throws IllegalArgumentException Thrown when the input array needs an output array bigger than |
|
|
|
|
* {@link Integer#MAX_VALUE} |
|
|
|
|
* @since 1.4 |
|
|
|
|
*/ |
|
|
|
|
public static byte[] encodeBase64(byte[] binaryData, boolean isChunked, boolean urlSafe) { |
|
|
|
@ -802,32 +820,33 @@ public class Base64 { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks. |
|
|
|
|
* Encodes binary data using the base64 algorithm, optionally chunking the output into 76 |
|
|
|
|
* character blocks. |
|
|
|
|
* |
|
|
|
|
* @param binaryData |
|
|
|
|
* Array containing binary data to encode. |
|
|
|
|
* @param isChunked |
|
|
|
|
* if <code>true</code> this encoder will chunk the base64 output into 76 character blocks |
|
|
|
|
* @param urlSafe |
|
|
|
|
* if <code>true</code> this encoder will emit - and _ instead of the usual + and / characters. |
|
|
|
|
* @param maxResultSize |
|
|
|
|
* The maximum result size to accept. |
|
|
|
|
* @param binaryData Array containing binary data to encode. |
|
|
|
|
* @param isChunked if <code>true</code> this encoder will chunk the base64 output into 76 |
|
|
|
|
* character blocks |
|
|
|
|
* @param urlSafe if <code>true</code> this encoder will emit - and _ instead of the usual + and / |
|
|
|
|
* characters. |
|
|
|
|
* @param maxResultSize The maximum result size to accept. |
|
|
|
|
* @return Base64-encoded data. |
|
|
|
|
* @throws IllegalArgumentException |
|
|
|
|
* Thrown when the input array needs an output array bigger than maxResultSize |
|
|
|
|
* @throws IllegalArgumentException Thrown when the input array needs an output array bigger than |
|
|
|
|
* maxResultSize |
|
|
|
|
* @since 1.4 |
|
|
|
|
*/ |
|
|
|
|
public static byte[] encodeBase64(byte[] binaryData, boolean isChunked, boolean urlSafe, int maxResultSize) { |
|
|
|
|
public static byte[] encodeBase64(byte[] binaryData, boolean isChunked, boolean urlSafe, |
|
|
|
|
int maxResultSize) { |
|
|
|
|
if (binaryData == null || binaryData.length == 0) { |
|
|
|
|
return binaryData; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
long len = getEncodeLength(binaryData, isChunked ? CHUNK_SIZE : 0, isChunked ? CHUNK_SEPARATOR : EMPTY_BYTE_ARRAY); |
|
|
|
|
long len = getEncodeLength(binaryData, isChunked ? CHUNK_SIZE : 0, |
|
|
|
|
isChunked ? CHUNK_SEPARATOR : EMPTY_BYTE_ARRAY); |
|
|
|
|
if (len > maxResultSize) { |
|
|
|
|
throw new IllegalArgumentException("Input array too big, the output array would be bigger (" + |
|
|
|
|
len + |
|
|
|
|
") than the specified maxium size of " + |
|
|
|
|
maxResultSize); |
|
|
|
|
throw new IllegalArgumentException("Input array too big, the output array would be bigger (" |
|
|
|
|
+ len |
|
|
|
|
+ ") than the specified maxium size of " |
|
|
|
|
+ maxResultSize); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Base64 b64 = isChunked ? new Base64(urlSafe) : new Base64(0, CHUNK_SEPARATOR, urlSafe); |
|
|
|
@ -837,8 +856,7 @@ public class Base64 { |
|
|
|
|
/** |
|
|
|
|
* Decodes a Base64 String into octets |
|
|
|
|
* |
|
|
|
|
* @param base64String |
|
|
|
|
* String containing Base64 data |
|
|
|
|
* @param base64String String containing Base64 data |
|
|
|
|
* @return Array containing decoded data. |
|
|
|
|
* @since 1.4 |
|
|
|
|
*/ |
|
|
|
@ -849,40 +867,36 @@ public class Base64 { |
|
|
|
|
/** |
|
|
|
|
* Decodes Base64 data into octets |
|
|
|
|
* |
|
|
|
|
* @param base64Data |
|
|
|
|
* Byte array containing Base64 data |
|
|
|
|
* @param base64Data Byte array containing Base64 data |
|
|
|
|
* @return Array containing decoded data. |
|
|
|
|
*/ |
|
|
|
|
public static byte[] decodeBase64(byte[] base64Data) { |
|
|
|
|
return new Base64().decode(base64Data); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Checks if a byte value is whitespace or not. |
|
|
|
|
* |
|
|
|
|
* @param byteToCheck |
|
|
|
|
* the byte to check |
|
|
|
|
* @param byteToCheck the byte to check |
|
|
|
|
* @return true if byte is whitespace, false otherwise |
|
|
|
|
*/ |
|
|
|
|
private static boolean isWhiteSpace(byte byteToCheck) { |
|
|
|
|
switch (byteToCheck) { |
|
|
|
|
case ' ' : |
|
|
|
|
case '\n' : |
|
|
|
|
case '\r' : |
|
|
|
|
case '\t' : |
|
|
|
|
case ' ': |
|
|
|
|
case '\n': |
|
|
|
|
case '\r': |
|
|
|
|
case '\t': |
|
|
|
|
return true; |
|
|
|
|
default : |
|
|
|
|
default: |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Encodes a byte[] containing binary data, into a String containing characters in the Base64 alphabet. |
|
|
|
|
* Encodes a byte[] containing binary data, into a String containing characters in the Base64 |
|
|
|
|
* alphabet. |
|
|
|
|
* |
|
|
|
|
* @param pArray |
|
|
|
|
* a byte array containing binary data |
|
|
|
|
* @param pArray a byte array containing binary data |
|
|
|
|
* @return A String containing only Base64 character data |
|
|
|
|
* @since 1.4 |
|
|
|
|
*/ |
|
|
|
@ -901,10 +915,10 @@ public class Base64 { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Encodes a byte[] containing binary data, into a byte[] containing characters in the Base64 alphabet. |
|
|
|
|
* Encodes a byte[] containing binary data, into a byte[] containing characters in the Base64 |
|
|
|
|
* alphabet. |
|
|
|
|
* |
|
|
|
|
* @param pArray |
|
|
|
|
* a byte array containing binary data |
|
|
|
|
* @param pArray a byte array containing binary data |
|
|
|
|
* @return A byte array containing only Base64 character data |
|
|
|
|
*/ |
|
|
|
|
public byte[] encode(byte[] pArray) { |
|
|
|
@ -938,7 +952,6 @@ public class Base64 { |
|
|
|
|
* @param chunkSize line-length of the output (<= 0 means no chunking) between each |
|
|
|
|
* chunkSeparator (e.g. CRLF). |
|
|
|
|
* @param chunkSeparator the sequence of bytes used to separate chunks of output (e.g. CRLF). |
|
|
|
|
* |
|
|
|
|
* @return amount of space needed to encoded the supplied array. Returns |
|
|
|
|
* a long since a max-len array will require Integer.MAX_VALUE + 33%. |
|
|
|
|
*/ |
|
|
|
@ -962,11 +975,11 @@ public class Base64 { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Implementation of integer encoding used for crypto
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Decodes a byte64-encoded integer according to crypto standards such as W3C's XML-Signature |
|
|
|
|
* |
|
|
|
|
* @param pArray |
|
|
|
|
* a byte array containing base64 character data |
|
|
|
|
* @param pArray a byte array containing base64 character data |
|
|
|
|
* @return A BigInteger |
|
|
|
|
* @since 1.4 |
|
|
|
|
*/ |
|
|
|
@ -977,11 +990,9 @@ public class Base64 { |
|
|
|
|
/** |
|
|
|
|
* Encodes to a byte64-encoded integer according to crypto standards such as W3C's XML-Signature |
|
|
|
|
* |
|
|
|
|
* @param bigInt |
|
|
|
|
* a BigInteger |
|
|
|
|
* @param bigInt a BigInteger |
|
|
|
|
* @return A byte array containing base64 character data |
|
|
|
|
* @throws NullPointerException |
|
|
|
|
* if null is passed in |
|
|
|
|
* @throws NullPointerException if null is passed in |
|
|
|
|
* @since 1.4 |
|
|
|
|
*/ |
|
|
|
|
public static byte[] encodeInteger(BigInteger bigInt) { |
|
|
|
@ -994,8 +1005,7 @@ public class Base64 { |
|
|
|
|
/** |
|
|
|
|
* Returns a byte-array representation of a <code>BigInteger</code> without sign bit. |
|
|
|
|
* |
|
|
|
|
* @param bigInt |
|
|
|
|
* <code>BigInteger</code> to be converted |
|
|
|
|
* @param bigInt <code>BigInteger</code> to be converted |
|
|
|
|
* @return a byte array representation of the BigInteger parameter |
|
|
|
|
*/ |
|
|
|
|
static byte[] toIntegerBytes(BigInteger bigInt) { |
|
|
|
|