Package android.util
Class Base64
- java.lang.Object
-
- android.util.Base64
-
public class Base64 extends java.lang.Object
Utilities for encoding and decoding the Base64 representation of binary data. See RFCs 2045 and 3548.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:- Standard base64 will wrap at 76 characters per default, as per RFC 2045, not only when requested as per RFC 3548.
-
Even
URL_SAFE
encoding will both pad and wrap lines per default, which frankly makes it neither safe for URLs nor filenames. - Line wrapping will only be applies as a splitter, it will never apply to the end of the encoded string. This is a divergence from the early implementation of the android.util.Base64 class at least.
-
-
Field Summary
Fields Modifier and Type Field 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
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method 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(java.lang.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 toencodeToString(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 toencodeToString(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 java.lang.String
encodeToString(byte[] source, int options)
Encodes a byte array into Base64 notation.static java.lang.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.
-
-
-
Field Detail
-
DEFAULT
public static final int DEFAULT
Default values for encoder/decoder flags.- See Also:
- Constant Field Values
-
NO_PADDING
public static final int NO_PADDING
Encoder flag bit to omit the padding '=' characters at the end of the output (if any).- See Also:
- Constant Field Values
-
NO_WRAP
public static final int NO_WRAP
Do not break lines when encoding. Value is 2.- See Also:
- Constant Field Values
-
CRLF
public static final int CRLF
Encoder flag bit to indicate lines should be terminated with a CRLF pair instead of just an LF. Has no effect if NO_WRAP is specified as well.- See Also:
- Constant Field Values
-
URL_SAFE
public static final 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 /. URL_SAFE implies NO_WRAP and NO_PADDING.- See Also:
- Constant Field Values
-
NO_CLOSE
public static final 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.- See Also:
- Constant Field Values
-
MAX_LINE_LENGTH
protected static final int MAX_LINE_LENGTH
Maximum line length (76) of Base64 output.- See Also:
- Constant Field Values
-
EQUALS_SIGN
protected static final byte EQUALS_SIGN
The equals sign (=) as a byte.- See Also:
- Constant Field Values
-
CR
protected static final byte CR
The new carriage return (\r) as a byte.- See Also:
- Constant Field Values
-
LF
protected static final byte LF
The new line character (\n) as a byte.- See Also:
- Constant Field Values
-
WHITE_SPACE_ENC
protected static final byte WHITE_SPACE_ENC
- See Also:
- Constant Field Values
-
-
Method Detail
-
getAlphabet
protected static byte[] getAlphabet(int options)
Returns one of the _SOMETHING_ALPHABET byte arrays depending on the options specified. It's possible, though silly, to specify ORDERED and URLSAFE in which case one of them will be picked, though there is no guarantee as to which one will be picked.- Parameters:
options
- Option used to determine alphabet.- Returns:
- The alphabet pos to byte value array.
-
getDecodabet
protected static byte[] getDecodabet(int options)
Returns one of the _SOMETHING_DECODABET byte arrays depending on the options specified. It's possible, though silly, to specify ORDERED and URL_SAFE in which case one of them will be picked, though there is no guarantee as to which one will be picked.- Parameters:
options
- Option used to determine decodabet.- Returns:
- The decodabet pos to byte value array.
-
encode3to4
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.
- 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.- Returns:
- the destination array
- Since:
- 1.3
-
encodeToString
public static java.lang.String encodeToString(byte[] source, int options)
Encodes a byte array into Base64 notation.- Parameters:
source
- The data to convertoptions
- Specified options- Returns:
- The Base64-encoded data as a String
- Throws:
java.lang.NullPointerException
- if source array is null- Since:
- 2.0
- See Also:
NO_WRAP
-
encodeToString
public static java.lang.String encodeToString(byte[] source, int off, int len, int options)
Encodes a byte array into Base64 notation.- Parameters:
source
- The data to convertoff
- Offset in array where conversion should beginlen
- Length of data to convertoptions
- Specified options- Returns:
- The Base64-encoded data as a String
- Throws:
java.lang.NullPointerException
- if source array is nulljava.lang.IllegalArgumentException
- if source array, offset, or length are invalid- Since:
- 2.0
- See Also:
NO_WRAP
-
encode
public static byte[] encode(byte[] source, int off, int len, int options)
Similar toencodeToString(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.- Parameters:
source
- The data to convertoff
- Offset in array where conversion should beginlen
- Length of data to convertoptions
- Specified options- Returns:
- The Base64-encoded data as a String
- Throws:
java.lang.NullPointerException
- if source array is nulljava.lang.IllegalArgumentException
- if source array, offset, or length are invalid- Since:
- 2.3.1
- See Also:
NO_WRAP
,URL_SAFE
,CRLF
-
encode
public static byte[] encode(byte[] source, int options)
Similar toencodeToString(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.- Parameters:
source
- The data to convertoptions
- Specified options- Returns:
- The Base64-encoded data as a String
- Throws:
java.lang.NullPointerException
- if source array is nulljava.lang.IllegalArgumentException
- if source array, offset, or length are invalid- Since:
- 2.3.1
- See Also:
NO_WRAP
-
decode4to3
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. 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 + 4 for the source array or destOffset + 3 for the destination array. This method returns the actual number of bytes that were converted from the Base64 encoding.This is the lowest level of the decoding methods with all possible parameters.
- 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)- Returns:
- the number of decoded bytes converted
- Throws:
java.lang.NullPointerException
- if source or destination arrays are nulljava.lang.IllegalArgumentException
- if srcOffset or destOffset are invalid or there is not enough room in the array.- Since:
- 1.3
-
decode
public static byte[] decode(byte[] source, int options)
Decode the Base64-encoded data in input and return the data in a new byte array. The padding '=' characters at the end are considered optional, but if any are present, there must be the correct number of them.- Parameters:
source
- The data to decodeoptions
- controls certain features of the decoded output. Pass DEFAULT to decode standard Base64.- Returns:
- decoded data
-
decode
public 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. The padding '=' characters at the end are considered optional, but if any are present, there must be the correct number of them.- Parameters:
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.- Returns:
- decoded data
-
decode
public static byte[] decode(java.lang.String s, int options)
Decodes data from Base64 notation, automatically detecting gzip-compressed data and decompressing it.- Parameters:
s
- the string to decodeoptions
- encodeToString options such as URL_SAFE- Returns:
- the decoded data
- Throws:
java.lang.NullPointerException
- ifs
is null
-
-