import java.io.*;
class MyBufferedInputStream
{
private InputStream is;
private int count=0;
private int n=0;
private byte[] date = new byte[2];
MyBufferedInputStream(InputStream is)
{
this.is=is;
}
public int myRead() throws IOException
{
if(count==0) //如果数据被取空了就重新取数据
{
n = 0;//n需要重置为0,如果不重置为0,一直加上去,肯定越界啊
count=is.read(date); //从文件中读出数据并且存入字节数组中
if (count==-1){
return -1;
}
byte b = date[n]; //从数组中取出一个字节,这时候应该又从角标0开始读取数据
count--;
n++;
return b&255; //返回这个字节
}
else if (count>0) //如果有数据,就继续取
{
byte b = date[n]; //从数组中取出一个字节
count--;
n++;
return b&255; //返回这个字节
}
else
return -1;
}
public void myClose() throws IOException
{
is.close();
}
}
public class Test
{
public static void main(String[] args) throws IOException
{
FileInputStream fis = new FileInputStream("F:\\11.jpg");
FileOutputStream fos = new FileOutputStream("E:\\22.jpg");
MyBufferedInputStream mbis = new MyBufferedInputStream(fis);
int c = 0;
while ((c = mbis.myRead())!=-1) //判断流中是否还有数据
{
fos.write(c);
}
fos.close();
mbis.myClose();
}
}
|