图片转为String传个给服务端(二)

2014-11-24 09:01:45 · 作者: · 浏览: 1
et) {
byte a = 0, b = 0, c = 0;
a = in[inOffset];
out[outOffset] = ALPHABET[(a >>> 2) & 0x3F];
if (len > 2) {
b = in[inOffset + 1];
c = in[inOffset + 2];
out[outOffset + 1] = ALPHABET[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];
out[outOffset + 2] = ALPHABET[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)];
out[outOffset + 3] = ALPHABET[c & 0x3F];
} else if (len > 1) {
b = in[inOffset + 1];
out[outOffset + 1] = ALPHABET[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];
out[outOffset + 2] = ALPHABET[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)];
out[outOffset + 3] = '=';
} else {
out[outOffset + 1] = ALPHABET[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];
out[outOffset + 2] = '=';
out[outOffset + 3] = '=';
}
}
/**
* Converts a Base64 encoded string to a byte array.
*
* @param encoded
* Base64 encoded data
* @return decode binary data; 3 bytes for every 4 chars - minus padding
* @exception IOException
* is thrown, if an I/O error occurs reading the data
*/
public static byte[] decode(String encoded) throws IOException {
return decode(encoded, 0, encoded.length());
}
/**
* Converts an embedded Base64 encoded string to a byte array.
*
* @param encoded
* a String with Base64 data embedded in it
* @param offset
* which char of the String to start at
* @param length
* how many chars to decode; must be a multiple of 4
* @return decode binary data; 3 bytes for every 4 chars - minus padding
* @exception IOException
* is thrown, if an I/O error occurs reading the data
*/
public static byte[] decode(String encoded, int offset, int length)
throws IOException {
int i;
int decodedLen;
byte[] decoded;
// the input must be a multiple of 4
if (length % 4 != 0) {
throw new IOException("Base64 string length is not multiple of 4");
}
// 4 chars for 3 bytes, but there may have been pad bytes
decodedLen = length / 4 * 3;
if (encoded.charAt(offset + length - 1) == '=') {
decodedLen--;
if (encoded.charAt(offset + length - 2) == '=') {
decodedLen--;
}
}
decoded = new byte[decodedLen];
for (i = 0, decodedLen = 0; i < length; i += 4, decodedLen += 3) {
decodeQuantum(encoded.charAt(offset + i),
encoded.charAt(offset + i + 1),
encoded.charAt(offset + i + 2),
encoded.charAt(offset + i + 3), decoded, decodedLen);
}
return decoded;
}
/**
* Decode 4 Base64 chars as 1, 2, or 3 bytes of data.
*
* @param in1
* first char of quantum to decode
* @param in2
* second char of quantum to decode
* @param in3
* third char of quantum to decode
* @param in4
* forth char of quantum to decode
* @param out
* buffer to put the output in
* @param outOffset
* where in the output buffer to put the bytes
*/
private static void decodeQuantum(char in1, char in2, char in3, char in4,
byte[] out, int outOffset) throws IOException {
int a = 0, b = 0, c = 0, d = 0;
int pad = 0;
a = valueDecoding[in1 & 127];
b = valueDecodin