package sony.utils;
import java.io.*;
import java.net.*;
public final class codes {
public final static byte[] base64encode(byte[] bytedata) {
if (bytedata == null)
return null;
int isrcidx; // index into source (bytedata)
int idestidx; // index into destination (bytedest)
byte bytedest[] = new byte[((bytedata.length + 2) / 3) * 4];
for (isrcidx = 0, idestidx = 0; isrcidx < bytedata.length – 2; isrcidx += 3) {
bytedest[idestidx++] = (byte) ((bytedata[isrcidx] >>> 2) & 077);
bytedest[idestidx++] =
(byte) ((bytedata[isrcidx + 1] >>> 4) & 017 | (bytedata[isrcidx] << 4) & 077);
bytedest[idestidx++] =
(byte) ((bytedata[isrcidx + 2] >>> 6)
& 003
| (bytedata[isrcidx + 1] << 2)
& 077);
bytedest[idestidx++] = (byte) (bytedata[isrcidx + 2] & 077);
}
if (isrcidx < bytedata.length) {
bytedest[idestidx++] = (byte) ((bytedata[isrcidx] >>> 2) & 077);
if (isrcidx < bytedata.length – 1) {
bytedest[idestidx++] =
(byte) ((bytedata[isrcidx + 1] >>> 4) & 017 | (bytedata[isrcidx] << 4) & 077);
bytedest[idestidx++] = (byte) ((bytedata[isrcidx + 1] << 2) & 077);
} else
bytedest[idestidx++] = (byte) ((bytedata[isrcidx] << 4) & 077);
}
for (isrcidx = 0; isrcidx < idestidx; isrcidx++) {
if (bytedest[isrcidx] < 26)
bytedest[isrcidx] = (byte) (bytedest[isrcidx] + a);
else
if (bytedest[isrcidx] < 52)
bytedest[isrcidx] = (byte) (bytedest[isrcidx] + a – 26);
else
if (bytedest[isrcidx] < 62)
bytedest[isrcidx] = (byte) (bytedest[isrcidx] + 0 – 52);
else
if (bytedest[isrcidx] < 63)
bytedest[isrcidx] = (byte) +;
else
bytedest[isrcidx] = (byte) /;
}
for (; isrcidx < bytedest.length; isrcidx++)
bytedest[isrcidx] = (byte) =;
return bytedest;
}
public final static string base64encode(string strinput) {
if (strinput == null)
return null;
return base64encode(strinput,"gb2312");
}
public final static string base64encode(string strinput,string charset) {
if (strinput == null)
return null;
string stroutput=null;
byte bytedata[] = new byte[strinput.length()];
try {
//strinput.getbytes(0, strinput.length(), bytedata, 0);
bytedata = strinput.getbytes(charset);
stroutput=new string(base64encode(bytedata),charset);
//stroutput=new string(base64encode(bytedata),0);
} catch (unsupportedencodingexception e) {
return null;
}
return stroutput;
}
/**
* 此处插入方法说明。
* 创建日期:(2000-11-4 18:27:35)
* @param steam java.io.inputstream
* @param charset java.lang.string
*/
public final static string base64encode(inputstream in, string charset) {
try {
int c;
byte[] buff = new byte[1024];
bytearrayoutputstream out = new bytearrayoutputstream(2048);
while ((c = in.read(buff, 0, 1024)) != -1) {
out.write(buff, 0, c);
//index+=1024;
//out.write(c);
//attachcontent+=ss;
}
in.close();
out.flush();
byte[] tmp2 = codes.base64encode(out.tobytearray());
out.close();
return new string(tmp2,charset);
}
catch (ioexception e) {
return "";
}
}/**
* 此处插入方法说明。
* 创建日期:(2000-11-3 23:31:04)
* @return java.lang.string
* @param strin java.lang.string
*/
public final static string chunksplit(string strin) {
return chunksplit(strin,76);
}/**
* 此处插入方法说明。
* 创建日期:(2000-11-3 23:31:04)
* @return java.lang.string
* @param strin java.lang.string
*/
public final static string chunksplit(string strin,int splitlen) {
int index=0;
string strout="";
while(index+splitlen<strin.length()){
strout+=strin.substring(index,index+splitlen)+"\n";
index+=splitlen;
}
if(index<strin.length()){
strout+=strin.substring(index);
}
return strout;
}
}
