BASE64Encoder竟然编出回车换行了?怎么回事?

Ah 2003-09-05 10:35:34
我使用 sun.misc.BASE64Encoder 将一个 byte[] buf BASE64编码,结果如下所示,竟然会有一个\r\n,谁知道这是怎么回事?要怎样解决?


rO0ABXVyABNbTGphdmEubGFuZy5TdHJpbmc7rdJW5+kde0cCAAB4cAAAAAN0AAMwMDB0AAMxMTF0

AAMyMjI=
...全文
2967 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangyanqiu 2003-09-11
  • 打赏
  • 举报
回复


学习!。。。
cbhyk 2003-09-11
  • 打赏
  • 举报
回复
送个BASE64实现类给你:
public final class Base64
{
private static final byte[] DECODE_TABLE=new byte[]
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,62, 0, 0, 0,63,
52,53,54,55,56,57,58,59,60,61, 0, 0, 0, 0, 0, 0,
0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
15,16,17,18,19,20,21,22,23,24,25, 0, 0, 0, 0, 0,
0,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
41,42,43,44,45,46,47,48,49,50,51, 0, 0, 0, 0, 0
};

//"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
private static final byte[] ENCODE_TABLE=new byte[]
{ 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
43, 47
};

static
{
//create encode table
//ENCODE_TABLE = new byte[64];
int index = 0;
for(char c='A'; c<='Z'; c++)
ENCODE_TABLE[index++] = (byte) c;
for(char c='a'; c<='z'; c++)
ENCODE_TABLE[index++] = (byte) c;
for(char c='0'; c<='9'; c++)
ENCODE_TABLE[index++] = (byte) c;
ENCODE_TABLE[index++] = (byte) '+';
ENCODE_TABLE[index++] = (byte) '/';

//create decode table
for(int i=0; i<64; i++)
DECODE_TABLE[(int) ENCODE_TABLE[i]] = (byte) i;
}

private Base64(){}

public static byte[] encode(byte[] data)
{
if(data==null)
return null;

int fullGroups = data.length/3;
int resultBytes = fullGroups*4;
if(data.length%3 != 0)
resultBytes += 4;

byte[] result = new byte[resultBytes];
int resultIndex = 0;
int dataIndex=0;
int temp = 0;
for(int i=0; i<fullGroups; i++)
{
temp = (data[dataIndex++]&0xff)<<16
| (data[dataIndex++]&0xff)<<8
| data[dataIndex++]&0xff;

result[resultIndex++] = ENCODE_TABLE[(temp>>18) & 0x3f];
result[resultIndex++] = ENCODE_TABLE[(temp>>12) & 0x3f];
result[resultIndex++] = ENCODE_TABLE[(temp>>6) & 0x3f];
result[resultIndex++] = ENCODE_TABLE[temp&0x3f];
}
temp=0;
while(dataIndex<data.length)
{
temp<<=8;
temp|=data[dataIndex++]&0xff;
}
switch(data.length%3)
{
case 1:
temp <<= 8;
temp <<= 8;
result[resultIndex++] = ENCODE_TABLE[(temp>>18) & 0x3f];
result[resultIndex++] = ENCODE_TABLE[(temp>>12) & 0x3f];
result[resultIndex++] = 0x3D;
result[resultIndex++] = 0x3D;
break;
case 2:
temp <<= 8;
result[resultIndex++] = ENCODE_TABLE[(temp>>18) & 0x3f];
result[resultIndex++] = ENCODE_TABLE[(temp>>12) & 0x3f];
result[resultIndex++] = ENCODE_TABLE[(temp>>6) & 0x3f];
result[resultIndex++] = 0x3D;
break;
default:
break;
}

return result;
}

public static byte[] decode(byte[] base64Data)
{
if(base64Data==null)
return null;
if(base64Data.length==0)
return new byte[0];
if(base64Data.length%4 !=0)
throw new IllegalArgumentException("数据不完整,长度为:"+base64Data.length);

byte[] result=null;
int groupCount = base64Data.length/4;

int lastData = base64Data.length;
while(base64Data[lastData-1] == 0x3D)
{
if(--lastData == 0)
return new byte[0];
}
result = new byte[lastData-groupCount];

int temp = 0;
int resultIndex = 0;
int dataIndex = 0;
for(; dataIndex+4 < base64Data.length;)
{
temp = DECODE_TABLE[base64Data[dataIndex++]];
temp = (temp<<6) + DECODE_TABLE[base64Data[dataIndex++]];
temp = (temp<<6) + DECODE_TABLE[base64Data[dataIndex++]];
temp = (temp<<6) + DECODE_TABLE[base64Data[dataIndex++]];

result[resultIndex++] = (byte)((temp>>16) & 0xff);
result[resultIndex++] = (byte)((temp>>8) & 0xff);
result[resultIndex++] = (byte)(temp & 0xff);
}

temp = 0;
int j = 0;
for(; dataIndex<base64Data.length; dataIndex++,j++)
temp = (temp<<6) + DECODE_TABLE[base64Data[dataIndex]];
for(; j<4; j++)
temp <<= 6;

result[resultIndex++] = (byte)((temp>>16) & 0xff);
if(base64Data[dataIndex-2] != '=')
result[resultIndex++] = (byte)((temp>>8) & 0xff);
if(base64Data[dataIndex-1] != '=')
result[resultIndex++] = (byte)(temp & 0xff);

return result;
}
}

需要注意的是:如果数据量大,比较耗内存
Ah 2003-09-11
  • 打赏
  • 举报
回复
up
Ah 2003-09-06
  • 打赏
  • 举报
回复
要怎样解决这个问题?我不想在编码中出现换行符
yangjuanli 2003-09-05
  • 打赏
  • 举报
回复
是的,一行不能多于76个字符的。
cbhyk 2003-09-05
  • 打赏
  • 举报
回复
RFC2045中规定的:
The encoded output stream must be represented in lines of no more than 76 characters each

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧