Title: Buffering Classes
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Buffering Classes

If you look up InputStream, and OutputStream in the Java documentation, you will see that these classes contain the fundamental things that we need to read and write data. I will create a simple copy method to demonstrate this below.

We almost never do I/O directly with InputStream and OutputStream. We almost always provide some other "Buffering" class to accomplish one of the 2 things:

- Provide a more convenient way to read and write data
- Provide for more efficiency

In our previous examples, we used the Scanner class as a "buffering" class for reading an InputStream, and we provided PrintStream as the "buffering" class for OutputStream.

I'm hoping you can see that the Scanner and PrintStream are more convenient than working with raw bytes.

In the next example, I am going to use 2 more Buffering classes which will add no real convenience, but dramatically improve our performance:

- BufferedInputStream
- BufferedOutputStream

If you look up them up in the Java documentation, you will see that the calling sequence is virtually the same as InputStream and OutputStream. In this case the main thing we will gain is peformance.

To do some timing, I am providing a Timer class that I will be using in other sections:

Timer.java

ByteCopyFileExamples.java


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class ByteCopyFileExamples {

    static void byteCopy(String from, String to) throws IOException
    {
        // Using Non-buffered Low Level I/O byte stream operations

        int b;
        // Using byte stream  (8 bit)
        FileInputStream in = new FileInputStream(from);
        FileOutputStream out = new FileOutputStream(to);

        while ((b = in.read()) != -1)
            out.write(b);

        in.close();
        out.close();
    }

    static void bufferedByteCopy(String from, String to) throws IOException
    {
        int b;
        // Using Buffered I/O byte(8 bit) stream operations

        FileInputStream insrc = new FileInputStream(from);
        FileOutputStream outsrc = new FileOutputStream(to);
        BufferedInputStream in = new BufferedInputStream(insrc, 128000);
        BufferedOutputStream out = new BufferedOutputStream(outsrc, 128000);


        while ((b = in.read()) != -1)
            out.write(b);

        in.close();
        out.close();
    }

    public static void main(String[] args) throws IOException{

        String bigFile = "bigFile.zip";
        Timer timer = new Timer();

        timer.start();
        bufferedByteCopy(bigFile, "junk.zip");
        timer.stop();
        System.out.println("Buffered time="+ timer.time());

        timer.start();
        byteCopy(bigFile, "junk.zip");
        timer.stop();
        System.out.println("non-Buffered time="+ timer.time());

    }

}
// For a bigFile.zip that was around 115 Mega-bytes I got the following output:

// Buffered time=5.18
// non-Buffered time = 1048.408



Why such a big diffrence?

Last Updated: July 28, 2014 10:32 AM