问题描述
CoreJava第二卷第八版似乎有个错误:3.5建立URL连接的示例代码URLConnectionTest.java:方法Base64OutputStream.flush():publicvoidflush()throwsIOException{if(i==1){super.write(toBase64[(inbuf[0]&0xFC)>>2]);super.write(toBase64[(inbuf[0]&0x03)<<4]);super.write('=');super.write('=');}elseif(i==2){super.write(toBase64[(inbuf[0]&0xFC)>>2]);super.write(toBase64[((inbuf[0]&0x03)<<4)|((inbuf[1]&0xF0)>>4)]);super.write(toBase64[(inbuf[1]&0x0F)<<2]);super.write('=');}if(col>0){super.write('n');col=0;}}
应该为:publicvoidflush()throwsIOException{if(i==1){super.write(toBase64[(inbuf[0]&0xFC)>>2]);super.write(toBase64[(inbuf[0]&0x03)<<4]);super.write('=');super.write('=');}elseif(i==2){super.write(toBase64[(inbuf[0]&0xFC)>>2]);super.write(toBase64[((inbuf[0]&0x03)<<4)|((inbuf[1]&0xF0)>>4)]);super.write(toBase64[(inbuf[1]&0x0F)<<2]);super.write('=');}i=0;//if(col>0)//{//super.write('n');//col=0;//}}
解决方案
解决方案二:
查了CayHorstmann的bug列表,没看到这个bug,我那里弄错了吗?有谁看过这个示例代码吗?base64编码为什么非要在最后加个回车?不是过了76个字节才加吗?