When to use a byte array and when to use a stream?

I need to send images and small video files (about 5 MB, less than 10 MB) to the REST service that I will write. I am wondering whether to use Byte [] or Stream to accomplish this task. What will be the dividing line in terms of transfer file size between using Byte [] and Stream?

+6
source share
3 answers

The amount of free memory that you are ready to complete transactions is your only real limitation.

If you have a 5M file, you need to load the whole thing into RAM, which will cost 5 million.

If you exchange it, you can use much less memory by reading small chunks from a file into a reusable buffer and writing these fragments to an HTTP stream.

+9
source

Ultimately, you are going to send a stream anyway.

If you received data from another source outside your control as a massive byte[] , you can also save it in this form for your processing, if it is not convenient for you, and let it be transferred to the network stream down the line.

If you get it as a stream, it makes no sense to turn it into a massive byte[] so that it is transferred to another stream. Use a buffer size of 4 or 8 kiB (4 or 8, not 4-8, as there are some minor advantages in all matches with all memory numbers that you can use).

If you create it yourself, the stream in most cases is facilitated (wrapping in binary or text script, as well as its execution), as well as more efficient.

In general, if I see a buffer larger than 8kiB that is written to or read from the stream, I should note that you should try to change first if something seems too slow.

+5
source

The general test when choosing between an array or a stream of bytes depends on whether you know, in front, the number of bytes in the data and if this number is small enough for a given purpose.

For example, if you are dealing with a small icon file (less than 50 KB) available on the local computer, and you know the file size, go to the byte array.

And vice versa, if you are working with a movie file, where it would be difficult, not to mention the need, to immediately save 2 GB of content in memory, use the stream.

Streams are best suited for processing large data sets or data whose length is not known in front.

0
source

Source: https://habr.com/ru/post/906214/


All Articles