字节流缓存

import java.io.*;
class MyBufferedInputStream{
    
    private InputStream in;
    private byte[] buf = byte[1024*4];
    private int pos = 0,count = 0;//指针,计数器
    
    MyBufferedInputStream(InputStream in)
    {
            this.in=in;
    }    
    public int myRead()
    {
        if(count==0){
            count = in.read(buf);//读取字符流,存入数组
            if(count<0)            //结尾
                return -1;
            pos=0;
            byte b= buf[pos];
            count--;
            pos++;
            return b&0xff;
        }else if (count>0){
            byte b= buf[pos];
            count--;
            pos++;
            return b&0xff;//避免文件中连续八个1,返回后为-1
        }
        return -1;
    }
    public void myClose()throws IOException
    {
        in.close();
    }
}

利用数组,模拟字符流缓冲区

posted @ 2016-01-06 16:49  lovedaydream  阅读(200)  评论(0编辑  收藏  举报