yo_haha
yo_haha

Reputation: 359

What's the fastest way to read from InputStream?

I'm trying to read from an input stream of a HttpURLConnection:

InputStream input = conn.getInputStream();
InputStreamReader isr = new InputStreamReader((input));
BufferedReader br = new BufferedReader(isr);

StringBuilder out = new StringBuilder("");
String output;
while ((output = br.readLine()) != null) {
    out.append(output);
}

This does take too much time when the input stream contains a lot of data. Is it possible to optimize this?

Upvotes: 3

Views: 10637

Answers (3)

anuuz soni
anuuz soni

Reputation: 1

In java inputstream we have method read(byte b[],off,len) which reads the from the input stream into the given byte array. Here off is the starting index of the array, len is the maximum number of byte to be read and b[] is the byte array. Read method will attempt to read maximum of len number of bytes but this method returns number of actual byte read as many times i will fail to read the desired number of bytes. Here is the example:-

FileInputStream i=new FileInputStream("file path");
FIleOutputStream o=new FileOutputStream("file path");
byte a[]=new byte[1024];
for(int j;(j=i.read(a,0,1024))!=-1;){
    o.write(a,0,j);
}                             

Upvotes: 0

aw-think
aw-think

Reputation: 4803

Maybe this will be a bit faster, cause the new Stream API in Java 8 ist using internaly a parallel mechanism:

package testing;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.stream.Stream;

public class StreamTest {

  /**
   * @param args the command line arguments
   * @throws java.io.IOException
   */
  public static void main(String[] args) throws IOException {
    URL url = new URL("http://www.google.com");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setUseCaches(false);
    if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
      BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

      Stream<String> s = br.lines();
      s.parallel().forEach(System.out::println);      
    }
  }

}

Upvotes: 3

user207421
user207421

Reputation: 310860

There's nothing slow about this code. You can read millions of lines a second with this, if the input arrives fast enough. Your time probably isn't spent reading the input stream at all, but in either blocking waiting for input or in appending to the StringBuilder.

But you shouldn't be doing this at all. Most files can be processed a line at a time or a record at a time. Compilers process them a token at a time, and there aren't many more complex file-processing tasks than compilation. It's possible.

Upvotes: 0

Related Questions