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?

3 Answers 3

12

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Makes sense to me, since Java represents compression as Input/Output streams anyway.
This method has a 2GB limit (just to clarify)
And when you call ByteArrayOutputStream#toByteArray stream calls Arrays.copyOf which can cause OutOfMemoryError
4

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

Comments

2

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.