大字节数组传输到客户端

发布于 2024-11-02 01:10:21 字数 400 浏览 7 评论 0原文

让我介绍一下我的情况。

我有很多以字节为单位的数据存储在服务器上的文件中。我正在使用 JDK7 中提供的 AIO 编写和读取此文件。因此,我使用 ByteBuffer 进行读取和写入操作。

问题是,一旦我在 AsynchronousFileChannel 上执行了读取,我想将读取操作中使用的 ByteByffer 的内容传输到客户端。因此我实际上想发送字节。

从这里出发的最佳方式是什么?我不想发送 ByteBuffer,因为我有一个可以重复使用的池,因此这不是一个选项。我还希望能够组合多次读取并一次发送组合的多个 ByteBuffer 的内容。

那我送什么呢。只是一个 byte[] 数组?或者我需要一些流吗?关于性能的最佳解决方案是什么?

我正在使用 RMI 进行通信。

提前致谢。

Let me present my situation.

I have a lot of data in bytes stored in files on server. I am writing and reading this files using AIO that is coming in JDK7. Thus, I am using ByteBuffer(s) for read and write operations.

The question is once I have performed a read on AsynchronousFileChannel I want to transfer the content of the ByteByffer that was used in read operation to the client. Thus I actually want to send the bytes.

What would be the best way to go from here. I don't want to send the ByteBuffer, because I have a pool of them that I reuse, thus this is not an option. I want to be able also to even maybe combine several reads and send the content of several ByteBuffer(s) combined at once.

So what do I send. Just a byte[] array? Or do I need some stream? What be the best solution regarding performance here.

I am using RMI for communication.

Thanx in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

月下客 2024-11-09 01:10:21

您可以使用 RMIIO 库模拟 rmi 上的流,这将允许您通过流式传输任意数量的字节RMI 不会在任一端造成内存问题。

(免责声明,我写了这个库)

You can simulate streams over rmi using the RMIIO library, which will allow you to stream arbitrary amounts of bytes via RMI without causing memory problems on either end.

(disclaimer, i wrote the library)

西瑶 2024-11-09 01:10:21

除非有充分的理由不这样做,否则只需发送字节数组以及足够的元数据即可提供可靠的服务。

需要通过 RMI 来回传输的底层实现越少越好。特别是当您使用尚未普遍可用的 Java 7 时。

Unless there is a very good reason not to, then just send the byte array along with sufficient meta data that you can provide reliable service.

The less of the underlying implementation you need to transfer back and forth over RMI, the better. Especially when you work with Java 7 which is not yet generally available.

小女人ら 2024-11-09 01:10:21

要使用 RMI,您必须以 byte[] 形式检索缓冲区的内容,然后将其写入 ObjectOutputStream(写入发生在幕后)。假设您当前正在使用直接缓冲区,这意味着在 Java 堆中创建数组的 CPU 时间,以及在写入数组后对其进行垃圾收集的 CPU 时间,以及流保留引用时间过长的可能性,导致内存不足错误。

在我看来,更好的方法是打开一个到目的地的 SocketChannel 并用它来写入缓冲区的内容。当然,为了完成这项工作,您需要编写额外的数据来描述缓冲区的大小,这可能会演变成一种通信协议。

To use RMI you have to retrieve the contents of the buffer as a byte[], then write it to an ObjectOutputStream (the write happens under the covers). Assuming that you're currently using direct buffers, this means CPU time to create the array in the Java heap, and CPU time to garbage-collect that array once it's been written, and the possibility that the stream will hold onto the reference too long, causing an out-of-memory error.

A better approach, in my opinion, is to open a SocketChannel to the destination and use it to write the buffer's contents. Of course, to make this work you'll need to write additional data describing the size of the buffer, and this will probably evolve into a communication protocol.

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