Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do most serializers use a stream instead of a byte array?

I am currently working on a socket server and I was wondering Why do serializers like

  • XmlSerializer
  • BinaryFormatter
  • Protobuf-net
  • DataContractSerializer

all require a Stream instead of a byte array?

like image 201
Jan-Fokke Avatar asked Mar 24 '17 13:03

Jan-Fokke


People also ask

Is a byte array a stream?

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.

What is the difference between Stream and MemoryStream?

Stream can be called as the parent of memory stream class, as the MemoryStream inherits Stream class.. Streams involve three fundamental operations: You can read from streams. Reading is the transfer of data from a stream into a data structure, such as an array of bytes.


2 Answers

It means you can stream to arbitrary destinations rather than just to memory.

If you want to write something to a file, why would you want to create a complete copy in memory first? In some cases that could cause you to use a lot of extra memory, possibly causing a failure.

If you want to create a byte array, just use a MemoryStream:

var memoryStream = new MemoryStream(); serializer.Write(foo, memoryStream); // Or whatever you're using var bytes = memoryStream.ToArray(); 

So with an abstraction of "you use streams" you can easily work with memory - but if the abstraction is "you use a byte array" you are forced to work with memory even if you don't want to.

like image 139
Jon Skeet Avatar answered Sep 22 '22 22:09

Jon Skeet


You can easily make a stream over a byte array...but a byte array is inherently size-constrained, where a stream is open-ended...big as you need. Some serialization can be pretty enormous.

Edit: Also, if I need to implement some kind of serialization, I want to do it for the most basic abstraction, and avoid having to do it over multiple abstractions. Stream would be my choice, as there are stream implementations over lots of things: memory, disk, network and so forth. As an implementer, I get those for "free".

like image 38
Clay Avatar answered Sep 19 '22 22:09

Clay