2

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
  • 1
    The InputStream.read(...) method is overloaded. Calling read() without any arguments returns "the next byte of data, or -1 if the end of the stream is reached.". See docs.
    – rwong
    Commented Dec 21, 2014 at 15:52

1 Answer 1

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
  • ya, you are right. Instead of reading byte-by-byte, how do i read blocks of data in second program? Commented Dec 21, 2014 at 17:17
  • ya, this is the question, if i pass byte[] user buffer in write() method, what is the advantage of using class BufferedInputStream & class BufferedOutputStream. It would be same as my first program right? i mean, Does the number of I/O's to disk will decrease if i use BufferedInputSream's read(buf[]) instead of FileInputStream's read(buf[]) method? Commented Dec 21, 2014 at 17:39
  • Oh ok, In second program, read() or read(buf[]) get the data from buffer that is already read? If yes, when was the data read and available in stream? Because we did not explicitly read and make available in buffer except reading a single byte read()? Commented Dec 21, 2014 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.