TagMemoryStream

Binary serialization

Today’s topic is Binary serialization, if you wish to read more about serialization or other types of serialization please visit this link:

Binary serialization is type of serialization where type fidelity and reference integrity are preserved, meaning assembly information, private and public fields are being preserved as well as theirs memory locations. You would normally use binary serialization to preserve application state, store it to a backing store (storage medium) and use it later to run application on same computer or maybe multiple other computers.

Hearth of binary serialization is BinaryFormatter class and its Serialize and Deserialize methods. Serialize and Deserialize  methods take two parameters stream and object graph and serialize or deserialize object accordingly. Result of serialization are unreadable to human eye but you can read some information:

Binary Formatter

BinaryFormatter serialization result

As you can see type and field information are preserved.

If you wish serialized data in more readable way you can use SoapFormatter. Note that SoapFormatter is obsolete so use it only for debugging and testing purposes.

SoapFormatter

SoapFormatter serialization result

To mark a type serializable use Serializable attribute to decorate type. Serializable attribute is not inherited so if you wish to serialize both base and derived class decorate both of them with serializable attribute. Other approach would be to implement ISerializable interface.

If you wish not to serialize filed add NonSerialized attribute.

You can serialize null types, but you can not serialize or deserialize null streams.

Properties are not serialized but their private fields are. Do not serialize auto implemented properties because you cannot guarantee that theirs private fields will have the same name on the other machines as they do on yours.

If we wish more control during Serialization/Deserialization processes we can use attributes from System.Runtime.Serialization and apply them to the methods.
Attributes we can use for methods during serialization:
OnSerializingAttribute – use this attribute to prepare data before deserializing . For example encrypt password field or change distance from miles to kilometers.
OnSerializedAttribute – restores type to a status it was before applying OnSerializingAttribute.

Attributes we can use for methods during serialization:
OnDeserializingAttribute – because serialization cannot use constructor, use this attribute for method that will simulate constructor, and maybe set fields to default values.
OnDeserializedAttribute – use this attribute to manually set value of the fields just after serialization process has finished.

 

Example for Binary serialization.

We will try to serialize data from Employee class:

 

 

Download link for entire solution is here.

Streams

 Streams are sequence of bytes that you can use to read from or write to a backing store.

Let’s break down last sentence into a few parts:

  1. „Stream is a sequence of bytes“ – When working with data it is preferable that we read data in chunks of bytes. By breaking data in chunks we do not have to wait for our application to retrieve all parts of a data. For example, we may be accessing a audio file from network location and we want to process that audio file. Retrieving that file would take time and our application would need to wait until retrieval process is done before it can do some job. When using Streams we can specify how large chunks will be. You have seen this example: when listening to a music from popular music streaming site you will see information about bit-rate, that bit-rate represents chunk of data that is downloaded to you device for consumption.
  2. „that you can use to read from or write“ – Stream allows us to read, write or both to a storage device.
  3. „to a backing store“ – backing store represents your local Hard drive, memory, network location or any type of storage medium.

System class in System.IO namespace is a base class for all streams.

When using streams type of file (.txt, .mp3, .mkv, .doc, …) does not matter because it will be transformed to a sequence of bytes and your job is to convert that sequence to a meaningful representation of data. Retrieving data from a backing store will store data in a memory so you can use it in your application.

Every backing store type has appropriate streaming class for reading and writing operations. These streams are called Base Streams retrieve bytes. To consume:

  • FileSytem (local Hard drive, usbv drive…) you would use FileStream
  • Local memory -> MemoryStream
  • Remote location (Internet, Local network) -> NetworkStream

You use Stream Adapters allow you to transform bytes to types you would usually use in your application, such as integers and strings, by giving you access to classes and methods to consume data. Most commonly used are:

  • StreamReader/StreamWriter
  • BinaryReader/BinaryWriter
  • XmlReader/XmlWriter

For Streams to know where is beginning, current position, and end of a stream, pointer is used. Default position of pointer is at the beginning of a stream, or poistion 0. Streaming ends when pointer hits last position in a stream.

Example:

Lets say that we need to read 10 bytes which will be redistributed to positions 0-9, and we read data in chunks of 4 bytes. First we will read first 4 bytes and pointer will be at the position 3, then next four bytes are read and position will move to 7 and finally last two bytes will be read an pointer will be moved to position 9 and stream will end because end of stream is reached.

Caution! Sometimes, mostly while using network streams, when we expect stream to retrieve certain number of bytes we will not retrieve them all, so be careful when reading or writing data and always check if all bytes are written or read.

Is it possible to set pointer at certain position of a stream?

Yes it is, that process is called seeking. Note that MemoryStream andFile stream support it but NetworkStream does not. Also seeking allows us to read from beginning, end or custom location from within a stream.

Do I have to dispose of a Streams?

Yes, you should always dispose of a stream or or use using statement.

This conclude introduction to Streams lets see some examples.

Example of using FileStream:

 

 

© 2021 LearnC#WithDarko

Theme by Anders NorenUp ↑