I need to send images and small video files (around 5MB, less than 10MB) to a REST service, which I will write. I am wondering whether I should use Byte[] or Stream to accomplish this task. What would be the dividing line in terms of transfer file size between using Byte[] and Stream?
ByteArray is an extremely powerful Class that can be used for many things related to data manipulation, including (but not limited to) saving game data online, encrypting data, compressing data, and converting a BitmapData object to a PNG or JPG file.
Streams are commonly written to byte arrays. The byte array is the preferred way to reference binary data in . NET. It can be used to data like the contents of a file or the pixels that make up the bitmap for an image.
These handle data in bytes (8 bits) i.e., the byte stream classes read/write data of 8 bits. Using these you can store characters, videos, audios, images etc.
You would use the FileStream to read/write a file but a MemoryStream to read/write in-memory data, such as a byte array decoded from a string. You would not use a Stream in and of itself, but rather use it for polymorphism, i.e. passing it to methods that can accept any implementation of Stream as an argument.
The general test when choosing between a byte array or stream depends on whether you know, up front, how many bytes there are in the data, and if this number is a reasonably small one for the given purpose.
For example, if you're dealing with a small icon file (less than 50KB) available on the local machine and you know the filesize, go with a byte array.
Conversely, if you're working with a movie file where it would be difficult, not to mention unnecessary, to keep 2GB of content in memory at once, use a stream.
Streams are best for handling larger sets of data or data whose length is not known up front.
Ultimately, you're going to be sending a stream either way.
If you received the data from another source outside of your control as the massive byte[]
, then you might as well keep it in that form for your processing unless it's inconvenient to you, and let it be pushed into the network stream down the line.
If you receive it as a stream, there's no point turning it into a massive byte[]
just to have it pushed to another stream. Use a buffer of 4 or 8 kiB (4 or 8 rather than 4 to 8, as there are some minor advantages in the whole-number-of-memory-page coincidences of those that you may as well take advantage of).
If you're creating it yourself, the stream is both easier for most cases (wrap in a binary or text writer, and work through that) as well as more efficient.
More generally, if I see a buffer of more than 8kiB that is being written to or read from a stream, then I'll note that as the first thing to try changing if things seem to be too slow.
The amount of free memory you're willing to commit to the transaction is your only real constraint.
If you have a 5M file, then you'll need to load the entire thing in to RAM, which will cost 5M.
If you stream it, you can use far less memory, by reading small chunks from the file in to a reusable buffer and writing those chunks to the HTTP stream.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With