java-base64

1.encode

  

    public static String encode(byte[] bValue, String encode) {

        ByteArrayOutputStream o = new ByteArrayOutputStream();
byte d[] = new byte[];
try {
int count = ; for(byte x[] = bValue; count < x.length;) {
byte c = x[count];
count++;
d[] = (byte) ((c & 0xfc) >> );
d[] = (byte) ((c & ) << ); if(count < x.length) {
c = x[count];
count++;
d[] = (byte) (d[] + (byte) ((c & 0xf0) >> ));
d[] = (byte) ((c & 0xf) << ); if(count < x.length) {
c = x[count];
count++;
d[] = (byte) (d[] + ((c & 0xc0) >> ));
d[] = (byte) (c & 0x3f);
}
else {
d[] = ;
}
}
else {
d[] = ;
d[] = ;
} int n = ; while(n <= ) {
o.write(strTableBase64.charAt(d[n]));
n++;
}
}
}
catch(StringIndexOutOfBoundsException e) {
logger.error(e.toString());
} String temp = null; if(o != null){ try {
temp = new String(o.toByteArray(), encode);
} catch (UnsupportedEncodingException e1) {
logger.error(e1.toString(), e1);
} try {
o.close();
} catch (IOException e) {
o = null;
} } return temp;
}

2.decode

 public static byte[] decode(String strValue, String encode) {
ByteArrayOutputStream o = new ByteArrayOutputStream();
byte d[] = new byte[]; try{
int count = ;
byte x[] = null;
try {
x = strValue.getBytes(encode);
} catch (UnsupportedEncodingException e) {
logger.error(e.toString(), e);
} do {
if(count >= x.length) {
break;
} for(int n = ; n <= ; n++){
if(count >= x.length) {
d[n] = ;
}
else {
int y = strTableBase64.indexOf(x[count]); if(y < ) {
y = ;
} d[n] = (byte) y;
} count++;
} o.write((byte) (((d[] & 0x3f) << ) + ((d[] & 0x30) >> ))); if(d[] != ) {
o.write((byte) (((d[] & 0xf) << ) + ((d[] & 0x3c) >> ))); if(d[] != ) {
o.write( (byte) ( ((d[] & ) << ) + (d[] & 0x3f)));
}
}
} while(true);
}
catch(StringIndexOutOfBoundsException e) {
logger.error(e.toString());
} if(o != null){
d = o.toByteArray();
try {
o.close();
o = null;
} catch (IOException e) {
logger.error(e.toString(), e);
} }
return d;
}
上一篇:python3 输出系统信息


下一篇:集合判断null