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[].
 
     
     
     
    