public class Base64 extends Object
Original Author: iharder Reference: http://iharder.sourceforge.net/current/java/base64/
NOTE: Note that the class is updated to to match the android.util.Base64 interface. Since that interface does not follow the Base64 standard this class also does not entirely follow the standard, notably:URL_SAFE
encoding will both pad and wrap lines per default,
which frankly makes it neither safe for URLs nor filenames.
Modifier and Type | Field and Description |
---|---|
protected static byte |
CR
The new carriage return (\r) as a byte.
|
static int |
CRLF
Encoder flag bit to indicate lines should be terminated with a CRLF pair
instead of just an LF.
|
static int |
DEFAULT
Default values for encoder/decoder flags.
|
protected static byte |
EQUALS_SIGN
The equals sign (=) as a byte.
|
protected static byte |
LF
The new line character (\n) as a byte.
|
protected static int |
MAX_LINE_LENGTH
Maximum line length (76) of Base64 output.
|
static int |
NO_CLOSE
Flag to pass to Base64OutputStream to indicate that it should not
close the output stream it is wrapping when it itself is closed.
|
static int |
NO_PADDING
Encoder flag bit to omit the padding '=' characters at the end of the
output (if any).
|
static int |
NO_WRAP
Do not break lines when encoding.
|
static int |
URL_SAFE
Encoder/decoder flag bit to indicate using the "URL and filename safe"
variant of Base64 (see RFC 3548 section 4) where - and _ are used in
place of + and /.
|
protected static byte |
WHITE_SPACE_ENC |
Modifier and Type | Method and Description |
---|---|
static byte[] |
decode(byte[] source,
int options)
Decode the Base64-encoded data in input and return the data in a new
byte array.
|
static byte[] |
decode(byte[] source,
int offset,
int len,
int options)
Decode the Base64-encoded data in input and return the data in a new
byte array.
|
static byte[] |
decode(String s,
int options)
Decodes data from Base64 notation, automatically
detecting gzip-compressed data and decompressing it.
|
protected static int |
decode4to3(byte[] src,
int len,
byte[] dest,
int offset,
byte[] decodabet)
Decodes four bytes from array source
and writes the resulting bytes (up to three of them)
to destination.
|
static byte[] |
encode(byte[] source,
int options)
Similar to
encodeToString(byte[], int, int, int) but returns
a byte array instead of instantiating a String. |
static byte[] |
encode(byte[] source,
int off,
int len,
int options)
Similar to
encodeToString(byte[], int, int, int) but returns
a byte array instead of instantiating a String. |
protected static int |
encode3to4(byte[] source,
int srcOffset,
int numSigBytes,
byte[] destination,
int destOffset,
boolean noPadding,
byte[] alphabet)
Encodes up to three bytes of the array source
and writes the resulting four Base64 bytes to destination.
|
static String |
encodeToString(byte[] source,
int options)
Encodes a byte array into Base64 notation.
|
static String |
encodeToString(byte[] source,
int off,
int len,
int options)
Encodes a byte array into Base64 notation.
|
protected static byte[] |
getAlphabet(int options)
Returns one of the _SOMETHING_ALPHABET byte arrays depending on
the options specified.
|
protected static byte[] |
getDecodabet(int options)
Returns one of the _SOMETHING_DECODABET byte arrays depending on
the options specified.
|
public static final int DEFAULT
public static final int NO_PADDING
public static final int NO_WRAP
public static final int CRLF
public static final int URL_SAFE
public static final int NO_CLOSE
protected static final int MAX_LINE_LENGTH
protected static final byte EQUALS_SIGN
protected static final byte CR
protected static final byte LF
protected static final byte WHITE_SPACE_ENC
protected static byte[] getAlphabet(int options)
options
- Option used to determine alphabet.protected static byte[] getDecodabet(int options)
options
- Option used to determine decodabet.protected static int encode3to4(byte[] source, int srcOffset, int numSigBytes, byte[] destination, int destOffset, boolean noPadding, byte[] alphabet)
Encodes up to three bytes of the array source and writes the resulting four Base64 bytes to destination. The source and destination arrays can be manipulated anywhere along their length by specifying srcOffset and destOffset. This method does not check to make sure your arrays are large enough to accomodate srcOffset + 3 for the source array or destOffset + 4 for the destination array. The actual number of significant bytes in your array is given by numSigBytes.
This is the lowest level of the encoding methods with all possible parameters.
source
- the array to convertsrcOffset
- the index where conversion beginsnumSigBytes
- the number of significant bytes in your arraydestination
- the array to hold the conversiondestOffset
- the index where output will be putnoPadding
- True if no padding should be added.alphabet
- The alphabet to use.public static String encodeToString(byte[] source, int options)
source
- The data to convertoptions
- Specified optionsNullPointerException
- if source array is nullNO_WRAP
public static String encodeToString(byte[] source, int off, int len, int options)
source
- The data to convertoff
- Offset in array where conversion should beginlen
- Length of data to convertoptions
- Specified optionsNullPointerException
- if source array is nullIllegalArgumentException
- if source array, offset, or length are invalidNO_WRAP
public static byte[] encode(byte[] source, int off, int len, int options)
encodeToString(byte[], int, int, int)
but returns
a byte array instead of instantiating a String. This is more efficient
if you're working with I/O streams and have large data sets to encodeToString.source
- The data to convertoff
- Offset in array where conversion should beginlen
- Length of data to convertoptions
- Specified optionsNullPointerException
- if source array is nullIllegalArgumentException
- if source array, offset, or length are invalidNO_WRAP
,
URL_SAFE
,
CRLF
public static byte[] encode(byte[] source, int options)
encodeToString(byte[], int, int, int)
but returns
a byte array instead of instantiating a String. This is more efficient
if you're working with I/O streams and have large data sets to encodeToString.source
- The data to convertoptions
- Specified optionsNullPointerException
- if source array is nullIllegalArgumentException
- if source array, offset, or length are invalidNO_WRAP
protected static int decode4to3(byte[] src, int len, byte[] dest, int offset, byte[] decodabet)
This is the lowest level of the decoding methods with all possible parameters.
src
- the array to convertlen
- The number of bytes to read within src.dest
- the array to hold the conversionoffset
- the index where output will be putdecodabet
- alphabet type is pulled from this (standard, url-safe, ordered)NullPointerException
- if source or destination arrays are nullIllegalArgumentException
- if srcOffset or destOffset are invalid
or there is not enough room in the array.public static byte[] decode(byte[] source, int options)
source
- The data to decodeoptions
- controls certain features of the decoded output. Pass
DEFAULT to decode standard Base64.public static byte[] decode(byte[] source, int offset, int len, int options)
source
- The data to decodeoffset
- The offset within the input array at which to startlen
- The number of byte sof input to decode.options
- controls certain features of the decoded output. Pass
DEFAULT to decode standard Base64.public static byte[] decode(String s, int options)
s
- the string to decodeoptions
- encodeToString options such as URL_SAFENullPointerException
- if s is nullCopyright © 2018. All rights reserved.