Java 中的无限 ByteBuffer
我正在开发一个程序,其中我压缩大量信息并将其以字节形式存储在缓冲区中。我无法使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.您应该将大量信息存储在文件或数据库中,而不是内存中。迟早你会耗尽内存。
You should store large amounts of information in a file, or a database, not memory. Sooner or later you will run out of memory.
您可以使用 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.