public class Base64 extends Object
Random rand = new Random(); // All separators, including the system one. String sysSep = System.getProperty("line.separator"); String[] seps = {null, "", "\r\n", "\n", "\r", sysSep}; sun.misc.BASE64Encoder sunEnc = new sun.misc.BASE64Encoder(); for (int i = 0; i < 100000; i++) { byte[] ba1 = new byte[(int) rand.nextInt(100000)]; rand.nextBytes(ba1); String sep = seps[rand.nextInt(seps.length)]; // Pick a random separator String s = Base64.encode(ba1, sep); byte[] ba2 = Base64.decode(s); if (Arrays.equals(ba1, ba2) == false) { System.out.println("Integrity Failure!!"); System.exit(0); } String sunS = sunEnc.encode(ba1); if (sep != null && sep.equals(sysSep)) { // trim since we aren't handling last separator exactly the same and number of white spaces doesn't matter anyway. if (sunS.trim().equals(s.trim()) == false) { System.out.println("Sun conformance Failure!!\n\"" + s + "\"\n\n != \n\n\"" + sunS + "\""); System.exit(0); } } } System.out.println("Success!!"); System.exit(0);Licence: Free to use for any legal purpose, though sending an email to base64 @ miginfocom . com to tell me you're using it will ut a smile on my face! :)
Constructor and Description |
---|
Base64() |
Modifier and Type | Method and Description |
---|---|
static byte[] |
decode(String s)
Decodes a BASE64 encoded string.
|
static String |
encode(byte[] bArr,
String lineSep)
Encodes a raw byte array into a BASE64 string representation i accordance with RFC 2045.
|
public static final String encode(byte[] bArr, String lineSep)
bArr
- The bytes to convert. If null
or length 0, "" will be returned.lineSep
- Optional line separator after 76 characters, unless end of file.
Max 2 chars length. If null
or length 0 no line breaks will be inserted.null
.public static final byte[] decode(String s)
s
- The string. null
or "" will return an empty array.null
but may be of length 0.