黑马程序员技术交流社区

标题: 关于毕老师视频中自定义字节缓冲区的问题 [打印本页]

作者: 小小迪    时间: 2015-3-29 22:56
标题: 关于毕老师视频中自定义字节缓冲区的问题
import java.io.*;

class MyBufferedInputStream
{
        private InputStream in;
        private int count=0;
        private int pos=0;
        private byte[] by = new byte[1024];
        MyBufferedInputStream(InputStream in)
        {
                this.in=in;
        }
        public int myBufferedRead()throws IOException
        {
               
                if(count==0)
                {
                        count=in.read(by);
                        if(count<0)
                             return -1;
                        pos=0;
                        byte b= by[pos];
                        pos++;
                        count--;
                        return b&255;
                }
                else if(count<0)
                        return -1;
                else if(count>0)
                {
                        byte b= by[pos];
                        pos++;
                        count--;
                        return b&0xff;
                }
                return -1;
        }
        public void myclose()throws IOException
        {
                in.close();
        }
}

public class MyStringBufferDemo
{
        public static void main(String[] args)throws IOException
        {
                MyBufferedInputStream mbs = new MyBufferedInputStream(new FileInputStream("xuexi.jpg"));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("xuexi2.jpg"));
               
                int num =0;
                while((num=mbs.myBufferedRead())!=-1)
                {
                        bos.write(num);
                }
                mbs.myclose();
                bos.close();
        }
}

毕老师的视频中是将count<0的情况放到了if的里面,用这个方法在我的电脑上虽然复制完数据没有丢失,但是不能正常播放,当把代码改到红色区域,在运行,就可以用了,求解释?




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