Base64解码类

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


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


/**
 * <p>
 * Description:base64解码类
 * </p>
 */
public class BASE64Decoder extends CharacterDecoder {


public BASE64Decoder() {
decode_buffer = new byte[4];
}


protected int bytesPerAtom() {
return 4;
}


protected int bytesPerLine() {
return 72;
}


protected void decodeAtom(InputStream inputstream, OutputStream outputstream, int i) throws IOException {
byte byte0 = -1;
byte byte1 = -1;
byte byte2 = -1;
byte byte3 = -1;
if (i < 2)
throw new IOException("BASE64Decoder: Not enough bytes for an atom.");
int j;
do {
j = inputstream.read();
if (j == -1)
throw new IOException("StreamExhausted");
} while (j == 10 || j == 13);
decode_buffer[0] = (byte) j;
j = readFully(inputstream, decode_buffer, 1, i - 1);
if (j == -1)
throw new IOException("StreamExhausted");
if (i > 3 && decode_buffer[3] == 61)
i = 3;
if (i > 2 && decode_buffer[2] == 61)
i = 2;
switch (i) {
case 4: // '\004'
byte3 = PEM_CONVERT_ARRAY[decode_buffer[3] & 0xff];
// fall through


case 3: // '\003'
byte2 = PEM_CONVERT_ARRAY[decode_buffer[2] & 0xff];
// fall through


case 2: // '\002'
byte1 = PEM_CONVERT_ARRAY[decode_buffer[1] & 0xff];
byte0 = PEM_CONVERT_ARRAY[decode_buffer[0] & 0xff];
// fall through


default:
switch (i) {
case 2: // '\002'
outputstream.write((byte) (byte0 << 2 & 0xfc | byte1 >>> 4 & 3));
break;


case 3: // '\003'
outputstream.write((byte) (byte0 << 2 & 0xfc | byte1 >>> 4 & 3));
outputstream.write((byte) (byte1 << 4 & 0xf0 | byte2 >>> 2 & 0xf));
break;


case 4: // '\004'
outputstream.write((byte) (byte0 << 2 & 0xfc | byte1 >>> 4 & 3));
outputstream.write((byte) (byte1 << 4 & 0xf0 | byte2 >>> 2 & 0xf));
outputstream.write((byte) (byte2 << 6 & 0xc0 | byte3 & 0x3f));
break;
}
break;
}
}


private static final 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', '+', '/' };
private static final byte PEM_CONVERT_ARRAY[];

byte decode_buffer[];


static {
PEM_CONVERT_ARRAY = new byte[256];
for (int i = 0; i < 255; i++)
PEM_CONVERT_ARRAY[i] = -1;


for (int j = 0; j < PEM_ARRAY.length; j++)
PEM_CONVERT_ARRAY[PEM_ARRAY[j]] = (byte) j;


}

}



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;


/**
 * <p>
 * Description:备注
 * </p>
 */


public abstract class CharacterDecoder {


public CharacterDecoder() {
}


protected abstract int bytesPerAtom();


protected abstract int bytesPerLine();


protected void decodeBufferPrefix(InputStream inputstream, OutputStream outputstream) throws IOException {
}


protected void decodeBufferSuffix(InputStream inputstream, OutputStream outputstream) throws IOException {
}


protected int decodeLinePrefix(InputStream inputstream, OutputStream outputstream) throws IOException {
return bytesPerLine();
}


protected void decodeLineSuffix(InputStream inputstream, OutputStream outputstream) throws IOException {
}


protected void decodeAtom(InputStream inputstream, OutputStream outputstream, int i) throws IOException {
throw new IOException("StreamExhausted");
}


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


return j;
}


@SuppressWarnings("unused")
public void decodeBuffer(InputStream inputstream, OutputStream outputstream) throws IOException {
int j = 0;
decodeBufferPrefix(inputstream, outputstream);
try {
do {
int k = decodeLinePrefix(inputstream, outputstream);
int i;
for (i = 0; i + bytesPerAtom() < k; i += bytesPerAtom()) {
decodeAtom(inputstream, outputstream, bytesPerAtom());
j += bytesPerAtom();
}


if (i + bytesPerAtom() == k) {
decodeAtom(inputstream, outputstream, bytesPerAtom());
j += bytesPerAtom();
} else {
decodeAtom(inputstream, outputstream, k - i);
j += k - i;
}
decodeLineSuffix(inputstream, outputstream);
} while (true);
} catch (IOException e) {
if (e.getMessage().equals("StreamExhausted"))
decodeBufferSuffix(inputstream, outputstream);
else
throw e;
}
}


public byte[] decodeBuffer(String s) throws IOException {
byte abyte0[] = s.getBytes();
ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(abyte0);
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
decodeBuffer(((InputStream) (bytearrayinputstream)), ((OutputStream) (bytearrayoutputstream)));
return bytearrayoutputstream.toByteArray();
}


public String decodeStr(String s) throws IOException {
return new String(decodeBuffer(s));
}


public byte[] decodeBuffer(InputStream inputstream) throws IOException {
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
decodeBuffer(inputstream, ((OutputStream) (bytearrayoutputstream)));
return bytearrayoutputstream.toByteArray();
}
}

猜你喜欢

转载自blog.csdn.net/A___B___C/article/details/80221693