简体   繁体   English

读取输入流到缓冲区的最佳方法

[英]Best way to read an input stream to a buffer

For reading any input stream to a buffer there are two methods. 要将任何输入流读取到缓冲区,有两种方法。 Can someone help me understand which is the better method and why? 有人可以帮我理解哪种方法更好,为什么? And in which situation we should use each method? 在哪种情况下我们应该使用每种方法?

  • Reading line by line and appending it to the buffer. 逐行读取并将其附加到缓冲区。

Eg: 例如:

public String fileToBuffer(InputStream is, StringBuffer strBuffer) throws IOException{
    StringBuffer buffer = strBuffer;
    InputStreamReader isr = null;

    try {
        isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line = null;

        while ((line = br.readLine()) != null) {
            buffer.append(line + "\n");
        }
    } finally {
        if (is != null) {
            is.close();
        }
        if (isr != null) {
            isr.close();
        }
    }

    return buffer.toString();
}

  • Reading up to buffer size ie 1024 bytes in a char array. 读取缓冲区大小,即char数组中的1024个字节。

Eg: 例如:

InputStreamReader isr = new InputStreamReader(is);
final int bufferSize = 1024;
char[] buffer = new char[bufferSize];
StringBuffer strBuffer = new StringBuffer();

/* read the base script into string buffer */
try {
    while (true) {
        int read = isr.read(buffer, 0, bufferSize);
        if (read == -1) {
           break;
        }

        strBuffer.append(buffer, 0, read);
     }
} catch (IOException e) {
}

Consider 考虑

public String fileToBuffer(InputStream is, StringBuffer strBuffer) throws IOException {
    StringBuilder sb = new StringBuilder(strBuffer);
    try (BufferedReader rdr = new BufferedReader(new InputStreamReader(is))) { 
        for (int c; (c = rdr.read()) != -1;) {
            sb.append((char) c);

        }
    }
    return sb.toString();
}

Depends on the purpose. 取决于目的。

For work with text files read lines (if you need them). 用于处理文本文件读取行(如果需要)。

For work with raw binary data use chunks of bytes. 对于原始二进制数据的使用,使用块的字节。

In you examples chunks of bytes are more robust. 在您的示例中,字节块更健壮。 What if a line is too long and breaks some of intermediate objects? 如果一条线太长并且打破了一些中间物体怎么办?

If your file is binary, do you know how big a line will be? 如果你的文件是二进制文件,你知道一行会有多大吗? May be the size of file. 可能是文件的大小。

Trying to "swallow" too big String may cause ErrorOutOfMemory. 试图“吞下”太大的字符串可能会导致ErrorOutOfMemory。 With 1024 bytes it (ok - almost) never happens. 有1024字节(好吧 - 几乎)永远不会发生。

Chunking by 1024 bytes may take longer, but its more reliable. 按1024字节分块可能需要更长时间,但它更可靠。

Using 'readLine' isn't so neat. 使用'readLine'并不是那么整洁。 The asker's method 2 is quite standard, but the below method is unique (and likely better): 提问者的方法2非常标准,但下面的方法是独特的(可能更好):

//read the whole inputstream and put into a string
public String inputstream2str(InputStream stream) {
  Scanner s = new Scanner(stream).useDelimiter("\\A");
  return s.hasNext()? s.next():"";
}

From a String you can convert to byte array or whatever buffer you want. 从String可以转换为字节数组或任何你想要的缓冲区。

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

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