Similar to "Byte array of unknown length in java" I need to be able to write an unknown number of bytes from a data source into a byte[] array. However I need the ability to read from bytes that were stored earlier, for a compression algorithm, so ByteArrayOutputStream doesn't work for me.
Right now I have a scheme where I allocate ByteBuffers of fixed size N, adding a new one as I reach N, 2N, 3N bytes etc. After the data is exhausted I dump all buffers into an array of now-known size.
Is there a better way to do this? Having fixed-size buffers reduces the flexibility of the compression algorithm.
What about using a circular byte buffer? It has the possibility to grow dynamically and is efficient.
There's an implementation here: http://ostermiller.org/utils/CircularByteBuffer.java.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With