Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Byte array of unknown length in Java: Part II

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.

like image 405
Ian Durkan Avatar asked Jun 26 '11 00:06

Ian Durkan


1 Answers

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

like image 100
Chris Dennett Avatar answered Sep 29 '22 09:09

Chris Dennett