|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.csource.common.Base64
public class Base64
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 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. version 1.4 2002 February 15 -- correct bugs with uneven line lengths, allow you to configure line separator. now need Base64 object and instance methods. new mailing address. version 1.3 2000 September 12 -- fix problems with estimating output length in encode version 1.2 2000 September 09 -- now handles decode as well. version 1.1 1999 December 04 -- more symmetrical encoding algorithm. more accurate StringBuffer allocation size. version 1.0 1999 December 03 -- posted in comp.lang.java.programmer. Futures Streams or files.
Constructor Summary | |
---|---|
Base64()
|
|
Base64(char chPlus,
char chSplash,
char chPad,
int lineLength)
|
|
Base64(int lineLength)
|
Method Summary | |
---|---|
byte[] |
decode(java.lang.String s)
decode a well-formed complete Base64 string back into an array of bytes. |
byte[] |
decodeAuto(java.lang.String s)
decode a well-formed complete Base64 string back into an array of bytes. |
static void |
display(byte[] b)
debug display array |
java.lang.String |
encode(byte[] b)
Encode an arbitrary array of bytes as Base64 printable ASCII. |
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 |
static void |
test()
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Base64()
public Base64(char chPlus, char chSplash, char chPad, int lineLength)
public Base64(int lineLength)
Method Detail |
---|
public java.lang.String encode(byte[] b) throws java.io.IOException
java.io.IOException
public byte[] decodeAuto(java.lang.String s)
public byte[] decode(java.lang.String s)
public void setLineLength(int length)
length
- 0 means no newlines inserted. Must be a multiple of 4.public 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 a-z 0-9 + /.public static void show(byte[] b)
public static void display(byte[] b)
public static void main(java.lang.String[] args)
public static void test()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |