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
?
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. TheStream
class deals with bytes.The concrete stream classes are used to deal with other types of data than bytes. For example:
FileStream
class is used when the outside source is a fileMemoryStream
is used to store data in memorySystem.Net.Sockets.NetworkStream
handles network dataReader/writer streams such as
StreamReader
andStreamWriter
are not streams - they are not derived fromSystem.IO.Stream
, they are designed to help to write and read data from and to stream!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...
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.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:
To use these, first you acquire your stream, then you create one of the above classes and associate it with the stream. E.g.
StreamReader and StreamWriter convert between native types and their string representations then transfer the strings to and from the stream as bytes. So
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
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.)
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.
Stream is just an abstraction (or a wrapper) over a
physical
stream of bytes. Thisphysical
stream is called thebase 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 ieFileStream
,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.