Input/Output 8: Stream
The Stream class is an abstract class that is implemented by BufferedStream, FileStream, MemoryStream, NetworkStream, and CryptoStream. The two child classes that are frequently used and will be discussed in this section are FileStream and BufferedStream.
Even though you cannot instantiate a Stream object directly, sometimes you obtain a Stream object from a method, such as the OpenFile method of the System.Windows.Forms.OpenFileDialog class. To improve performance, you might want to wrap the Stream object inside of a BufferedStream object.
The Stream class provides functionality that enables programmers to manipulate a Stream. For example, the Length property returns the number of bytes in the Stream object, and the Position property specifies the position of the pointer in the stream. Three other properties are CanRead, CanWrite, and CanSeek, all of which are self-explanatory. Among others, the Stream class provides methods for reading and writing. When a read or write operation is performed, the position of the Stream is advanced by the number of bytes read or written.
Getting into it
Closes the Stream object and frees all of the resources it consumes. Even though unused resources will eventually be garbage collected, it is good programming practice to always call this method when you no longer need a Stream object.
Forces the contents of the buffer to be written to the underlying device. For example, in a FileStream, this method writes the buffer to the file.
Reads available data to an array of bytes. This method returns an Integer that indicates the number of bytes read. If the pointer is at the end of the stream when Read is called, the method will return zero. The method has the following signature:
public abstract int Read(
in byte buffer,
The bytes read will be placed in the buffer. You can specify the maximum number of bytes that can be read in count. If count is greater than the number of available bytes, only the available bytes will be returned. The offset argument specifies the number of bytes that need to be skipped before starting to read. If you specify 0 for offset, the read operation will start from the byte pointed to by the stream pointer.
This method reads one byte and casts it into an integer, which is returned by the method. If the end of the stream has been reached when this method is called, a -1 is returned. The method has no parameters.
This method writes an array of bytes into the stream. It does not return any value, and has the following signature:
public abstract void Write(
The Write method writes the buffer byte array into the stream. It will write the number of bytes specified by count and skip the offset number of bytes in buffer before starting the write operation. Note that count must not be greater than the number of available bytes in the buffer.
Writes a single byte to the stream. This method accepts a byte as its argument and does not return a value.
Feel free to comment this article using a facebook profile.
I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.