Sitemap

Thursday, June 25, 2015

Java: Input and Output streams

java.io package provides I/O classes to manipulate streams. This package supports two types of streams:
1. binary streams which handle binary data. InputStream and OutputStream are high level interfaces for manipulating binary streams.
2. character streams which handle character data. Reader and Writer are high level interfaces for manipulating character streams. In this section, the main focus is on binary streams.

By default, most of the streams read or write one byte at a time. This causes poor I/O performance because it takes lot of time to read/write byte by byte when dealing with large amounts of data. I/O provides Buffered streams to override this byte by byte default behaviors.



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

public class Main {

    private static final String SOURCE_FILE = "D:\\test.jar";

    public static void main(String[] args) {
        Main io = new Main();
        try {
            long startTime = System.currentTimeMillis();
            io.readWrite(SOURCE_FILE, "D:\\test1.jar");
            long endTime = System.currentTimeMillis();
            System.out.println("Time taken for reading and writing using default behaviour : " + (endTime - startTime) +
                               " milli seconds");

            long startTime1 = System.currentTimeMillis();
            io.readWriteBuffer(SOURCE_FILE, "D:\\test2.jar");
            long endTime1 = System.currentTimeMillis();
            System.out.println("Time taken for reading and writing using buffered streams : " +
                               (endTime1 - startTime1) + " milli seconds");

            long startTime2 = System.currentTimeMillis();
            io.readWriteArray(SOURCE_FILE, "D:\\test3.jar");
            long endTime2 = System.currentTimeMillis();
            System.out.println("Time taken for reading and writing using custom buffering : " +
                               (endTime2 - startTime2) + " milli seconds");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void readWrite(String fileFrom, String fileTo) throws IOException {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream(fileFrom);
            out = new FileOutputStream(fileTo);
            while (true) {
                int bytedata = in.read();
                if (bytedata == -1)
                    break;
                out.write(bytedata);
            }
        } finally {
            if (in != null)
                in.close();
            if (out != null)
                out.close();
        }
    }

    public static void readWriteBuffer(String fileFrom, String fileTo) throws IOException {
        InputStream inBuffer = null;
        OutputStream outBuffer = null;
        try {
            InputStream in = new FileInputStream(fileFrom);
            inBuffer = new BufferedInputStream(in);
            OutputStream out = new FileOutputStream(fileTo);
            outBuffer = new BufferedOutputStream(out);
            while (true) {
                int bytedata = inBuffer.read();
                if (bytedata == -1)
                    break;
                out.write(bytedata);
            }
        } finally {
            if (inBuffer != null)
                inBuffer.close();
            if (outBuffer != null)
                outBuffer.close();
        }
    }

    public static void readWriteArray(String fileFrom, String fileTo) throws IOException {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream(fileFrom);
            out = new FileOutputStream(fileTo);
            int availableLength = in.available();
            byte[] totalBytes = new byte[availableLength];
            int bytedata = in.read(totalBytes);
            out.write(totalBytes);

        } finally {
            if (in != null)
                in.close();
            if (out != null)
                out.close();
        }
    }
}


OUTPUT
Time taken for reading and writing using default behaviour : 5188 milli seconds
Time taken for reading and writing using buffered streams : 3105 milli seconds
Time taken for reading and writing using custom buffering : 7 milli seconds

1 comment:

  1. private static int IO_BUFFER_SIZE = 8196;

    public static int transferContent(InputStream inStream, OutputStream outStream) throws IOException {
    if ((inStream == null) || (outStream == null)) {
    throw new IOException("Input or output stream is null. So cannot transfer content between those.");
    }
    byte[] ioBuffer = new byte[IO_BUFFER_SIZE];

    int totalLen = 0;
    int len;
    while ((len = inStream.read(ioBuffer)) >= 0) {
    outStream.write(ioBuffer, 0, len);
    totalLen += len;
    }
    return totalLen;
    }

    ReplyDelete