Java 中的无限 ByteBuffer

发布于 2024-09-12 03:25:40 字数 94 浏览 3 评论 0原文

我正在开发一个程序,其中我压缩大量信息并将其以字节形式存储在缓冲区中。我无法使用 ByteBuffer 因为我不知道最终的大小。

实现这一点的更好方法是什么?

I'm working on a program where I'm compressing a large amount on information and storing it in bytes in a buffer. I can't use ByteBuffer because I don't know the finall size.

What would be a better way to implement this?

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

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

发布评论

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

评论(3

稀香 2024-09-19 03:25:40

ByteArrayOutputStream 怎么样?当然,它并不那么方便,但它会做你想做的事。当你完成收集字节后,你可以弹出一个字节数组。

How about ByteArrayOutputStream? Granted, it's not exactly as convenient, but it'll do what you want. When you're finished gathering bytes you can just pop out a byte array.

鸠书 2024-09-19 03:25:40

您应该将大量信息存储在文件或数据库中,而不是内存中。迟早你会耗尽内存。

You should store large amounts of information in a file, or a database, not memory. Sooner or later you will run out of memory.

埖埖迣鎅 2024-09-19 03:25:40

您可以使用 Apache MINA IOBuffer 然而,它的调整大小算法相当昂贵。

我所做的是使用直接字节缓冲区,您不需要确切知道它有多大,因为未使用的空间消耗虚拟内存,而不是堆甚至主内存。在 64 位机器上,虚拟内存非常便宜。

您知道最终的尺寸不会比原始尺寸大很多。

You can use Apache MINA IOBuffer however its resizing algo is fairly expensive.

What I do is use a direct byte buffer, you don't need to know eactly how big it will be, as unused space consumes virtual memory, not heap or even main memory. On a 64-bit machine, virtual memory is very cheap.

You know the final size won't be much larger than the orginal.

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