Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I buffer the InputStream or the InputStreamReader?

What are the differences (if any) between the following two buffering approaches?

Reader r1 = new BufferedReader(new InputStreamReader(in, "UTF-8"), bufferSize); Reader r2 = new InputStreamReader(new BufferedInputStream(in, bufferSize), "UTF-8"); 
like image 635
bdkosher Avatar asked Aug 11 '10 14:08

bdkosher


People also ask

What is the difference between InputStreamReader and BufferedReader?

BufferedReader reads a couple of characters from the Input Stream and stores them in a buffer. InputStreamReader reads only one character from the input stream and the remaining characters still remain in the streams hence There is no buffer in this case.

What is the difference between InputStream and InputStreamReader?

An InputStream is typically always connected to some data source, like a file, network connection, pipe etc. This is also explained in more detail in the Java IO Overview text. InputStreamReader takes an inputstream and converts the bytes Strem into characters when you are reading it.

Do we need to close InputStreamReader?

Therefore, if you close the Reader, you don't need to also close the InputStream.

Why do we use InputStreamReader?

The InputStreamReader class of the java.io package can be used to convert data in bytes into data in characters. It extends the abstract class Reader . The InputStreamReader class works with other input streams. It is also known as a bridge between byte streams and character streams.


1 Answers

r1 is more efficient. The InputStreamReader itself doesn't have a large buffer. The BufferedReader can be set to have a larger buffer than InputStreamReader. The InputStreamReader in r2 would act as a bottleneck.

In a nut: you should read the data through a funnel, not through a bottle.


Update: here's a little benchmark program, just copy'n'paste'n'run it. You don't need to prepare files.

package com.stackoverflow.q3459127;  import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader;  public class Test {      public static void main(String... args) throws Exception {          // Init.         int bufferSize = 10240; // 10KB.         int fileSize = 100 * 1024 * 1024; // 100MB.         File file = new File("/temp.txt");          // Create file (it's also a good JVM warmup).         System.out.print("Creating file .. ");         BufferedWriter writer = null;         try {             writer = new BufferedWriter(new FileWriter(file));             for (int i = 0; i < fileSize; i++) {                 writer.write("0");             }             System.out.printf("finished, file size: %d MB.%n", file.length() / 1024 / 1024);         } finally {             if (writer != null) try { writer.close(); } catch (IOException ignore) {}         }          // Read through funnel.         System.out.print("Reading through funnel .. ");         Reader r1 = null;                 try {             r1 = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"), bufferSize);             long st = System.nanoTime();             for (int data; (data = r1.read()) > -1;);             long et = System.nanoTime();             System.out.printf("finished in %d ms.%n", (et - st) / 1000000);         } finally {             if (r1 != null) try { r1.close(); } catch (IOException ignore) {}         }          // Read through bottle.         System.out.print("Reading through bottle .. ");         Reader r2 = null;                 try {             r2 = new InputStreamReader(new BufferedInputStream(new FileInputStream(file), bufferSize), "UTF-8");             long st = System.nanoTime();             for (int data; (data = r2.read()) > -1;);             long et = System.nanoTime();             System.out.printf("finished in %d ms.%n", (et - st) / 1000000);         } finally {             if (r2 != null) try { r2.close(); } catch (IOException ignore) {}         }          // Cleanup.         if (!file.delete()) System.err.printf("Oops, failed to delete %s. Cleanup yourself.%n", file.getAbsolutePath());     }  } 

Results at my Latitude E5500 with a Seagate Momentus 7200.3 harddisk:

 Creating file .. finished, file size: 99 MB. Reading through funnel .. finished in 1593 ms. Reading through bottle .. finished in 7760 ms. 
like image 114
BalusC Avatar answered Oct 13 '22 03:10

BalusC