import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class MyBufferedInputStreamDemo {
public static void main(String[] args) {
MyBufferedInputStream mbis = null;
BufferedOutputStream bos = null;
try {
mbis = new MyBufferedInputStream(new FileInputStream("G:\\1.mp3"));
bos = new BufferedOutputStream(new FileOutputStream("G:\\2.mp3"));
long start = System.currentTimeMillis();
for(int ch;(ch=mbis.read())!=-1;) {
bos.write(ch);
}
long end = System.currentTimeMillis();
System.out.println((end-start) + "毫秒");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class MyBufferedInputStream extends InputStream {
private InputStream in;
private byte[] buf = new byte[1024];
private int pos = 0, count = 0;
public MyBufferedInputStream(InputStream in) {
super();
this.in = in;
}
@Override
public int read() throws IOException {
return in.read();
}
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;
} else if (count > 0) {
byte b = buf[pos];
count--;
pos++;
return b;
}
return -1;
}
@Override
public void close() throws IOException {
in.close();
}
}
java 自定义字节缓冲流_好慢啊
最新推荐文章于 2022-03-25 01:58:55 发布