Base64 encoded JAVA Implementation)

Source: Internet
Author: User
Import java. Io .*;

Public class mimebase64 {
/*
This is a simple base64 encoding program.
By ROC Chen rocanny@163.com.
Base64 uses 65 characters from a subset of the US-ASCII, each representing 6 Characters
Therefore, the base64 Value of "M" is 38, and the binary format is 100110.

For text strings, the encoding process is as follows. For example, "men ":

First convert to US-ASCII value.

"M": decimal 109
"E": decimal 101
"N" decimal 110

Binary:

M 01101101
E 01100101
N 01101110

Three 8-bit connections are 24-bit
011011010110010101101110

Then it is divided into 4 6 digits
011011 010110 010101 101110

Now we get four values in decimal format.
27 22 21 46

The corresponding base64 characters are:
B W v U

The encoding is always based on three characters, resulting in 4 base64 characters.
If only one or two characters are left, use the special character "=" to fill in the 4 characters of base64.
For example, encoding "me"

01101101 01100101
0110110101100101
011011 010110 0101
111111 (and, complement 6)
011011 010110 010100
B W u
B W u = ("=" complement 4 characters)

Therefore, "BWU =" is the base64 Value of "me.

Another example is encoding "M"

01101101
011011 01
111111
011011 010000
B q =
Therefore, "BQ =" is the base64 Value of "M.

It is worth noting that mime requires a maximum of 76 characters in a row.

*/

Static string basetable [] = {
"A", "B", "C", "D", "E", "F", "g", "H", "I", "J ", "k", "L", "M", "n", "O", "P ",
"Q", "r", "S", "T", "U", "V", "W", "X", "Y", "Z ", "A", "B", "C", "D", "E", "F ",
"G", "H", "I", "J", "k", "L", "M", "n", "O", "P ", "Q", "r", "S", "T", "U", "V ",
"W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5 ", "6", "7", "8", "9", "+ ","/"
};

Public static void encode (string filename, bufferedwriter out ){
Try {
File F = new file (filename );
Fileinputstream fin = new fileinputstream (filename );

// Read the file to the byte array
Byte bytes [] = new byte [(INT) (F. Length ()];
Int n = fin. Read (bytes );

If (n <1) return; // NO content

Byte Buf [] = new byte [4]; // base64 character array

Int n3byt = N/3; // 3 bytes
Int nrest = n % 3; // The remaining bytes after the group
Int K = n3byt * 3 ;//
Int linelength = 0; // President
Int I = 0; // pointer

// 3-bytes group...
While (I <k ){
Buf [0] = (byte) (Bytes [I] & 0xfc)> 2 );
Buf [1] = (byte) (Bytes [I] & 0x03) <4) |
(Bytes [I + 1] & 0xf0)> 4 ));
Buf [2] = (byte) (Bytes [I + 1] & 0x0f) <2) |
(Bytes [I + 2] & 0xc0)> 6 ));
Buf [3] = (byte) (Bytes [I + 2] & 0x3f );
Send (Out, basetable [Buf [0]);
Send (Out, basetable [Buf [1]);
Send (Out, basetable [Buf [2]);
Send (Out, basetable [Buf [3]);
/*
The above code can be optimized, but it is hard to understand
Buf [0] = (byte) (B [I]> 2 );
Buf [1] = (byte) (B [I] & 0x03) <4) | (B [I + 1]> 4 ));
Buf [2] = (byte) (B [I + 1] & 0x0f) <2) | (B [I + 2]> 6 ));
Buf [3] = (byte) (B [I + 2] & 0x3f );
Send (Out, basetable [Buf [0] + basetable [Buf [1] +
Basetable [Buf [2] + basetable [Buf [3]);
*/

If (linelength + = 4)> = 76 ){
Send (Out ,"");
Linelength = 0;
}
I + = 3;
}

// Process the tail...
If (nrest = 2 ){
// 2 bytes left
Buf [0] = (byte) (Bytes [k] & 0xfc)> 2 );
Buf [1] = (byte) (Bytes [k] & 0x03) <4) |
(Bytes [k + 1] & 0xf0)> 4 ));
Buf [2] = (byte) (Bytes [k + 1] & 0x0f) <2 );
}
Else if (nrest = 1 ){
// 1 byte left
Buf [0] = (byte) (Bytes [k] & 0xfc)> 2 );
Buf [1] = (byte) (Bytes [k] & 0x03) <4 );
}
If (nrest> 0 ){
// Sending end
If (linelength + = 4)> = 76) Send (Out ,"");
Send (Out, basetable [Buf [0]);
Send (Out, basetable [Buf [1]);
//
If (nrest = 2 ){
Send (Out, basetable [Buf [2]);
}
Else {
Send (Out, "= ");
}
Send (Out, "= ");
}
Out. Flush ();
// The send method used here. Please write it as needed. You can output the result to the console or send an email.
}
Catch (exception e ){
E. printstacktrace ();
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.