Infinite ByteBuffer in Java
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?
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.
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.
精彩评论