?? base64.java
字號:
package Base64;
import java.io.PrintStream;
public class Base64
{
public Base64()
{
}
public static String encode(String s)
{
return getString(encode(getBinaryBytes(s)));
}
public static byte[] encode(byte abyte0[])
{
int l = abyte0.length;
StringBuffer stringbuffer = new StringBuffer((l / 3 + 1) * 4);
for(int i1 = 0; i1 < l; i1++)
{
int i = abyte0[i1] >> 2 & 0x3f;
stringbuffer.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(i));
i = abyte0[i1] << 4 & 0x3f;
if(++i1 < l)
i |= abyte0[i1] >> 4 & 0xf;
stringbuffer.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(i));
if(i1 < l)
{
int j = abyte0[i1] << 2 & 0x3f;
if(++i1 < l)
j |= abyte0[i1] >> 6 & 3;
stringbuffer.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(j));
} else
{
i1++;
stringbuffer.append('=');
}
if(i1 < l)
{
int k = abyte0[i1] & 0x3f;
stringbuffer.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(k));
} else
{
stringbuffer.append('=');
}
}
return getBinaryBytes(stringbuffer.toString());
}
public static String decode(String s)
{
return getString(decode(getBinaryBytes(s)));
}
public static byte[] decode(byte abyte0[])
{
int k = abyte0.length;
StringBuffer stringbuffer = new StringBuffer((k * 3) / 4);
for(int l = 0; l < k; l++)
{
int i = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf(abyte0[l]);
l++;
int j = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf(abyte0[l]);
i = i << 2 | j >> 4 & 3;
stringbuffer.append((char)i);
if(++l < k)
{
i = abyte0[l];
if(61 == i)
break;
i = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf((char)i);
j = j << 4 & 0xf0 | i >> 2 & 0xf;
stringbuffer.append((char)j);
}
if(++l >= k)
continue;
j = abyte0[l];
if(61 == j)
break;
j = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf((char)j);
i = i << 6 & 0xc0 | j;
stringbuffer.append((char)i);
}
return getBinaryBytes(stringbuffer.toString());
}
private static String getString(byte abyte0[])
{
StringBuffer stringbuffer = new StringBuffer();
for(int i = 0; i < abyte0.length; i++)
stringbuffer.append((char)abyte0[i]);
return stringbuffer.toString();
}
private static byte[] getBinaryBytes(String s)
{
byte abyte0[] = new byte[s.length()];
for(int i = 0; i < abyte0.length; i++)
abyte0[i] = (byte)s.charAt(i);
return abyte0;
}
public static void main(String args[])
{
String s;
if(args.length > 0)
s = args[0];
else
s = "101";
System.out.println("Encoding string [" + s + "]");
s = encode(s);
System.out.println("Encoded string [" + s + "]");
s = decode(s);
System.out.println("Decoded string [" + s + "]");
System.out.println();
byte abyte0[] = getBinaryBytes(s);
System.out.println("Encoding bytes [" + getString(abyte0) + "]");
abyte0 = encode(abyte0);
System.out.println("Encoded bytes [" + getString(abyte0) + "]");
abyte0 = decode(abyte0);
System.out.println("Decoded bytes [" + getString(abyte0) + "]");
}
private static final int fillchar = 61;
private static final String cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -