Streams and Byte Streams in VB.NET

In this article I will explain you about Streams and Byte Streams in VB.NET.
  • 3028

Streams

Whether they contain water or data, streams evoke an image of efficient flow. Suppose you have to transfer water from one tank to another. You could repeatedly fill a bucket with water from one tank and empty it into the other tank. However, if you simply use a pipe to join the two tanks and let the water flow as a stream, your work becomes very fast and easy!

The same concept applies to the flow of data. Streams in VB.NET allow you to carry data from one point to another quickly and efficiently. The data transfer can take place between files, sockets, objects, or even other streams.

Streams in CLR come in three forms: streams that read and write bytes, streams that read and write characters, and a stream to read and write primitive types. Table 1.1 lists the types of streams.

Table 1.1: Streams in .NET

Byte Streams

Character Streams

Primitive Type Stream

Stream TextReader/TextWriter BinaryReader/BinaryWriter
FileStream StreamReader/StreamWriter  
MemoryStream StringReader/StringWriter  
BufferedStream    

Byte Streams

Byte streams comprise classes that treat data in the stream as bytes. These streams are most useful when you work with data that is not in a format readable by humans.

Stream Class

In the CLR, the Stream class provides the base for other byte stream classes. If you want to implement your own byte-stream class, you have to override the Stream class, which is an abstract class. Table 1.2 highlights some of the properties of a stream.

Table 1.2: Some Important Properties of a Stream

Property

Use

CanRead Indicates whether the stream supports reading.
CanSeek Indicates whether the stream supports seeking (used for random access).
CanWrite Indicates whether the stream supports writing.
Length Returns the length of the stream.
Position Returns the current position of  the cursor in the stream.

The Stream class provides support for both synchronous and asynchronous reading and writing of data through the methods outlined in Table 1.3.

Table 1.3: Some Methods for Asynchronous Reads and Writes

Method

Use

BeginRead Start an asynchronous read.
BeginWrite Start an asynchronous write.
EndRead Ends an asynchronous read.
EndWrite End an asynchronous write.

Table 1.4 lists other methods you can use with the Stream class. Note that you should always call the Close method explicitly rather than depend upon the Garbage Collector (GC) to free any resources used by the stream.

Table 1.4: Other Useful Methods

Method

Use

Close Close the stream and frees up any resources.
Flush Flushes the current buffer to the file and then clears the buffer.
Read Reads the specified number of bytes into the buffer and increments the current position accordingly.
ReadByte Reads a single byte from the stream.
Seek Seeks to a specified position within the current stream.
Write Writes the specified number bytes from the buffer and increments the current position accordingly.
WriteByte Writes a single byte to the stream.

Conclusion

Hope this article would have helped you in understanding Streams and Byte Streams in VB.NET.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.