简体   繁体   English

从 InputStream 读取的最快方法是什么?

[英]What's the fastest way to read from InputStream?

I'm trying to read from an input stream of a HttpURLConnection:我正在尝试从 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?有没有可能优化这个?

Maybe this will be a bit faster, cause the new Stream API in Java 8 ist using internaly a parallel mechanism: 也许这会更快一些,因为Java 8中的新Stream API使用internaly并行机制:

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);      
    }
  }

}

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 . 您的时间可能根本不用于读取输入流,而是阻塞等待输入或附加到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. 这是可能的。

In java inputstream we have method read(byte b[],off,len) which reads the from the input stream into the given byte array.在 java inputstream 中,我们有 read(byte b[],off,len) 方法,它从输入流中读取到给定的字节数组。 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.这里 off 是数组的起始索引,len 是要读取的最大字节数,b[] 是字节数组。 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. Read 方法将尝试读取最多 len 个字节,但此方法返回实际读取的字节数,因为我将无法读取所需的字节数。 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);
}                             

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM