Export (0) Print
Expand All

BufferedStream Class

Adds a buffering layer to read and write operations on another stream. This class cannot be inherited.

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

'Declaration
<ComVisibleAttribute(True)> _
Public NotInheritable Class BufferedStream
	Inherits Stream
'Usage
Dim instance As BufferedStream

/** @attribute ComVisibleAttribute(true) */ 
public final class BufferedStream extends Stream
ComVisibleAttribute(true) 
public final class BufferedStream extends Stream

For an example of creating a file and writing text to a file, see How to: Write Text to a File. For an example of reading text from a file, see How to: Read Text from a File. For an example of reading from and writing to a binary file, see How to: Read and Write to a Newly Created Data File.

A buffer is a block of bytes in memory used to cache data, thereby reducing the number of calls to the operating system. Buffers improve read and write performance. A buffer can be used for either reading or writing, but never both simultaneously. The Read and Write methods of BufferedStream automatically maintain the buffer.

BufferedStream can be composed around certain types of streams. It provides implementations for reading and writing bytes to an underlying data source or repository. Use BinaryReader and BinaryWriter for reading and writing other data types. BufferedStream is designed to prevent the buffer from slowing down input and output when the buffer is not needed. If you always read and write for sizes greater than the internal buffer size, then BufferedStream might not even allocate the internal buffer. BufferedStream also buffers reads and writes in a shared buffer. It is assumed that you will almost always be doing a series of reads or writes, but rarely alternate between the two of them.

The following code example shows how to use the BufferedStream class over the NetworkStream class to increase the performance of certain I/O operations. Start the server on a remote computer before starting the client. Specify the remote computer name as a command-line argument when starting the client. Vary the dataArraySize and streamBufferSize constants to view their effect on performance.

' Compile using /r:System.dll.
Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Globalization
Imports System.Net
Imports System.Net.Sockets

Public Class Client 

    Const dataArraySize As Integer    =   100
    Const streamBufferSize As Integer =  1000
    Const numberOfLoops As Integer    = 10000

    Shared Sub Main(args As String()) 
    
        ' Check that an argument was specified when the 
        ' program was invoked.
        If args.Length = 0 Then
            Console.WriteLine("Error: The name of the host " & _
                "computer must be specified when the program " & _ 
                "is invoked.")
            Return
        End If

        Dim remoteName As String = args(0)

        ' Create the underlying socket and connect to the server.
        Dim clientSocket As New Socket(AddressFamily.InterNetwork, _
            SocketType.Stream, ProtocolType.Tcp)

        clientSocket.Connect(New IPEndPoint( _
            Dns.Resolve(remoteName).AddressList(0), 1800))

        Console.WriteLine("Client is connected." & vbCrLf)

        ' Create a NetworkStream that owns clientSocket and then 
        ' create a BufferedStream on top of the NetworkStream.
        Dim netStream As New NetworkStream(clientSocket, True)
        Dim bufStream As New _
            BufferedStream(netStream, streamBufferSize)
        
        Try
            ' Check whether the underlying stream supports seeking.
            If bufStream.CanSeek Then
                Console.WriteLine("NetworkStream supports" & _
                    "seeking." & vbCrLf)
            Else
                Console.WriteLine("NetworkStream does not " & _
                    "support seeking." & vbCrLf)
            End If

            ' Send and receive data.
            If bufStream.CanWrite Then
                SendData(netStream, bufStream)
            End If            
            If bufStream.CanRead Then
                ReceiveData(netStream, bufStream)
            End If
        Finally

            ' When bufStream is closed, netStream is in turn 
            ' closed, which in turn shuts down the connection 
            ' and closes clientSocket.
            Console.WriteLine(vbCrLf & "Shutting down the connection.")
            bufStream.Close()
        End Try
    End Sub

    Shared Sub SendData(netStream As Stream, bufStream As Stream)
    
        Dim startTime As DateTime 
        Dim networkTime As Double, bufferedTime As Double 

        ' Create random data to send to the server.
        Dim dataToSend(dataArraySize - 1) As Byte
        Dim randomGenerator As New Random()
        randomGenerator.NextBytes(dataToSend)

        ' Send the data using the NetworkStream.
        Console.WriteLine("Sending data using NetworkStream.")
        startTime = DateTime.Now
        For i As Integer = 1 To numberOfLoops
            netStream.Write(dataToSend, 0, dataToSend.Length)
        Next i
        networkTime = DateTime.Now.Subtract(startTime).TotalSeconds
        Console.WriteLine("{0} bytes sent in {1} seconds." & vbCrLf, _
            numberOfLoops * dataToSend.Length, _
            networkTime.ToString("F1"))

        ' Send the data using the BufferedStream.
        Console.WriteLine("Sending data using BufferedStream.")
        startTime = DateTime.Now
        For i As Integer = 1 To numberOfLoops
            bufStream.Write(dataToSend, 0, dataToSend.Length)
        Next i
        
        bufStream.Flush()
        bufferedTime = DateTime.Now.Subtract(startTime).TotalSeconds
        Console.WriteLine("{0} bytes sent In {1} seconds." & vbCrLf, _
            numberOfLoops * dataToSend.Length, _
            bufferedTime.ToString("F1"))

        ' Print the ratio of write times.
        Console.Write("Sending data using the buffered " & _
            "network stream was {0}", _
            (networkTime/bufferedTime).ToString("P0"))
        If bufferedTime < networkTime Then
            Console.Write(" faster")
        Else
            Console.Write(" slower")
        End If
        Console.WriteLine(" than using the network stream alone.")
    End Sub

    Shared Sub ReceiveData(netStream As Stream, bufStream As Stream)
    
        Dim startTime As DateTime 
        Dim networkTime As Double, bufferedTime As Double = 0

        Dim bytesReceived As Integer = 0
        Dim receivedData(dataArraySize - 1) As Byte

        ' Receive data using the NetworkStream.
        Console.WriteLine("Receiving data using NetworkStream.")
        startTime = DateTime.Now
        While bytesReceived < numberOfLoops * receivedData.Length
            bytesReceived += netStream.Read( _
                receivedData, 0, receivedData.Length)
        End While
        networkTime = DateTime.Now.Subtract(startTime).TotalSeconds
        Console.WriteLine("{0} bytes received in {1} " & _
            "seconds." & vbCrLf, _
            bytesReceived.ToString(), _
            networkTime.ToString("F1"))

        ' Receive data using the BufferedStream.
        Console.WriteLine("Receiving data using BufferedStream.")
        bytesReceived = 0
        startTime = DateTime.Now
        While bytesReceived < numberOfLoops * receivedData.Length
            bytesReceived += bufStream.Read( _
                receivedData, 0, receivedData.Length)
        End While
        bufferedTime = DateTime.Now.Subtract(startTime).TotalSeconds
        Console.WriteLine("{0} bytes received in {1} " & _
            "seconds." & vbCrLf, _
            bytesReceived.ToString(), _
            bufferedTime.ToString("F1"))

        ' Print the ratio of read times.
        Console.Write("Receiving data using the buffered " & _
            "network stream was {0}", _
            (networkTime/bufferedTime).ToString("P0"))
        If bufferedTime < networkTime Then
            Console.Write(" faster")
        Else
            Console.Write(" slower")
        End If
        Console.WriteLine(" than using the network stream alone.")
    End Sub
End Class

import System.*;
import System.IO.*;
import System.Globalization.*;
import System.Net.*;
import System.Net.Sockets.*;

public class Client
{
    private static int dataArraySize = 100;
    private static int streamBufferSize = 1000;
    private static int numberOfLoops = 10000;

    public static void main(String[] args)
    {
        // Check that an argument was specified when the 
        // program was invoked.
        if ( args.length == 0 ) {
            Console.WriteLine(("Error: The name of the host computer" 
                + " must be specified when the program is invoked."));
            return;
        }
        String remoteName = args[0];

        // Create the underlying socket and connect to the server.
        Socket clientSocket =  new Socket(AddressFamily.InterNetwork, 
            SocketType.Stream, ProtocolType.Tcp);

        clientSocket.Connect(
            new IPEndPoint(Dns.Resolve(remoteName).get_AddressList()[0], 1800));

        Console.WriteLine("Client is connected.\n");

        // Create a NetworkStream that owns clientSocket and 
        // then create a BufferedStream on top of the NetworkStream.
        // Both streams are disposed when execution exits the
        // using statement.

        Stream netStream = new NetworkStream(clientSocket, true);
        Stream bufStream = new BufferedStream(netStream, streamBufferSize);
        try {
            try {
                // Check whether the underlying stream supports seeking.
                Console.WriteLine("NetworkStream {0} seeking.\n",
                    (bufStream.get_CanSeek())? "supports" : "does not support");
                // Send and receive data.
                if ( bufStream.get_CanWrite()  ) {
                    SendData(netStream, bufStream);
                }
                if ( bufStream.get_CanRead()  ) {
                    ReceiveData(netStream, bufStream);
                }

                // When execution exits the using statement, netStream
                // is disposed, which in turn shuts down the connection 
                // and closes clientSocket.
                Console.WriteLine("\nShutting down the connection.");
            }
            finally {
                netStream.Dispose();
            }
        }
        finally {
            bufStream.Dispose();
        }         
    } //main   
   
    static void SendData(Stream netStream, Stream bufStream) 
    {
        DateTime startTime;
        double networkTime,bufferedTime;

        // Create random data to send to the server.        
        ubyte dataToSend[] = new ubyte[dataArraySize];
        new Random().NextBytes(dataToSend);

        // Send the data using the NetworkStream.
        Console.WriteLine("Sending data using NetworkStream.");
        startTime = DateTime.get_Now();
        for(int i=0;i < numberOfLoops;i++) {
            netStream.Write(dataToSend, 0, dataToSend.length);
        }        
        networkTime =
            ((DateTime.get_Now()).Subtract(startTime)).get_TotalSeconds();
        Console.WriteLine("{0} bytes sent in {1} seconds.\n",
            System.Convert.ToString(numberOfLoops * dataToSend.length),
            ((System.Double) networkTime).ToString("F1"));

        // Send the data using the BufferedStream.
        Console.WriteLine("Sending data using BufferedStream.");
        startTime = DateTime.get_Now();
        for(int i=0;i < numberOfLoops;i++) {
            bufStream.Write(dataToSend, 0, dataToSend.length);
        }        
        bufStream.Flush();
        bufferedTime = 
            ((DateTime.get_Now()).Subtract(startTime)).get_TotalSeconds();
        Console.WriteLine("{0} bytes sent in {1} seconds.\n",
            System.Convert.ToString (numberOfLoops * dataToSend.length),
            ((System.Double)bufferedTime).ToString("F1"));

        // Print the ratio of write times.
        Console.WriteLine("Sending data using the buffered "
            + "network stream was {0} {1} than using the network "
            + "stream alone.\n",
            ((System.Double)(networkTime / bufferedTime)).ToString("P0"),
            (bufferedTime < networkTime) ? "faster" : "slower");
    } //SendData
   
    static void ReceiveData(Stream netStream, Stream bufStream) 
    {
        DateTime startTime;
        double bufferedTime = 0;
        double networkTime;
        int bytesReceived = 0;
        ubyte receivedData[] = new ubyte[dataArraySize];

        // Receive data using the NetworkStream.
        Console.WriteLine("Receiving data using NetworkStream.");
        startTime = DateTime.get_Now();
        while((bytesReceived < numberOfLoops * receivedData.length)) {
            bytesReceived += netStream.Read(receivedData,0,receivedData.length);
        }
        networkTime = 
            ((DateTime.get_Now()).Subtract(startTime)).get_TotalSeconds();
        Console.WriteLine("{0} bytes received in {1} seconds.\n",
            (new Integer(bytesReceived)).ToString(),
            ((System.Double)networkTime).ToString("F1"));

        // Receive data using the BufferedStream.
        Console.WriteLine("Receiving data using BufferedStream.");
        bytesReceived = 0;
        startTime = DateTime.get_Now();
        while((bytesReceived < numberOfLoops * receivedData.length)) {
            bytesReceived += bufStream.Read(receivedData,0,receivedData.length);
        }
        bufferedTime =
            ((DateTime.get_Now()).Subtract(startTime)).get_TotalSeconds();
        Console.WriteLine("{0} bytes received in {1} seconds.\n",
            (new Integer(bytesReceived)).ToString(),
            ((System.Double) bufferedTime).ToString("F1"));

        // Print the ratio of read times.
        Console.WriteLine("Receiving data using the buffered network"
            + " stream was {0} {1} than using the network stream alone.",
            ((System.Double)(networkTime / bufferedTime)).ToString("P0"),
            (bufferedTime < networkTime) ? "faster" : "slower");
    } //ReceiveData
} //Client

' Compile using /r:System.dll.
Imports Microsoft.VisualBasic
Imports System
Imports System.Net
Imports System.Net.Sockets

Public Class Server 

    Shared Sub Main() 
    
        ' This is a Windows Sockets 2 error code.
        Const WSAETIMEDOUT As Integer = 10060

        Dim serverSocket As Socket 
        Dim bytesReceived As Integer
        Dim totalReceived As Integer = 0
        Dim receivedData(2000000-1) As Byte

        ' Create random data to send to the client.
        Dim dataToSend(2000000-1) As Byte
        Dim randomGenerator As New Random()
        randomGenerator.NextBytes(dataToSend)

        Dim ipAddress As IPAddress = _
            Dns.Resolve(Dns.GetHostName()).AddressList(0)

        Dim ipEndpoint As New IPEndPoint(ipAddress, 1800)

        ' Create a socket and listen for incoming connections.
        Dim listenSocket As New Socket(AddressFamily.InterNetwork, _
            SocketType.Stream, ProtocolType.Tcp)
        
        Try
            listenSocket.Bind(ipEndpoint)
            listenSocket.Listen(1)

            ' Accept a connection and create a socket to handle it.
            serverSocket = listenSocket.Accept()
            Console.WriteLine("Server is connected." & vbCrLf)
        Finally
            listenSocket.Close()
        End Try

        Try
            ' Send data to the client.
            Console.Write("Sending data ... ")
            Dim bytesSent As Integer = serverSocket.Send( _
                dataToSend, 0, dataToSend.Length, SocketFlags.None)
            Console.WriteLine("{0} bytes sent." & vbCrLf, _
                bytesSent.ToString())

            ' Set the timeout for receiving data to 2 seconds.
            serverSocket.SetSocketOption(SocketOptionLevel.Socket, _
                SocketOptionName.ReceiveTimeout, 2000)

            ' Receive data from the client.
            Console.Write("Receiving data ... ")
            Try
                Do
                    bytesReceived = serverSocket.Receive( _
                        receivedData, 0, receivedData.Length, _
                        SocketFlags.None)
                    totalReceived += bytesReceived
                Loop While bytesReceived <> 0
            Catch e As SocketException
                If(e.ErrorCode = WSAETIMEDOUT)
                
                    ' Data was not received within the given time.
                    ' Assume that the transmission has ended.
                Else
                    Console.WriteLine("{0}: {1}" & vbCrLf, _
                        e.GetType().Name, e.Message)
                End If
            Finally
                Console.WriteLine("{0} bytes received." & vbCrLf, _
                    totalReceived.ToString())
            End Try
        Finally
            serverSocket.Shutdown(SocketShutdown.Both)
            Console.WriteLine("Connection shut down.")
            serverSocket.Close()
        End Try
    
    End Sub
End Class

System.Object
   System.MarshalByRefObject
     System.IO.Stream
      System.IO.BufferedStream

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft