Change Log:
- v1.3.6 - Fixed
OutputStream.flush()so that 'position' is reset. - v1.3.5 - Added flag to turn on and off line breaks. Fixed bug in input stream where last buffer being read, if not completely full, was not returned.
- v1.3.4 - Fixed when "improperly padded stream" error was thrown at the wrong time.
- v1.3.3 - Fixed I/O streams which were totally messed up.
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/xmlizable periodically to check for updates or to contribute improvements.
Modified by jrandom for i2p, using safeEncode / safeDecode to create filesystem and URL safe base64 values (replacing / with ~, and + with -).
Field Summary
Modifier and TypeFieldDescriptionprivate static final byte[]The 64 valid Base64 values.private static final byte[]The 64 valid Base64 values for I2P.static final StringThe I2P Alphabet.private 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 byteThe DECODABET value of the equals sign.private static final byteThe equals sign (=) as a byte.private static final intMaximum line length (76) of Base64 output.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionprivate static voiddecode(InputStream in, OutputStream out) Base64-decodes one stream into another.static byte[]Decodes data from Base64 notation using the I2P alphabet.static byte[]Decodes data from Base64 notation using the I2P alphabet.private static byte[]Decodes Base64 content in string format and returns the decoded byte array.private static intdecode4to3(byte d0, byte d1, byte d2, byte d3, byte[] destination, int destOffset) Decodes four Base64 chars, already mapped throughDECODABET, and writes the resulting bytes (up to three of them) to destination at destOffset.static StringDecodes data from Base64 notation and returns it as a string.static Stringencode(byte[] source) Output will be a multiple of 4 chars, including 0-2 trailing '='.static Stringencode(byte[] source, boolean useStandardAlphabet) Output will be a multiple of 4 chars, including 0-2 trailing '='.static Stringencode(byte[] source, int off, int len) Output will be a multiple of 4 chars, including 0-2 trailing '='.static Stringencode(byte[] source, int off, int len, boolean useStandardAlphabet) Output will be a multiple of 4 chars, including 0-2 trailing '='.private static voidencode(InputStream in, OutputStream out) Base64-encodes one stream into another.static StringOutput will be a multiple of 4 chars, including 0-2 trailing '=' As of 0.9.14, encodes the UTF-8 encoding of source.private static voidencode3to4(byte[] source, int srcOffset, int numSigBytes, StringBuilder buf, byte[] alpha) Appends the encoding of the significant source bytes to buf, using alpha.private static voidencodeBytes(byte[] source, int off, int len, boolean breakLines, StringBuilder out, byte[] alpha) Encodes a byte array into Base64 notation.private static voidhelp()Exits 1, never returns.static voidCommand-line tool for encoding and decoding Base64.private static byte[]read(InputStream in) Reads a whole stream.private static voidRuns the command line encoder and decoder.private static byte[]safeDecode(String source, boolean useStandardAlphabet) Same as decode, except from a filesystem / URL friendly set of characters, replacing / with ~, and + with -.private static StringsafeEncode(byte[] source, int off, int len, boolean useStandardAlphabet) Same as encodeBytes, except uses a filesystem / URL friendly set of characters, replacing / with ~, and + with -.private static byte[]Decodes data from Base64 notation.
Field Details
MAX_LINE_LENGTH
private static final int MAX_LINE_LENGTHMaximum line length (76) of Base64 output.- See Also:
EQUALS_SIGN
ALPHABET
private static final byte[] ALPHABETThe 64 valid Base64 values.ALPHABET_I2P
ALPHABET_ALT
private static final byte[] ALPHABET_ALTThe 64 valid Base64 values for I2P.DECODABET
private static final byte[] DECODABETTranslates a Base64 value to either its 6-bit reconstruction value or a negative number indicating some other meaning. As of 0.9.14 this is the decoding for the I2P alphabet. SeesafeDecode(String, boolean).DECODABET_EQUALS
private static final byte DECODABET_EQUALSThe DECODABET value of the equals sign. No other character maps to it, so a decoded value of -1 means the char was '='.- See Also:
Constructor Details
Base64
private Base64()Defeats instantiation.
Method Details
encode
Output will be a multiple of 4 chars, including 0-2 trailing '=' As of 0.9.14, encodes the UTF-8 encoding of source. Prior to that, used the platform's encoding.- Parameters:
source- the string to encode, if null will return ""- Returns:
- the Base64 encoded string
encode
Output will be a multiple of 4 chars, including 0-2 trailing '='.- Parameters:
source- the byte array to encode, if null will return ""- Returns:
- the Base64 encoded string
encode
Output will be a multiple of 4 chars, including 0-2 trailing '='.- Parameters:
source- the byte array to encode, if null will return ""off- the offset in the source arraylen- the length of data to encode- Returns:
- the Base64 encoded string
encode
Output will be a multiple of 4 chars, including 0-2 trailing '='.- Parameters:
source- the byte array to encode, if null will return ""useStandardAlphabet- Warning, must be false for I2P compatibility- Returns:
- the Base64 encoded string
encode
Output will be a multiple of 4 chars, including 0-2 trailing '='.- Parameters:
source- the byte array to encode, if null will return ""off- the offset in the source arraylen- the length of data to encodeuseStandardAlphabet- Warning, must be false for I2P compatibility- Returns:
- the Base64 encoded string
decode
Decodes data from Base64 notation using the I2P alphabet.As of 0.9.14, does not require trailing '=' if remaining bits are zero. Prior to that, trailing 1, 2, or 3 chars were ignored.
As of 0.9.14, trailing garbage after an '=' will cause an error. Prior to that, it was ignored.
As of 0.9.14, whitespace will cause an error. Prior to that, it was ignored.
- Parameters:
s- Base 64 encoded string using the I2P alphabet A-Z, a-z, 0-9, -, ~- Returns:
- the decoded data, null on error
decode
Decodes data from Base64 notation using the I2P alphabet.- Parameters:
s- the Base64 encoded stringuseStandardAlphabet- Warning, must be false for I2P compatibility- Returns:
- the decoded data, null on error
main
Command-line tool for encoding and decoding Base64.- Parameters:
args- a subcommand and its arguments; empty prints the help
runApp
Runs the command line encoder and decoder.- Parameters:
args- the command line arguments
read
Reads a whole stream.- Parameters:
in- the stream to read- Returns:
- the bytes read
- Throws:
IOException- if the stream cannot be read
encode
Base64-encodes one stream into another.- Parameters:
in- the bytes to encodeout- where the encoded text is written- Throws:
IOException- if either stream fails
decode
Base64-decodes one stream into another.- Parameters:
in- the encoded text to decodeout- where the bytes are written- Throws:
IOException- if either stream fails
help
private static void help()Exits 1, never returns.encode3to4
private static void encode3to4(byte[] source, int srcOffset, int numSigBytes, StringBuilder buf, byte[] alpha) Appends the encoding of the significant source bytes to buf, using alpha.- Parameters:
source- the array containing the bytes to encodesrcOffset- the index in source where the bytes to encode beginnumSigBytes- number of significant bytes to encode, 1-3, any other value appends nothingbuf- the StringBuilder the four encoded characters are appended toalpha- the 64 character alphabet to index, plus its '=' padding
safeEncode
Same as encodeBytes, except uses a filesystem / URL friendly set of characters, replacing / with ~, and + with -.- Parameters:
source- the byte array to encodeoff- index in source where encoding beginslen- number of bytes to encodeuseStandardAlphabet- true for the standard +/ alphabet, false for the I2P -~ alphabet- Returns:
- the Base64 encoded string
safeDecode
Same as decode, except from a filesystem / URL friendly set of characters, replacing / with ~, and + with -.- Parameters:
source- the encoded string to decode, may be nulluseStandardAlphabet- true if source uses the standard +/ alphabet, false for the I2P -~ alphabet- Returns:
- the decoded data, null on error
encodeBytes
private static void encodeBytes(byte[] source, int off, int len, boolean breakLines, StringBuilder out, byte[] alpha) Encodes a byte array into Base64 notation.- Parameters:
source- The data to convertoff- Offset in array where conversion should beginlen- Length of data to convertbreakLines- Break lines at 80 characters or less.out- the StringBuilder the encoded text is appended toalpha- the 64 character alphabet to index
decode4to3
private static int decode4to3(byte d0, byte d1, byte d2, byte d3, byte[] destination, int destOffset) Decodes four Base64 chars, already mapped throughDECODABET, and writes the resulting bytes (up to three of them) to destination at destOffset. This method does not check that destination is large enough for destOffset + 3.- Parameters:
d0- the DECODABET value of the first source chard1- the DECODABET value of the second source chard2- the DECODABET value of the third char, or '=' paddingd3- the DECODABET value of the fourth char, or '=' paddingdestination- the array to hold the conversiondestOffset- the index where output will be put- Returns:
- the number of decoded bytes converted 1-3, or -1 on error, never zero
standardDecode
Decodes data from Base64 notation. As of 0.9.14, this uses the I2P alphabet, so it is not "standard".- Parameters:
s- the string to decode- Returns:
- the decoded data, null on error
decodeToString
Decodes data from Base64 notation and returns it as a string. Equivalent to callingnew String( decode( s ) )As of 0.9.14, decodes as UTF-8. Prior to that, it used the platform's encoding. For best results, decoded data should be 7 bit.
As of 0.9.14, does not require trailing '=' if remaining bits are zero. Prior to that, trailing 1, 2, or 3 chars were ignored.
As of 0.9.14, trailing garbage after an '=' will cause an error. Prior to that, it was ignored.
As of 0.9.14, whitespace will cause an error. Prior to that, it was ignored.
- Parameters:
s- the string to decode- Returns:
- The data as a string, or null on error
decode
Decodes Base64 content in string format and returns the decoded byte array. The chars are looked up in the DECODABET directly, so no UTF-8 byte[] copy of the string is made. The caller must have verified that every char is ASCII.As of 0.9.14, does not require trailing '=' if remaining bits are zero. Prior to that, trailing 1, 2, or 3 chars were ignored.
As of 0.9.14, trailing garbage after an '=' will cause an error. Prior to that, it was ignored.
As of 0.9.14, whitespace will cause an error. Prior to that, it was ignored.
- Parameters:
source- The Base64 encoded dataoff- The offset of where to begin decodinglen- The length of characters to decode- Returns:
- decoded data, null on error