黑马程序员技术交流社区

标题: 关于自定义字节缓冲区问题 [打印本页]

作者: LeaonButcher    时间: 2014-7-5 22:25
标题: 关于自定义字节缓冲区问题
拷贝的图片少了100多个字节,代码如下,请大虾看看哪边有问题

  1. import java.io.*;

  2. class MyBufferedInputStreamDemo
  3. {
  4.         public static void main(String[] args) throws IOException
  5.         {
  6.                 MyBufferedInputStream bis = new MyBufferedInputStream(new FileInputStream("touxiang.jpg"));
  7.                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy2.jpg"));
  8.                 int b = 0;
  9.                 while((b=bis.read())!=-1){
  10.                         bos.write(b);
  11.                 }
  12.                 bis.close();
  13.                 bos.close();
  14.         }
  15. }

  16. class MyBufferedInputStream
  17. {
  18.         private InputStream is;
  19.         private byte[] buf = new byte[1024];
  20.         private int pos=0,count=0;

  21.         public MyBufferedInputStream(InputStream is){
  22.                 this.is = is;
  23.         }

  24.         public int read()throws IOException{
  25.                 if(count ==0){
  26.                         count = is.read(buf);
  27.                         if(count==-1)
  28.                                 return -1;
  29.                         pos = 0;
  30.                 }
  31.                 count--;
  32.                 return buf[pos++]&255;
  33.         }

  34.         public void close()throws IOException{
  35.                 is.close();
  36.         }

  37. }
复制代码


作者: xp8812    时间: 2014-7-6 00:40
兄弟,我测试了没有少字节啊
作者: LeaonButcher    时间: 2014-7-6 08:22
xp8812 发表于 2014-7-6 00:40
兄弟,我测试了没有少字节啊

我试了好几遍都会少字节,不知道怎么回事。
作者: xp8812    时间: 2014-7-6 13:13
LeaonButcher 发表于 2014-7-6 08:22
我试了好几遍都会少字节,不知道怎么回事。

wo图片的大小是67kb,运行完每次都能完整打开copy2图片,你的外部环境呢
作者: ╰_〃无法释怀的    时间: 2014-7-6 16:36
没有少字节




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2