Below is the code that is written using byte stream non-buffer class FileInputStream
and FileOutputStream
with the usage of explicit user buffers.
public class FileCopyUserBuffer{
public static void main(String[] args){
String inFileStr = "C:\practice_in.jpg";
String outFileStr = "C:\practice_out.jpg";
FileInputStream in = null;
FileOutputStream out = null;
try{
in = new FileInputStream(inFileStr);
out = new FileOutputStream(outFileStr);
startTime = System.nanoTime();
byte[] byteBuf = new byte[4096];
int numBytesRead;
while((numBytesRead = in.read(byteBuf)) != -1){
out.write(byteBuf, 0, numBytesRead);
}
}catch(IOException ex){
ex.printStackTrace();
}
}
}
Below is the program written using byte based buffer I/O stream class BufferedOutputStream
& BufferedInputStream
.
public class FileCopyBufferedStream{
public void main(String[] args){
String inFileStr = "C:\practice_in.jpg";
String outFileStr = "C:\practice_out.jpg";
BufferedInputStream in = null;
BufferedOutputStream out = null;
File fileIn = new File(inFileStr);
System.out.println("File size is: " + fileIn.length() + " bytes");
try{
in = new BufferedInputStream(new FileInputStream(inFileStr));
out = new BufferedOutputStream(new FileOutputStream(outFileStr));
startTime = System.nanoTime();
int byteRead;
while((byteRead = in.read()) != -1){
out.write(byteRead);
}
}catch(IOException ex){
ex.printStackTrace();
}
}
}
Above two programs perform the same functionality but am not clear about the functionality of write(#bytes)
method of class BufferedOutputStream
.
Based on the observation, below two classes have their own buffers.
public class BufferedInputStream extends FilterInputStream {
.........
protected volatile byte buf[];
..........
}
public class BufferedOutputStream extends FilterOutputStream {
........
protected byte buf[];
.......
}
In second program, How does out
object of class BufferedOutputStream
is able to write the buffer that is part of class BufferedInputStream
? Because we are not passing buffer as argument of out.write(#bytes)
method[which looks non-intuitive] unlike the first program which passes user buffer out.write(byteBuf,,)
? In second program, out
object should at-least have access to object reference in
to access the buffer populated using in.read()
.
1
Because we are not passing buffer as argument of out.write(#bytes) method[which looks non-intuitive] unlike the first program which passes user buffer out.write(byteBuf,,)?
No, you are not passing buffer as argument,
you are passing the single byte that you have read.
You have that single byte in the byteRead
variable,
returned by BufferedInputStream
,
and you are passing that single byte into BufferedOutputStream
.
In second program, out object should at-least have access to object reference in to access the buffer populated using in.read().
No, BufferedOutputStream
doesn’t have access to BufferedInputStream
‘s buffer.
It seems you simply misunderstood what .read()
without arguments really does.
There’s nothing magical going on here.
3