何时在 Java 中使用 BufferedInput/OutputStreams?

发布于 2024-10-31 05:42:59 字数 525 浏览 0 评论 0原文

我有一个客户端程序,它将加密的对象发送到服务器,并使用一堆流来执行此操作:

    //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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

信仰 2024-11-07 05:42:59

我是否将缓冲区放在正确的位置?

是的。一般来说,最好将缓冲流尽可能靠近生成或使用数据的代码。这是假设较低层可以比小数据块更有效地处理大数据块。

这重要吗?

这取决于底层的内容,特别是每个级别上分块的性能增益。如果性能提升很小,那么这并不重要。但如果性能增益很大(例如,在进行系统调用的阶段),那么适当的缓冲就非常重要。


在某些情况下,插入缓冲流可能会降低性能(有点)。例如:

  • 如果应用程序代码(生产者/消费者)正在大块地读取/写入数据,或者
  • 流正在写入/读取内存缓冲区,并且流堆栈中没有过滤/转换,那么从卡盘中受益。

在这些情况下,缓冲没有帮助,并且由于额外的方法调用和每次调用的缓冲区状态管理而增加了性能成本。然而,即使流是应用程序的性能关键部分,性能损失也不太可能很大。 (但如果性能是一个重要要求,请对其进行分析!)

Am I putting the buffer in the right place?

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.

Does it matter?

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:

  • if the application code (the producer / consumer) is reading / writing data in large chunks, or
  • if the stream is writing to / reading from an in-memory buffer and there is no filtering / conversion in the stream stack that would benefit from chucking.

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!)

自由范儿 2024-11-07 05:42:59

缓冲应该尽可能靠近原始流。在这种情况下,它应该包装 http 输出流。

The Buffering should happen the closest to the raw stream as possible. in this case it should be wrapping the http output stream.

西瓜 2024-11-07 05:42:59

您可能有兴趣阅读 有关 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文