C# using streams
Asked Answered
H

8

135

Streams are kind of mysterious to me. I don't know when to use which stream and how to use them. Can someone explain to me how streams are used?

If I understand correctly, there are three stream types:

  • stream
  • read stream
  • write stream

Is this correct? And, for example, what is the difference between a Memorystream and a FileStream?

Hupp answered 10/9, 2009 at 9:40 Comment(2)
you may want to check #508247Kamilahkamillah
Take an array of bytes and then create a wrapper for it (Stream) which exposes some helpful methods such as reading, writing and changing position. Now you can create classes based on their backing store (FileStream, MemoryStream) which inherit from Stream and build upon that functionality based on the particular backing store.Palpitation
S
95

A stream is an object used to transfer data. There is a generic stream class System.IO.Stream, from which all other stream classes in .NET are derived. The Stream class deals with bytes.

The concrete stream classes are used to deal with other types of data than bytes. For example:

  • The FileStream class is used when the outside source is a file
  • MemoryStream is used to store data in memory
  • System.Net.Sockets.NetworkStream handles network data

Reader/writer streams such as StreamReader and StreamWriter are not streams - they are not derived from System.IO.Stream, they are designed to help to write and read data from and to stream!

Subnormal answered 10/9, 2009 at 9:48 Comment(6)
So, if i understand correctly, the stream contains the data and doesn't do anything with it. The reader and writer 'helper' classes can handle (manipulate) the data within the stream?Hupp
No, Stream is not data container, it is using to transfer data, for example FileStream transfers data from byte[] to phisical file, NetworkStream transfers byte[] by socket. Reader Writer classes are helper classes to write and read from stream, for example StreamReader can be used to read from Stream string not byte[]. if you will give FileStream as a parameter it will read from File, if NetworkStream from socket.Subnormal
additionally, StreamReader and StreamWriter are for reading and writing TEXT (character) streams.Libel
there has a good article to help you understand MemoryStream. codeproject.com/Articles/832387/…Slavonic
I'm not sure if there couldn't be other examples as well like an AudioStream if the audio were digitized on the fly, or a TemperatureStream again if there were a port specifically for that and it digitized the data from its port on the fly.Krakau
@user420667. good question. In both the AudioStream and TemperatureStream cases, they most likely would be BinaryStreams to the driver that is associated with the device. Or, you could create a CustomStream that's built specifically for the interface.Libel
D
66

To expand a little on other answers here, and help explain a lot of the example code you'll see dotted about, most of the time you don't read and write to a stream directly. Streams are a low-level means to transfer data.

You'll notice that the functions for reading and writing are all byte orientated, e.g. WriteByte(). There are no functions for dealing with integers, strings etc. This makes the stream very general-purpose, but less simple to work with if, say, you just want to transfer text.

However, .NET provides classes that convert between native types and the low-level stream interface, and transfers the data to or from the stream for you. Some notable such classes are:

StreamWriter // Badly named. Should be TextWriter.
StreamReader // Badly named. Should be TextReader.
BinaryWriter
BinaryReader

To use these, first you acquire your stream, then you create one of the above classes and associate it with the stream. E.g.

MemoryStream memoryStream = new MemoryStream();
StreamWriter myStreamWriter = new StreamWriter(memoryStream);

StreamReader and StreamWriter convert between native types and their string representations then transfer the strings to and from the stream as bytes. So

myStreamWriter.Write(123);

will write "123" (three characters '1', '2' then '3') to the stream. If you're dealing with text files (e.g. html), StreamReader and StreamWriter are the classes you would use.

Whereas

myBinaryWriter.Write(123);

will write four bytes representing the 32-bit integer value 123 (0x7B, 0x00, 0x00, 0x00). If you're dealing with binary files or network protocols BinaryReader and BinaryWriter are what you might use. (If you're exchanging data with networks or other systems, you need to be mindful of endianness, but that's another post.)

Diplosis answered 31/5, 2013 at 14:19 Comment(2)
StreamWriter and Reader adapter classes are seriously badly named. Thanks for mentioning that. How come they did come up with this name is still surprising to me.Carlyn
Also, even the binary writer and reader classes are badly named.Carlyn
P
23

Streams are good for dealing with large amounts of data. When it's impractical to load all the data into memory at the same time, you can open it as a stream and work with small chunks of it.

Ponce answered 10/9, 2009 at 9:47 Comment(2)
Would love to see an example of what you have just said "work with small chunks of it".Miscreance
Streams are good for small amounts of data too. If a C# programmer wants to manipulate the contents of a file, s/he must use streams, regardless of the amount of data. The same statement is true for networkstreams as well. Granted, if the programmer is coding is a lower-level language like C, then it is possible to write chars or bytes directly to a disk or socket, but even for a small amount of data, it is time-consuming and more prone to error.Libel
K
13

Stream is just an abstraction (or a wrapper) over a physical stream of bytes. This physical stream is called the base stream. So there is always a base stream over which a stream wrapper is created and thus the wrapper is named after the base stream type ie FileStream, MemoryStream etc.

The advantage of the stream wrapper is that you get a unified api to interact with streams of any underlying type usb, file etc.

Why would you treat data as stream - because data chunks are loaded on-demand, we can inspect/process the data as chunks rather than loading the entire data into memory. This is how most of the programs deal with big files, for eg encrypting an OS image file.

Karttikeya answered 20/7, 2016 at 11:25 Comment(0)
L
5

I would start by reading up on streams on MSDN: http://msdn.microsoft.com/en-us/library/system.io.stream.aspx

Memorystream and FileStream are streams used to work with raw memory and Files respectively...

Lindsaylindsey answered 10/9, 2009 at 9:44 Comment(1)
Thanks for the link. I loved that "You can browse through the source code online, download the reference for offline viewing, and step through the sources (including patches and updates) during debugging". This feature offers a new level of insight.Marnimarnia
Q
4

There is only one basic type of Stream. However in various circumstances some members will throw an exception when called because in that context the operation was not available.

For example a MemoryStream is simply a way to moves bytes into and out of a chunk of memory. Hence you can call Read and Write on it.

On the other hand a FileStream allows you to read or write (or both) from/to a file. Whether you can actually Read or Write depends on how the file was opened. You can't Write to a file if you only opened it for Read access.

Quartz answered 10/9, 2009 at 9:45 Comment(0)
G
1

I wouldn't call those different kind of streams. The Stream class have CanRead and CanWrite properties that tell you if the particular stream can be read from and written to.

The major difference between different stream classes (such as MemoryStream vs FileStream) is the backing store - where the data is read from or where it's written to. It's kind of obvious from the name. A MemoryStream stores the data in memory only, a FileStream is backed by a file on disk, a NetworkStream reads data from the network and so on.

Greaten answered 10/9, 2009 at 9:46 Comment(0)
J
0

Here are some commonly used stream classes from microsoft:

  • FileStream – for reading and writing to a file.
  • IsolatedStorageFileStream – for reading and writing to a file in isolated storage.
  • MemoryStream – for reading and writing to memory as the backing store.
  • BufferedStream – for improving performance of read and write operations.
  • NetworkStream – for reading and writing over network sockets.
  • PipeStream – for reading and writing over anonymous and named pipes.
  • CryptoStream – for linking data streams to cryptographic transformations.

MemoryStream can usually be used when you have a relatively small amount of data that you want to work with in memory and you know the size of your memory and the size of lets say the data to be loaded into memory. That will at least improve the performance since you have the needed data in memory and do not have to access from disk. Where as a FileStream is suitable when dealing with larger files which may not be really possible to load into memory.

For the "read stream" and "write stream", they're are not types of streams but ratherthe System.IO namespace also provides types for reading encoded characters from streams and writing them to streams. And these types include StreamReader and StreamWriter.

Jacintha answered 13/10, 2023 at 10:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.