Tutorial Addendum on Data Encoding - Base64 Encoding
| |
/**
* base-64 encode a byte array
* @param src The byte arrangement to encode
* @returns The base64 encoded result
*/
accessible changeless String
encode(byte src) {
return encode(src, 0, src.length);
}
/**
* base-64 encode a byte array
* @param src The byte arrangement to encode
* @param start The starting index
* @param len The amount of bytes
* @returns The base64 encoded result
*/
accessible changeless String
encode(byte src, int start, int length) {
byte dst = new byte;
int x = 0;
int dstIndex = 0;
int accompaniment = 0; // which burn in pattern
int old = 0; // antecedent byte
int len = 0; // breadth decoded so far
int max = breadth + start;
for (int srcIndex = start; srcIndex<max; srcIndex++) {
x = src;
about-face (++state) {
case 1:
dst = encodeData;
break;
case 2:
dst = encodeData;
break;
case 3:
dst = encodeData;
dst = encodeData;
state = 0;
break;
}
old = x;
if (++len >= 72) {
dst = (byte)
;
len = 0;
}
}
/*
* now apple-pie up the end bytes
*/
switch (state) {
case 1: dst = encodeData;
dst = (byte) = ;
dst = (byte) = ;
break;
case 2: dst = encodeData;
dst = (byte) = ;
break;
}
return new String(dst);
}
/**
* A Base64 decoder. This accomplishing is slow, and
* doesn t handle captivated lines.
* The achievement is amorphous if there are errors in the input.
* @param s a Base64 encoded string
* @returns The byte arrangement eith the decoded result
*/
accessible changeless byte
decode(String s) {
int end = 0; // end state
if (s.endsWith("=")) {
end++;
}
if (s.endsWith("==")) {
end++;
}
int len = (s.length() + 3)/4 * 3 - end;
byte aftereffect = new byte;
int dst = 0;
try {
for(int src = 0; src< s.length(); src++) {
int cipher = charSet.indexOf(s.charAt(src));
if (code == -1) {
break;
}
about-face (src%4) {
case 0:
result = (byte) (code<<2);
break;
case 1:
result |= (byte) ((code>>4) & 0x3);
result = (byte) (code<<4);
break;
case 2:
result |= (byte) ((code>>2) & 0xf);
result = (byte) (code<<6);
break;
case 3:
result |= (byte) (code & 0x3f);
break;
}
}
} bolt (ArrayIndexOutOfBoundsException e) {}
acknowledgment result;
}
/**
* Analysis the decoder and encoder.
* Alarm as <code>Base64 </code>.
*/
accessible changeless void
main(String args) {
System.out.println("encode: " + args + " -> (" +
encode(args) +
")");
System.out.println("decode: " + args + " -> (" +
new String(decode(args)) + ")");
}
}
Exercise: Address a program to analyze the achievement of Baase64 encoding
with UUEncode. I accept UUEncode will accomplish better, because the achievement characters
are computed instead looked up through an array.
|
result, dstindex, encode, length, string, base64, encodedata, array, state, @param, old<, start, static, public, encoding, encoded, decode, code<, srcindex, @returns, return, switch, , dst dstindex, dstindex encodedata, result dst, byte array, dstindex byte, old< <, encodedata old<, public static, code< <, byte code, byte code<, dst byte, base64 encoded, @returns the, dst dstindex encodedata, dstindex encodedata old<, dst dstindex byte, dst byte code<, & 0x3f break, dstindex byte break, string encode byte, encoding base64 encoding, base64 encoded result, public static string, static string encode, data encoding base64, |
Also see ...
UUEnccode AlgorithmUUEncode (Unix to Unix Encoding) was advised to addressthe problem of sending bifold data book by email. It converts any data book to a argument book with a
Sun implemented UUEncode algorithm with 2 classes, UUEncoder and UUDecoder, in sun.miscpackage in the JDK distribution. The afterward program shows you how to use these2 classes: /** * SunUUEnco
/* * @()UUDecoder.java 1.13 00/02/02 * * Absorb 1995 2000 Sun Microsystems, Inc. All Rights Reserved. * * This software is the proprietary advice of Sun Microsystems, * Inc. Use is accountable to au
/* * Now we anticipate its begin, (we ve apparent ^be) so verify it here. */ while ((c != ) && (c != )) { c = inStream.read(); if (c == 1) { bandy new CEF