本帖最后由 王陶成 于 2012-9-11 17:39 编辑
看我毕老师的自定义字节缓冲流视频,自己模仿着写了一个,可是复制mp3的时候报出了错误,不知道这个是什么错误,
麻烦给看一下
import java.io.IOException;
import java.io.InputStream;
class MyBufferedInputStream {
private InputStream in;
private byte[] buf = new byte[1024];
//指针,计数器
private int pos = 0, count = 0;
public MyBufferedInputStream(InputStream in) {
this.in = in;
}
public int myRead() throws IOException {
if(count == 0) {
count = in.read(buf);
if(count<0){
return -1;
}
byte b = buf[pos];
count --;
pos ++;
return b&255;
}
else if(count > 0) {
byte b = buf[pos];
count --;
pos ++;
return b&0xff;
}
return -1;
}
public void myClose() throws IOException {
in.close();
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyMp3 {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
copy();
long end = System.currentTimeMillis();
long time = end - start;
//获取拷贝时间
System.out.println(time);
}
public static void copy() throws IOException {
MyBufferedInputStream bis = new MyBufferedInputStream(new FileInputStream("demo.mp3"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("demo_copy.mp3"));
int by = 0;
while((by=bis.myRead()) != -1) {
bos.write(by);
}
bis.myClose();
bos.close();
}
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4096
at day19.MyBufferedInputStream.myRead(MyBufferedInputStream.java:36)
at day19.CopyMp3.copy_2(CopyMp3.java:42)
at day19.CopyMp3.main(CopyMp3.java:14)
|