何时在 Java 中使用 BufferedInput/OutputStreams?
我有一个客户端程序,它将加密的对象发送到服务器,并使用一堆流来执行此操作:
//httpOutput is the HTTPUrlConnection request stream
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
rsaCipher.init(Cipher.ENCRYPT_MODE, certificate);
CipherOutputStream cipherOutput = new CipherOutputStream(httpOutput, rsaCipher);
BufferedOutputStream bufferedOutput = new BufferedOutputStream(cipherOutput);
ObjectOutputStream objectOutput = new ObjectOutputStream(bufferedOutput );
我是否将缓冲区放在正确的位置/这很重要吗?
I have a client program that sends an encrypted object to a server, and am using a bunch of streams to do it:
//httpOutput is the HTTPUrlConnection request stream
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
rsaCipher.init(Cipher.ENCRYPT_MODE, certificate);
CipherOutputStream cipherOutput = new CipherOutputStream(httpOutput, rsaCipher);
BufferedOutputStream bufferedOutput = new BufferedOutputStream(cipherOutput);
ObjectOutputStream objectOutput = new ObjectOutputStream(bufferedOutput );
Am I putting the buffer in the right place/does it matter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的。一般来说,最好将缓冲流尽可能靠近生成或使用数据的代码。这是假设较低层可以比小数据块更有效地处理大数据块。
这取决于底层的内容,特别是每个级别上分块的性能增益。如果性能提升很小,那么这并不重要。但如果性能增益很大(例如,在进行系统调用的阶段),那么适当的缓冲就非常重要。
在某些情况下,插入缓冲流可能会降低性能(有点)。例如:
在这些情况下,缓冲没有帮助,并且由于额外的方法调用和每次调用的缓冲区状态管理而增加了小性能成本。然而,即使流是应用程序的性能关键部分,性能损失也不太可能很大。 (但如果性能是一个重要要求,请对其进行分析!)
Yes. In general, it is best to put the buffered stream as close as possible to the code that produces or consumes the data. This is on the assumption that the lower layers can handle large chunks of data more efficiently than small chunks.
It depends on what is underneath, and specifically on what the performance gain of chunking is at each level. If the performance gain is small, then it doesn't matter much. But if the performance gain is large (e.g. at the stage where you are doing system calls), then having the buffering in place very important.
There are a few situations where inserting a buffered stream could degrade performance (a bit). For example:
In these cases, the buffering does not help, and adds a small performance cost due to the extra method calls and per-call buffer state management. However, the performance penalty is unlikely to be significant, even if the streaming is a performance critical part of the application. (But if performance is an important requirement, profile it!)
缓冲应该尽可能靠近原始流。在这种情况下,它应该包装 http 输出流。
The Buffering should happen the closest to the raw stream as possible. in this case it should be wrapping the http output stream.
您可能有兴趣阅读 有关 I/O 的 Java 文档性能。缓冲流允许您处理数据块而不是单个字节。在大多数情况下,这允许更快地从磁盘读取和写入。
我从未做过性能测试,但如果我使用缓冲流,通常会在包装的早期进行。例如,我将首先缓冲
httpOutput
流,然后对其应用 CipherOutputStream,最后应用 ObjectOutputStream。但是,我不确定为什么我这样做,这就是我一直这样做的方式 - 尽早应用缓冲。You might be interested in reading the Java documentation on I/O Performance. Buffered streams allow you to deal with chunks of data instead of individuals bytes. In most cases, this allows for faster reading and writing from disk.
I've never done performance testing, but if I'm using a buffered stream, it's usually very early on in the wrapping. For example, I would buffer the
httpOutput
stream first, then apply the CipherOutputStream to that, then finally the ObjectOutputStream. However, I'm not sure why I do it that way, it's just the way I've always done it - apply the buffering as early as possible.