A base64 class written in Java

Source: Internet
Author: User

/* Base64.java
*/
Package com. wepull;

Import java. Io .*;

Public class base64 {
Private Static char [] base64encodechars = new char [] {
'A', 'B', 'C', 'D', 'E', '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', '+ ','/'};

Private Static byte [] base64decodechars = new byte [] {
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-62,-1, -1,-1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61,-1,-1,-1,-1,-1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,-1,-1,-1,-1,-1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,-1,-1,-1,-1,-1 };

Public static string encode (byte [] data ){
Stringbuffer sb = new stringbuffer ();
Int Len = data. length;
Int I = 0;
Int B1, B2, B3;
While (I <Len ){
B1 = data [I ++] & 0xff;
If (I = Len)
{
SB. append (base64encodechars [B1 >>> 2]);
SB. append (base64encodechars [(B1 & 0x3) <4]);
SB. append ("= ");
Break;
}
B2 = data [I ++] & 0xff;
If (I = Len)
{
SB. append (base64encodechars [B1 >>> 2]);
SB. append (base64encodechars [(B1 & 0x03) <4) | (B2 & 0xf0) >>> 4)]);
SB. append (base64encodechars [(B2 & 0x0f) <2]);
SB. append ("= ");
Break;
}
B3 = data [I ++] & 0xff;
SB. append (base64encodechars [B1 >>> 2]);
SB. append (base64encodechars [(B1 & 0x03) <4) | (B2 & 0xf0) >>> 4)]);
SB. append (base64encodechars [(B2 & 0x0f) <2) | (B3 & 0xc0) >>> 6)]);
SB. append (base64encodechars [B3 & 0x3f]);
}
Return sb. tostring ();
}

Public static byte [] Decode (string Str) throws unsupportedencodingexception {
Stringbuffer sb = new stringbuffer ();
Byte [] DATA = Str. getbytes ("US-ASCII ");
Int Len = data. length;
Int I = 0;
Int B1, B2, B3, B4;
While (I <Len ){
/* B1 */
Do {
B1 = base64decodechars [DATA [I ++];
} While (I <Len & b1 =-1 );
If (b1 =-1) break;
/* B2 */
Do {
B2 = base64decodechars [DATA [I ++];
} While (I <Len & b2 =-1 );
If (b2 =-1) break;
SB. append (char) (b1 <2) | (B2 & 0x30) >>> 4 )));
/* B3 */
Do {
B3 = data [I ++];
If (B3 = 61) return sb. tostring (). getbytes ("ISO-8859-1 ");
B3 = base64decodechars [B3];
} While (I <Len & B3 =-1 );
If (B3 =-1) break;
SB. append (char) (B2 & 0x0f) <4) | (B3 & 0x3c) >>> 2 )));
/* B4 */
Do {
B4 = data [I ++];
If (B4 = 61) return sb. tostring (). getbytes ("ISO-8859-1 ");
B4 = base64decodechars [B4];
} While (I <Len & B4 =-1 );
If (B4 =-1) break;
SB. append (char) (B3 & 0x03) <6) | B4 ));
}
Return sb. tostring (). getbytes ("ISO-8859-1 ");
}
}

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.