黑马程序员技术交流社区
标题:
MyBufferedInputSteam中老毕写的从缓冲区读取字符
[打印本页]
作者:
突然世界晴
时间:
2015-3-14 16:57
标题:
MyBufferedInputSteam中老毕写的从缓冲区读取字符
import java.io.*;
class MyBufferedInputStream
{
private InputStream in;
private byte[] buf = new byte[1024*4];
private int pos = 0,count = 0;
MyBufferedInputStream(InputStream in)
{
this.in = in;
}
//一次读一个字节,从缓冲区(字节数组)获取。
public int myRead()throws IOException
{
//通过in对象读取硬盘上数据,并存储buf中。
if(count==0) //if里面是只执行一次把??????????????
{
count = in.read(buf); //比如count=1024每次获取的值不都是一样么????等else if执行完,重新执行不又是1024吗?
if(count<0)
return -1;
pos = 0; // pos指针从0开始取
byte b = buf[pos]; //b里获取一个个 b=a
count--; //1024变成1023
pos++; //pos=1
return b&255; //返回,
}
else if(count>0)
{
byte b = buf[pos]; //b里面获取第二个 count=1023,pos=1 b=ab?
count--; //count=1022 ,当count=0时, 在执行if
pos++; //pos=2
return b&0xff; //b=ab ??
}
return -1;
}
public void myClose()throws IOException
{
in.close();
}
}
复制代码
作者:
谢鸿
时间:
2015-3-14 18:42
支持一下!!!!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2