Can I make the size of a byte array dynamic?

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

Fixed Size Byte Array

public : array<Byte>^ Foo(array<Byte>^ data) gets dynamic size managed array but how can I get fixed size managed byte array? I wanna force C# user to send me 8 byte array; and get 8 byte

How to declare a byte array of infinite size/dynamic byte array in Java?

I am declaring a byte array which is of unknown size to me as it keeps on updating, so how can I declare the byte array of infinite size/variable size?



Create a byte array with dynamic size in F#

In C# and Java a byte array can be created like this byte[] b = new byte[x]; where x denotes the size of the array. What I want to do is to do the same thing in F#. I have searched for how to do it a

How can I create semi dynamic byte array

How can generate a byte dynamically in c# code? Format as below : First byte is standard 88, second byte is the number of remaining byte, and the last one is the remaining bytes. Example: 1 byte: byte

What is the byte array size in this code? Can I make it more generic?

I read a tutorial ‘Read file in String with BufferedInputStream’, and I got a code from here: http://examples.javacodegeeks.com/core-java/io/bufferedinputstream/read-file-in-string-with-bufferedinputs

Dynamic Array List size

How can I set size of a dynamic array with Java? I tried setsize(…) with the array variable but not working. How can I do this?

How can I make the array dynamic?

I am doing a site in PHP and have a graph module. I have done the graph module in that x-axis values and y-axis values are passed as array like: <script type=text/javascript> var chart; var ch

Prepending the size of a byte array to the byte array

I have a byte array, for example: (def g (byte-array (map byte [0 1 2 3]))) How do I get the size of it and how do I prepend this size to the g byte array?

how can i creating dynamic size array of objects by new without using default constructor?

As the title says, is there a way to create a dynamic size array of objects with new, with this condition that I don’t want to call the default constructor ? something like : myclass* myarray = new my

C++ dynamic array size definition

I am used to java and php and now I need to write some c++ code. I got difficulties to create a BYTE-array with dynamic size. How to achieve this? int byteSize = shm.getMemorySize(); BYTE byte[44]; //

Answers

It doesn’t matter what size the buffer is from the point of view of the code. A fixed size of 8192 is sufficient for most purposes, regardless of the file length. Your code will work with any size greater than zero.

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.

Simply take some small amount of buffer. It works.

Long back stumbled with this sort of situation and the below piece of code working fine for me.

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

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

Asked and got clarification here long back :How the buffer byte array is continuously filling while streaming?