Class Base64
Base64 is a way of encoding 8-bit characters using only ASCII printable characters similar to UUENCODE. UUENCODE includes a filename where BASE64 does not. The spec is described in RFC 2045. Base64 is a scheme where 3 bytes are concatenated, then split to form 4 groups of 6-bits each; and each 6-bits gets translated to an encoded printable ASCII character, via a table lookup. An encoded string is therefore longer than the original by about 1/3. The "=" character is used to pad the end. Base64 is used, among other things, to encode the user:password string in an Authorization: header for HTTP. Don't confuse Base64 with x-www-form-urlencoded which is handled by Java.net.URLEncoder.encode/decode
If you don't like this code, there is another implementation at http://www.ruffboy.com/download.htm Sun has an undocumented method called sun.misc.Base64Encoder.encode. You could use hex, simpler to code, but not as compact.
If you wanted to encode a giant file, you could do it in large chunks that are even multiples of 3 bytes, except for the last chunk, and append the outputs.
To encode a string, rather than binary data java.net.URLEncoder may be better. See printable characters in the Java glossary for a discussion of the differences.
Base 64 armouring uses only the characters A-Z a-z 0-9 +/=. This makes it suitable for encoding binary data as SQL strings, that will work no matter what the encoding. Unfortunately + / and = all have special meaning in URLs.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbyte[]
decode a well-formed complete Base64 string back into an array of bytes.static void
display
(byte[] b) debug display array as if there were 8-bit Latin-1encode
(byte[] b) Encode an arbitrary array of bytes as Base64 printable ASCII.static void
test drivervoid
setLineLength
(int length) determines how long the lines are that are generated by encode.void
setLineSeparator
(String lineSeparator) How lines are separated.static void
show
(byte[] b) debug display array as hex
-
Field Details
-
DEBUGGING
public static final boolean DEBUGGINGused to disable test driver- See Also:
-
-
Constructor Details
-
Base64
public Base64()constructor
-
-
Method Details
-
display
public static void display(byte[] b) debug display array as if there were 8-bit Latin-1- Parameters:
b
- byte array to display
-
show
public static void show(byte[] b) debug display array as hex- Parameters:
b
- byte array to display
-
setLineSeparator
How lines are separated. Ignored by decode.- Parameters:
lineSeparator
- may be "" but not null. Usually contains only a combination of chars \n and \r. Could be any chars not in set A-Z a-z 0-9 + /.
-
decode
decode a well-formed complete Base64 string back into an array of bytes. It must have an even multiple of 4 data characters (not counting \n), padded out with = as needed.- Parameters:
s
- base64-encoded string- Returns:
- plaintext as a byte array.
-
encode
Encode an arbitrary array of bytes as Base64 printable ASCII. It will be broken into lines of 72 chars each. The last line is not terminated with a line separator. The output will always have an even multiple of data characters, exclusive of \n. It is padded out with =.- Parameters:
b
- byte array to encode, typically produced by a ByteArrayOutputStream- Returns:
- base-64 encoded String, not char[] or byte[].
-
setLineLength
public void setLineLength(int length) determines how long the lines are that are generated by encode. Ignored by decode.- Parameters:
length
- 0 means no newlines inserted. Must be a multiple of 4.
-
main
test driver- Parameters:
args
- not used
-