简体   繁体   English

读取所有字符时,是否有理由在InputStreamReader上使用BufferedReader?

[英]Is there a reason to use BufferedReader over InputStreamReader when reading all characters?

I currently use the following function to do a simple HTTP GET. 我目前使用以下功能来执行简单的HTTP GET。

public static String download(String url) throws java.io.IOException {
    java.io.InputStream s = null;
    java.io.InputStreamReader r = null;
    //java.io.BufferedReader b = null;
    StringBuilder content = new StringBuilder();
    try {
        s = (java.io.InputStream)new URL(url).getContent();

        r = new java.io.InputStreamReader(s);
        //b = new java.io.BufferedReader(r);

        char[] buffer = new char[4*1024];
        int n = 0;
        while (n >= 0) {
            n = r.read(buffer, 0, buffer.length);
            if (n > 0) {
                content.append(buffer, 0, n);
            }
        }
    }
    finally {
        //if (b != null) b.close();
        if (r != null) r.close();
        if (s != null) s.close();
    }
    return content.toString();
}

I see no reason to use the BufferedReader since I am just going to download everything in sequence. 我没有理由使用BufferedReader因为我BufferedReader顺序下载所有内容。 Am I right in thinking there is no use for the BufferedReader in this case? 我是否认为在这种情况下BufferedReader没有用?

In this case, I would do as you are doing (use a byte array for buffering and not one of the stream buffers). 在这种情况下,我将按照您的方式做(使用字节数组进行缓冲,而不要使用流缓冲区之一)。

There are exceptions, though. 但是也有例外。 One place you see buffers (output this time) is in the servlet API. Servlet API中是您看到缓冲区(这次输出)的一个地方。 Data isn't written to the underlying stream until flush() is called, allowing you to buffer output but then dump the buffer if an error occurs and write an error page instead. 在调用flush()之前,不会将数据写入底层流,这使您可以缓冲输出,但是如果发生错误,则转储缓冲区,而改写错误页面。 You might buffer input if you needed to reset the stream for rereading using mark(int) and reset() . 如果需要使用mark(int)reset()重置流以进行重新读取,则可以缓冲输入。 For example, maybe you'd inspect the file header before deciding on which content handler to pass the stream to. 例如,在决定将流传递到哪个内容处理程序之前,您可能会检查文件头。

Unrelated, but I think you should rewrite your stream handling. 无关,但我认为您应该重写流处理。 This pattern works best to avoid resource leaks: 此模式最能避免资源泄漏:

    InputStream stream = new FileInputStream("in");
    try { //no operations between open stream and try block
        //work
    } finally { //do nothing but close this one stream in the finally
        stream.close();
    }

If you are opening multiple streams, nest try/finally blocks. 如果要打开多个流,请嵌套try / finally块。

Another thing your code is doing is making the assumption that the returned content is encoded in your VM's default character set (though that might be adequate, depending on the use case). 您的代码正在做的另一件事是假设返回的内容已编码为VM的默认字符集(尽管根据使用情况这可能就足够了)。

Each invocation of one of an InputStreamReader 's read() methods may cause one or more bytes to be read from the underlying byte-input stream. InputStreamReader的read()方法之一的每次调用都可能导致从基础字节输入流中读取一个或多个字节。 To enable the efficient conversion of bytes to characters, more bytes may be read ahead from the underlying stream than are necessary to satisfy the current read operation. 为了实现字节到字符的有效转换,与满足当前读取操作所需的字节数相比,可以从基础流中提前读取更多字节。

You are correct, if you use BufferedReader for reading HTTP content and headers you will want InputStreamReader so you can read byte for byte. 没错,如果您使用BufferedReader读取HTTP内容和标头,则需要InputStreamReader以便逐字节读取字节。

BufferedReader in this scenario sometimes does weird things...escpecially when it comes to reading HTTP POST headers, sometimes you will be unable to read the POST data, if you use the InputStreamReader you can read the content length and read that many bytes... 在这种情况下,BufferedReader有时会做一些奇怪的事情……尤其是在读取HTTP POST标头时,有时您将无法读取POST数据,如果使用InputStreamReader,则可以读取内容长度并读取那么多字节。 。

我的直觉告诉我,由于您已经在使用字节数组执行缓冲,因此使用BufferedReader是多余的。

暂无
暂无

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

相关问题 有没有理由不将InputStreamReader与BufferedReader包装在一起? - Is there ever a reason not to wrap InputStreamReader with BufferedReader? 使用InputStreamReader和BufferedReader读取HTTP响应的速度很慢? - Slow reading of HTTP response using InputStreamReader and BufferedReader? 读取多字节字符时InputStream和InputStreamReader之间的区别 - The difference between InputStream and InputStreamReader when reading multi-byte characters 更好地使用ObjectOut / InputStream或InputStreamReader / BufferedReader? - Better to use ObjectOut/InputStream or InputStreamReader / BufferedReader? 如何重置BufferedReader(或InputStreamReader)以两次使用readline()? - How to reset BufferedReader (or InputStreamReader) to use readline() twice? Java-BufferedReader无法读取字符 - Java - BufferedReader not reading characters 读取Java BufferedReader的所有内容,包括行终止字符 - Reading all content of a Java BufferedReader including the line termination characters 为什么我的Java客户端/服务器应用程序在使用ObjectInputStream时挂起但在我使用带有InputStreamReader的BufferedReader时却没有? - Why does my Java client/server app hang when using ObjectInputStream but doesn't when I use BufferedReader with InputStreamReader? 缓冲读取器或输入流读取器中的gc开销 - gc overhead in bufferedreader or inputstreamreader 关闭BufferedReader和InputStreamReader - Closing BufferedReader and InputStreamReader
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM