代码是建立了我的缓冲区输入流,基本上是复写毕老师的代码,对于myBufferedInputStream类中的copy()方法过程不是太了解,到底BufferedInputStream在内存中是怎么样运行的呢?数组中的数据不就是在缓冲区么?为什么还要取出来放在缓冲区?感觉这样不是绕远路了?具体过程哪位能不能给我解释下,最好能画个图,毕老师说的不太懂啊
import java.io.*;
import java.util.*;
class myBufferedInput
{
public static void main(String[] args) throws IOException
{
copy();
}
public static void copy()throws IOException
{ myBufferedInputStream bfi=new myBufferedInputStream(new FileInputStream("c:\\1.mp3"));
BufferedOutputStream bfo=new BufferedOutputStream(new FileOutputStream("c:\\王利强.mp3"));
int by=0;
while((by=bfi.myRead())!=-1)
{
bfo.write(by);
}
bfi.myClose();
bfo.close();
}
}
class myBufferedInputStream
{ private InputStream In;
byte[] buf=new byte[1024];
int pos,count;
myBufferedInputStream(InputStream In)
{
this.In=In;
}
public int myRead()throws IOException
{
if(count==0)
{
count=In.read(buf);
if (count<0)
return -1;
pos=0;
byte b=buf[pos];
count--;
pos++;
return b&255;
}
else if(count>0)
{
byte b=buf[pos];
count--;
pos++;
return b&255;
}
return -1;
}
public void myClose()throws IOException
{
In.close();
}
}
|