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:

 

 

2 Comments

  1. That’s cleared my thoughts. Thanks for cornuibtting.

    • NoKii

      02/03/2017 at 14:32

      Thank you for your comment! IF you have some questions about other topics or you wish me to write article about some topic please do send me a message.

Leave a Reply

Your email address will not be published.

*

© 2019 LearnC#WithDarko

Theme by Anders NorenUp ↑