I am reading a binary file that has structure :
(integer)(byte[])
The integer represents the size of the byte array that comes next. To avoid allocate all time a byte array, i just allocate when the size of the next read is greater than the last one.
int sz = rawBuffer.getInt();
if (sz > lSz){ //lSz is a class variable
buffer = new byte[sz];
lSz = sz;
}
rawBuffer.get(buffer, 0, sz);
The problem is that the buffer has always the maximum size. If a read a byte array of size 5 and the a byte array of size 3, the buffer.length is going to be 5.
Is there anyway to resize the buffer, without need to realocate? Or is it better to have another structure that holds the efective size of the array, like a ByteBuffer for example.
ArrayList?