将BufferedWriter和BufferedOutputStream一起使用是否过度? [英] Is it overkill to use BufferedWriter and BufferedOutputStream together?

查看:112
本文介绍了将BufferedWriter和BufferedOutputStream一起使用是否过度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个套接字。从阅读网络IO,在我看来,写入它的最佳方式是做这样的事情:

I want to write to a socket. From reading about network IO, it seems to me that the optimal way to write to it is to do something like this:

OutputStream outs=null;
BufferedWriter out=null;
out =
  new BufferedWriter(
    new OutputStreamWriter(new BufferedOutputStream(outs),"UTF-8"));

BufferedWriter 会缓冲输入到建议使用 OutputStreamWriter ,因为它会阻止编写器为每个字符启动编码器。

The BufferedWriter would buffer the input to the OutputStreamWriter which is recommended, because it prevents the writer from starting up the encoder for each character.

BufferedOutputStream 然后将缓冲 Writer 中的字节,以避免一次将一个字节放到网络上。

The BufferedOutputStream would then buffer the bytes from the Writer to avoid putting one byte at a time potentially onto the network.

看起来有点像矫枉过正,但这一切似乎都有帮助吗?
感谢任何帮助..

It looks a bit like overkill, but it all seems like it helps? Grateful for any help..

编辑:来自上的javadoc> OutputStreamWriter


每次调用write()方法都会导致在给定字符上调用编码转换器。生成的字节在写入底层输出流之前在缓冲区中累积。可以指定此缓冲区的大小,但默认情况下,它足够大,可用于大多数用途。请注意,传递给write()方法的字符不会被缓冲。

Each invocation of a write() method causes the encoding converter to be invoked on the given character(s). The resulting bytes are accumulated in a buffer before being written to the underlying output stream. The size of this buffer may be specified, but by default it is large enough for most purposes. Note that the characters passed to the write() methods are not buffered.

为了获得最高效率,请考虑在BufferedWriter中包装OutputStreamWriter,以避免频繁的转换器调用。例如:

For top efficiency, consider wrapping an OutputStreamWriter within a BufferedWriter so as to avoid frequent converter invocations. For example:



Writer out = new BufferedWriter(new OutputStreamWriter(System.out));


推荐答案


BufferedWriter会缓冲outputStreamWriter的输入,建议使用它,因为它会阻止编写器为每个字符启动编码器。

The BufferedWriter would buffer the input to the outputStreamWriter, which is recommended because it prevents the writer from starting up the encoder for each character.

由谁推荐,在什么情况下? 启动编码器是什么意思?你是不是一次给作家写一个角色? (我们不太了解你如何使用作者...这可能很重要。)

Recommended by who, and in what context? What do you mean by "starting up the encoder"? Are you writing a single character at a time to the writer anyway? (We don't know much about how you're using the writer... that could be important.)


BufferedOutputStream然后缓冲来自Writer的字节,以避免一次将一个字节放到网络上。

The BufferedOutputStream would then buffer the bytes from the Writer to avoid putting one byte at a time potentially onto the network.

是什么让你觉得它会写一个一次一个字节?我认为 OutputStreamWriter 不太可能一次写一个字节到底层编写器,除非你真的一次写一个字符。

What makes you think it would write one byte at a time? I think it very unlikely that OutputStreamWriter will write a byte at a time to the underlying writer, unless you really write a character at a time to it.

此外,我希望网络输出流使用 Nagle的算法避免发送单字节数据包。

Additionally, I'd expect the network output stream to use something like Nagle's algorithm to avoid sending single-byte packets.

与优化一样,你应该根据证据来做...你做过任何测试吗?没有这些缓冲层?

As ever with optimization, you should do it based on evidence... have you done any testing with and without these layers of buffering?

编辑:只是为了澄清,我不是说缓冲类是没用的。在某些情况下,他们绝对是正确的方法。我只是说,与所有优化一样,不应盲目使用它们。您应该考虑您要优化的内容(处理器使用情况,内存使用情况,网络使用情况等)和度量。这里有许多重要因素 - 其中最重要的是写模式。如果您已经大块地编写 - 编写大块字符数据 - 那么缓冲区的影响相对较小。如果你实际上是一次写一个角色给作家,那么它们会更重要。

Just to clarify, I'm not saying the buffering classes are useless. In some cases they're absolutely the right way to go. I'm just saying that as with all optimization, they shouldn't be used blindly. You should consider what you're trying to optimize for (processor usage, memory usage, network usage etc) and measure. There are many factors which matter here - not least of which is the write pattern. If you're already writing "chunkily" - writing large blocks of character data - then the buffers will have relatively little impact. If you're actually writing a single character at a time to the writer, then they would be more significant.

这篇关于将BufferedWriter和BufferedOutputStream一起使用是否过度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆