Base64编码类

package com.rd.p2p.common.util.code;


import java.io.IOException;
import java.io.OutputStream;


/**
 * <p>
 * Description:base64编码类
 * </p>
 */
public class BASE64Encoder extends CharacterEncoder {


public BASE64Encoder() {
}


protected int bytesPerAtom() {
return 3;
}


protected int bytesPerLine() {
return 57;
}


protected void encodeAtom(OutputStream outputstream, byte abyte0[], int i, int j) throws IOException {
if (j == 1) {
byte byte0 = abyte0[i];
int k = 0;
outputstream.write(PEM_ARRAY[byte0 >>> 2 & 0x3f]);
outputstream.write(PEM_ARRAY[(byte0 << 4 & 0x30) + (k >>> 4 & 0xf)]);
outputstream.write(61);
outputstream.write(61);
} else if (j == 2) {
byte byte1 = abyte0[i];
byte byte3 = abyte0[i + 1];
int l = 0;
outputstream.write(PEM_ARRAY[byte1 >>> 2 & 0x3f]);
outputstream.write(PEM_ARRAY[(byte1 << 4 & 0x30) + (byte3 >>> 4 & 0xf)]);
outputstream.write(PEM_ARRAY[(byte3 << 2 & 0x3c) + (l >>> 6 & 3)]);
outputstream.write(61);
} else {
byte byte2 = abyte0[i];
byte byte4 = abyte0[i + 1];
byte byte5 = abyte0[i + 2];
outputstream.write(PEM_ARRAY[byte2 >>> 2 & 0x3f]);
outputstream.write(PEM_ARRAY[(byte2 << 4 & 0x30) + (byte4 >>> 4 & 0xf)]);
outputstream.write(PEM_ARRAY[(byte4 << 2 & 0x3c) + (byte5 >>> 6 & 3)]);
outputstream.write(PEM_ARRAY[byte5 & 0x3f]);
}
}


private final static char PEM_ARRAY[] = { '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', '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', '+', '/' };


}



package com.rd.p2p.common.util.code;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;


/**
 * <p>
 * Description:base64解码类
 * </p>
 */
public abstract class CharacterEncoder {


public CharacterEncoder() {
}


protected abstract int bytesPerAtom();


protected abstract int bytesPerLine();


protected void encodeBufferPrefix(OutputStream outputstream) throws IOException {
pStream = new PrintStream(outputstream);
}


protected void encodeBufferSuffix(OutputStream outputstream) throws IOException {
}


protected void encodeLinePrefix(OutputStream outputstream, int i) throws IOException {
}


protected void encodeLineSuffix(OutputStream outputstream) throws IOException {
// pStream.println();
}


protected abstract void encodeAtom(OutputStream outputstream, byte abyte0[], int i, int j) throws IOException;


protected int readFully(InputStream inputstream, byte abyte0[]) throws IOException {
for (int i = 0; i < abyte0.length; i++) {
int j = inputstream.read();
if (j == -1)
return i;
abyte0[i] = (byte) j;
}


return abyte0.length;
}


public void encode(InputStream inputstream, OutputStream outputstream) throws IOException {
byte abyte0[] = new byte[bytesPerLine()];
encodeBufferPrefix(outputstream);
do {
int j = readFully(inputstream, abyte0);
if (j == 0)
break;
encodeLinePrefix(outputstream, j);
for (int i = 0; i < j; i += bytesPerAtom())
if (i + bytesPerAtom() <= j)
encodeAtom(outputstream, abyte0, i, bytesPerAtom());
else
encodeAtom(outputstream, abyte0, i, j - i);


if (j < bytesPerLine())
break;
encodeLineSuffix(outputstream);
} while (true);
encodeBufferSuffix(outputstream);
}


public void encode(byte abyte0[], OutputStream outputstream) throws IOException {
ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(abyte0);
encode(((InputStream) (bytearrayinputstream)), outputstream);
}


public String encode(byte abyte0[]) {
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(abyte0);
String s = null;
try {
encode(((InputStream) (bytearrayinputstream)), ((OutputStream) (bytearrayoutputstream)));
s = bytearrayoutputstream.toString("8859_1");
} catch (Exception exception) {
throw new Error("ChracterEncoder::encodeBuffer internal error");
}
return s;
}


public void encodeBuffer(InputStream inputstream, OutputStream outputstream) throws IOException {
byte abyte0[] = new byte[bytesPerLine()];
encodeBufferPrefix(outputstream);
int j;
do {
j = readFully(inputstream, abyte0);
if (j == 0)
break;
encodeLinePrefix(outputstream, j);
for (int i = 0; i < j; i += bytesPerAtom())
if (i + bytesPerAtom() <= j)
encodeAtom(outputstream, abyte0, i, bytesPerAtom());
else
encodeAtom(outputstream, abyte0, i, j - i);


encodeLineSuffix(outputstream);
} while (j >= bytesPerLine());
encodeBufferSuffix(outputstream);
}


public void encodeBuffer(byte abyte0[], OutputStream outputstream) throws IOException {
ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(abyte0);
encodeBuffer(((InputStream) (bytearrayinputstream)), outputstream);
}


public String encodeBuffer(byte abyte0[]) {
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(abyte0);
try {
encodeBuffer(((InputStream) (bytearrayinputstream)), ((OutputStream) (bytearrayoutputstream)));
} catch (Exception exception) {
throw new Error("ChracterEncoder::encodeBuffer internal error");
}
return bytearrayoutputstream.toString();
}


protected PrintStream pStream;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值