tags:

views:

117

answers:

3

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?

Thanks,

+10  A: 

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.

Pointy
Makes sense to me, since Java represents compression as Input/Output streams anyway.
Licky Lindsay
Perfect solution!
Skuge
+2  A: 

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

EJP
A: 

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.

Peter Lawrey