自定义一个字节缓冲区的简单示例

来源:互联网 发布:搞笑照片贴图软件 编辑:程序博客网 时间:2024/05/31 18:51
package day06;
import java.io.*;
class MyBufferedInputStream {
private InputStream in;
private byte[] buff=new byte[1024];
private int pos=0,count=0;
MyBufferedInputStream(InputStream in){
this.in=in;
}
//一次读一个字节,从缓冲区(字节数组)获取
public int myRead() throws IOException{
//通过in对象读取硬盘上的数据,并存储到buff中
if(count==0){
count=in.read(buff);
if(count<0){
return -1;
}
pos=0;
byte by=buff[pos];
count--;

pos++;

//由于会出现所读取文件的数据是-1的情况会与返回-1标记相同导致停止,所以将读取的数据与上255转成int类型

return by&255;
}
else if(count>0){
byte by=buff[pos];
count--;
pos++;
return by&255;
}
else{
return -1;
}
}
public void myClose() throws IOException{
in.close();
}
}
0 0
原创粉丝点击