Package org.eclipse.jgit.util
Class Base64
java.lang.Object
org.eclipse.jgit.util.Base64
Encodes and decodes to and from Base64 notation.
I am placing this code in the Public Domain. Do with it as you will. This software comes with no guarantees or warranties but with plenty of well-wishing instead! Please visit http://iharder.net/base64 periodically to check for updates or to contribute improvements.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final byte[]
Translates a Base64 value to either its 6-bit reconstruction value or a negative number indicating some other meaning.private static final byte[]
The 64 valid Base64 values.private static final byte
The equals sign (=) as a byte.private static final byte
Indicates equals sign in encoding.private static final byte
Indicates an invalid byte during decoding.private static final byte
Indicates white space in encoding. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]
decode
(byte[] source, int off, int len) Low-level decoding ASCII characters from a byte array.static byte[]
Decodes data from Base64 notation.private static int
decode4to3
(byte[] source, int srcOffset, byte[] destination, int destOffset) Decodes four bytes from array source and writes the resulting bytes (up to three of them) to destination.private static void
encode3to4
(byte[] source, int srcOffset, int numSigBytes, byte[] destination, int destOffset) Encodes up to three bytes of the array source and writes the resulting four Base64 bytes to destination.static String
encodeBytes
(byte[] source) Encodes a byte array into Base64 notation.static String
encodeBytes
(byte[] source, int off, int len) Encodes a byte array into Base64 notation.
-
Field Details
-
EQUALS_SIGN
private static final byte EQUALS_SIGNThe equals sign (=) as a byte.- See Also:
-
EQUALS_SIGN_DEC
private static final byte EQUALS_SIGN_DECIndicates equals sign in encoding.- See Also:
-
WHITE_SPACE_DEC
private static final byte WHITE_SPACE_DECIndicates white space in encoding.- See Also:
-
INVALID_DEC
private static final byte INVALID_DECIndicates an invalid byte during decoding.- See Also:
-
ENC
private static final byte[] ENCThe 64 valid Base64 values. -
DEC
private static final byte[] DECTranslates a Base64 value to either its 6-bit reconstruction value or a negative number indicating some other meaning. The table is only 7 bits wide, as the 8th bit is discarded during decoding.
-
-
Constructor Details
-
Base64
private Base64()Defeats instantiation.
-
-
Method Details
-
encode3to4
private static void encode3to4(byte[] source, int srcOffset, int numSigBytes, byte[] destination, int destOffset) 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 accommodate 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.- 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 put
-
encodeBytes
Encodes a byte array into Base64 notation.- Parameters:
source
- The data to convert- Returns:
- encoded base64 representation of source.
-
encodeBytes
Encodes a byte array into Base64 notation.- Parameters:
source
- The data to convertoff
- Offset in array where conversion should beginlen
- Length of data to convert- Returns:
- encoded base64 representation of source.
-
decode4to3
private static int decode4to3(byte[] source, int srcOffset, byte[] destination, int destOffset) 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 accommodate 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.- Parameters:
source
- the array to convertsrcOffset
- the index where conversion beginsdestination
- the array to hold the conversiondestOffset
- the index where output will be put- Returns:
- the number of decoded bytes converted
-
decode
public static byte[] decode(byte[] source, int off, int len) Low-level decoding ASCII characters from a byte array.- Parameters:
source
- The Base64 encoded dataoff
- The offset of where to begin decodinglen
- The length of characters to decode- Returns:
- decoded data
- Throws:
IllegalArgumentException
- the input is not a valid Base64 sequence.
-
decode
Decodes data from Base64 notation.- Parameters:
s
- the string to decode- Returns:
- the decoded data
-