public class Base64
extends java.lang.Object
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 \\p{Lower} 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.
Works exactly like Base64 except avoids using the characters + / and =. This means Base64u-encoded data can be used either URLCoded or plain in URL-Encoded contexts such as GET, PUT or URLs. You can treat the output either as not needing encoding or already URLEncoded.
Base64 ASCII armouring. Encode arbitrary binary into printable ASCII using BASE64 encoding. very loosely based on the
Base64 Reader by: Dr. Mark Thornton
Optrak Distribution Software Ltd. http://www.optrak.co.uk and Kevin Kelley's
http://www.ruralnet.net/~kelley/java/Base64.java
real.sergeych@gmail.com: Added some utility functions to simplify its usage.
| Modifier and Type | Field and Description |
|---|---|
protected int[] |
charToValue
binary value encoded by a given letter of the alphabet 0..63, overridden in Base64u.
|
protected static int[] |
cv
binary value encoded by a given letter of the alphabet 0..63.
|
protected static boolean |
DEBUGGING
used to disable test driver.
|
protected static int |
IGNORE
Marker value for chars we just ignore, e.g.
|
protected int |
lineLength
max chars per line, excluding lineSeparator.
|
protected java.lang.String |
lineSeparator
how we separate lines, e.g.
|
protected static int |
PAD
Marker for = trailing pad.
|
protected char |
spec1
special character 1, will be - in Base64u.
|
protected char |
spec2
special character 2, will be in in Base64u.
|
protected char |
spec3
special character 3, will be * in Base64u.
|
protected char[] |
valueToChar
letter of the alphabet used to encode binary values 0..63, overridden in Base64u.
|
protected static char[] |
vc
letter of the alphabet used to encode binary values 0..63
|
| Constructor and Description |
|---|
Base64()
constructor.
|
| Modifier and Type | Method and Description |
|---|---|
byte[] |
decode(java.lang.String s)
decode a well-formed complete Base64 string back into an array of bytes.
|
static byte[] |
decodeCompactString(java.lang.String s) |
static byte[] |
decodeLines(java.lang.String s)
Decodes a byte array from Base64 format and ignores line separators, tabs and blanks.
|
java.lang.String |
encode(byte[] b)
Encode an arbitrary array of bytes as Base64 printable ASCII.
|
static java.lang.String |
encodeCompactString(byte[] data) |
static java.lang.String |
encodeLines(byte[] data) |
static java.lang.String |
encodeString(byte[] data) |
static void |
main(java.lang.String[] args)
test driver.
|
void |
setLineLength(int length)
determines how long the lines are that are generated by encode.
|
void |
setLineSeparator(java.lang.String lineSeparator)
How lines are separated.
|
static void |
show(byte[] b)
debug display array as hex.
|
protected static final boolean DEBUGGING
protected static final int IGNORE
protected static final int PAD
protected static int[] cv
protected static char[] vc
protected java.lang.String lineSeparator
protected char[] valueToChar
protected char spec1
protected char spec2
protected char spec3
protected int[] charToValue
protected int lineLength
public static byte[] decodeLines(java.lang.String s)
s - A Base64 String to be decoded.java.lang.IllegalArgumentException - If the input is not valid Base64 encoded data.public static java.lang.String encodeString(byte[] data)
public static java.lang.String encodeCompactString(byte[] data)
public static java.lang.String encodeLines(byte[] data)
public static void show(byte[] b)
b - byte array to display.public static void main(java.lang.String[] args)
args - not usedpublic byte[] decode(java.lang.String s)
s - base64-encoded stringpublic java.lang.String encode(byte[] b)
b - byte array to encode, typically produced by a ByteArrayOutputStream.public final void setLineLength(int length)
length - 0 means no newlines inserted. Must be a multiple of 4.public final void setLineSeparator(java.lang.String lineSeparator)
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 \\p{Lower} 0-9 + /.public static byte[] decodeCompactString(java.lang.String s)