1 import java.io.*; 2 3 class MyBufferedInputStream 4 { 5 private InputStream in; 6 7 private byte[] buf = new byte[1024*4]; 8 9 private int pos = 0,count = 0; 10 11 MyBufferedInputStream(InputStream in) 12 { 13 this.in= in; 14 } 15 16 //一次读一个字节,从缓冲区(字节数组)获取。 17 public int myRead()throws IOException 18 { 19 //通过in对象读取硬盘上数据,并存储buf中。 20 if(count==0) 21 { 22 count= in.read(buf); 23 if(count<0) 24 return-1; 25 pos= 0; 26 byte b = buf[pos]; 27 28 count--; 29 pos++; 30 return b&255;//取最低八位 31 } 32 else if(count>0) 33 { 34 byte b = buf[pos]; 35 count--; 36 pos++; 37 return b&0xff; //十六进制的255 38 } 39 return -1; 40 41 } 42 public void myClose()throws IOException 43 { 44 in.close(); 45 } 46 }