|
BufferTest.java
We can ignore buffering, we can write our own buffering,
or we can simply wrap our streams in BufferedInputStream and BufferedOutputStream:
BufferedInputStream
|
+ BufferedInputStream( InputStream )
+ BufferedInputStream( InputStream, bufSize )
|
BufferedOutputStream
|
+ BufferedOutputStream( OutputStream )
+ BufferedOutputStream( OutputStream, bufSize )
|
Copying a massive file -- 2,957,286 bytes.
Here's the results with a 512-byte buffer:
ignoreBuff: 39156 milliseconds
bufferWrap: 969 milliseconds
rollYourOwn: 453 milliseconds
No buffer takes FORTY seconds.
Unacceptable!
The computer has LOCKED UP!
Hit the RESET button!
|
private static void ignoreBuff() throws IOException {
InputStream in = new FileInputStream(INFILE);
OutputStream out = new FileOutputStream(OUTFILE);
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
out.close();
}
private static void bufferWrap() throws IOException {
InputStream in = new BufferedInputStream(
new FileInputStream(INFILE));
OutputStream out = new BufferedOutputStream(
new FileOutputStream(OUTFILE));
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
out.close();
}
private static void rollYourOwn() throws IOException {
InputStream in = new FileInputStream(INFILE);
OutputStream out = new FileOutputStream(OUTFILE);
byte[] buf = new byte[512];
int count;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
in.close();
out.close();
}
|
|