Export (0) Print
Expand All

Composing Streams

A backing store is a storage medium, such as a disk or memory. Each different backing store implements its own stream as an implementation of the Stream class. Each stream type reads and writes bytes from and to its given backing store. Streams that connect to backing stores are called base streams. Base streams have constructors that have the parameters necessary to connect the stream to the backing store. For example, FileStream has constructors that specify a path parameter, which specifies how the file will be shared by processes, and so on.

The design of the System.IO classes provides simplified stream composing. Base streams can be attached to one or more pass-through streams that provide the functionality you want. A reader or writer can be attached to the end of the chain so that the preferred types can be read or written easily.

The following code example creates a FileStream around the existing MyFile.txt in order to buffer MyFile.txt. (Note that FileStreams are buffered by default.) Next, a StreamReader is created to read characters from the FileStream, which is passed to the StreamReader as its constructor argument. ReadLine reads until Peek finds no more characters.

Imports System
Imports System.IO

Public Class CompBuf
    Private Const FILE_NAME As String = "MyFile.txt" 

    Public Shared Sub Main()
        If Not File.Exists(FILE_NAME) Then
            Console.WriteLine("{0} does not exist!", FILE_NAME)
            Return 
        End If 
        Dim fsIn As new FileStream(FILE_NAME, FileMode.Open, _
            FileAccess.Read, FileShare.Read)
        ' Create an instance of StreamReader that can read 
        ' characters from the FileStream. 
        Using sr As New StreamReader(fsIn)
            Dim input As String 
            ' While not at the end of the file, read lines from the file. 
            While sr.Peek() > -1
                input = sr.ReadLine()
                Console.WriteLine(input)
            End While 
        End Using 
    End Sub 
End Class

The following code example creates a FileStream around the existing MyFile.txt in order to buffer MyFile.txt. (Note that FileStreams are buffered by default.) Next, a BinaryReader is created to read bytes from the FileStream, which is passed to the BinaryReader as its constructor argument. ReadByte reads until PeekChar finds no more bytes.

Imports System
Imports System.IO

Public Class ReadBuf
    Private Const FILE_NAME As String = "MyFile.txt" 

    Public Shared Sub Main()
        If Not File.Exists(FILE_NAME) Then
            Console.WriteLine("{0} does not exist.", FILE_NAME)
            Return 
        End If 
        Dim f As New FileStream(FILE_NAME, FileMode.Open, _
            FileAccess.Read, FileShare.Read)
        ' Create an instance of BinaryReader that can 
        ' read bytes from the FileStream. 
        Using br As new BinaryReader(f)
            Dim input As Byte 
            ' While not at the end of the file, read lines from the file. 
            While br.PeekChar() > -1
                input = br.ReadByte()
                Console.WriteLine (input)
            End While 
        End Using 
    End Sub 
End Class
Show:
© 2014 Microsoft