MrCoder
7/5/2013 - 12:36 PM

http://stackoverflow.com/questions/3122422/usage-of-bufferedinputstream The following code support the selected answer on stackoverflow.

http://stackoverflow.com/questions/3122422/usage-of-bufferedinputstream The following code support the selected answer on stackoverflow.

    String sourceFileName = "source.ged";
    String userDir = System.getProperty("user.dir");
    File sourceFile = new File(userDir, sourceFileName);
    @Test // 1s
    public void read_with_FileInputStream() throws IOException {
        for (int i = 0; i < 1000; i++){
            FileInputStream fileInputStream = new FileInputStream(sourceFile);
            int read = fileInputStream.read();
            while (read != -1){
                read = fileInputStream.read();
            }
            fileInputStream.close();
        }
    }

    @Test // 0.029s
    public void read_with_FileInputStream_read_800_bytes_each_time() throws IOException {
        for (int i = 0; i < 1000; i++){
            FileInputStream fileInputStream = new FileInputStream(sourceFile);
            byte[] bytes = new byte[800];
            int read = fileInputStream.read(bytes);
            while (read == 800){
                read = fileInputStream.read(bytes);
            }
            fileInputStream.close();
        }
    }

    @Test // 0.093s
    public void read_with_FileInputStream_and_BufferedInputStream() throws IOException {
        for (int i = 0; i < 1000; i++){
            InputStream fileInputStream = new BufferedInputStream(new FileInputStream(sourceFile));
            int read = fileInputStream.read();
            while (read != -1){
                read = fileInputStream.read();
            }
            fileInputStream.close();
        }
    }

    @Test // 0.043s
    public void read_with_FileInputStream_and_BufferedInputStream_read_800_bytes_each_time() throws IOException {
        for (int i = 0; i < 1000; i++){
            InputStream fileInputStream = new BufferedInputStream(new FileInputStream(sourceFile));
            byte[] bytes = new byte[800];
            int read = fileInputStream.read(bytes);
            while (read == 800){
                read = fileInputStream.read(bytes);
            }
            fileInputStream.close();
        }
    }