欢迎光临
我们一直在努力

一个进行Base64编码的类。-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

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;
}
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 一个进行Base64编码的类。-JSP教程,Java技巧及代码
分享到: 更多 (0)