您的位置:首页 > 其它

字节流缓存

2016-01-06 16:49 260 查看
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();
}
}


利用数组,模拟字符流缓冲区
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: