Can I make the size of a byte array dynamic?

postmalloc picture postmalloc · Oct 9, 2013 · Viewed 8.2k times · Source

I'm using a byte array with the write() method of OutputStream to store the data into a file. For that, I'm initiating the byte array with a predefined size. But I don't want it to take up so much space for every InputStream whose size could be much lower than the size of the array. Is it possible to somehow make the array dynamic so that it adjusts its size depending on the size of the InputStream? I have something like this in my code:

byte[] b = new byte[20000];
int length;                 
while ((length = in.read(b))!= -1) {
    //writing it to a file
    op.write(b, 0, length);
    k++;
}

in.close();
op.close();

Here, in is the InputStream object and op is the FileOutputStream object. I've tried using ArrayLists, but there is no direct method of FileOutputStream to write data into an ArrayList, it only accepts byte[].

Answer

Jon Skeet picture Jon Skeet · Oct 9, 2013

The problem isn't on the output side at all - it's just that you don't know how big the input is.

It's not clear where the InputStream is coming from - if the input is being received over the network for example, you may not have any indication of the size beforehand. InputStream itself doesn't have any simple way of checking whether the stream has a known length.

Bear in mind that the 20K array here is only important while you're copying the data. After that, it can be garbage collected. Do you have any indication that this is actually an issue?

You could just reduce the size of the array to (say) 4K or 8K. You'd still be able to handle any size of input, as you're looping over it and copying it in chunks. It just wouldn't be quite as efficient in cases where you can read more than that buffer size in one go. The extent of that efficiency difference will be very context-sensitive - only you can measure whether how that code will perform in your real situation - or how important that performance is in terms of time, or how useful it is to save a few thousand bytes which can be garbage collected immediately afterwards anyway.