import java.io.*;
class MyBufferedStreamDemo
{
public static void main(String[] args)throws IOException
{
long start =System.currentTimeMillis();
MyBufferedInputStream my =new MyBufferedInputStream(new FileInputStream("1.wma"));
BufferedOutputStream bufo =new BufferedOutputStream(new FileOutputStream("2.wma"));
int leng =0;
System.out.println(my.myRead());
while ((leng=my.myRead() )!=-1)
{
bufo.write(leng);
}
bufo.close();
my.myclose();
long end =System.currentTimeMillis();
System.out.println((end-start)+"毫秒");
}
}
class MyBufferedInputStream
{
private InputStream input;
public byte[] buf =new byte[1024];
private int pos =0,count =0;
MyBufferedInputStream(InputStream in)
{
input =in;
}
public int myRead()throws IOException
{
if (count ==0)
{
count =input.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
{
input.close();
}
}
上面的代码时根据字节缓冲区的原理,编写的自定义字节缓冲区,在写myRead()方法时,用int类型提升byte类型,并在返回的字节&255,来避免为-1情况出现,然后在用write()方法,取最低8位字节保证数据的原样性,但加上这句System.out.println(my.myRead());为什么复制会少一些,有些不明白,请大家说一下,谢谢
|