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)

[<Sealed>]
[<ComVisibleAttribute(true)>]
type BufferedStream =  
    class 
        inherit Stream 
    end

The BufferedStream type exposes the following members.

  NameDescription
Public methodBufferedStream(Stream)Initializes a new instance of the BufferedStream class with a default buffer size of 4096 bytes.
Public methodBufferedStream(Stream, Int32)Initializes a new instance of the BufferedStream class with the specified buffer size.
Top

  NameDescription
Public propertyCanReadGets a value indicating whether the current stream supports reading. (Overrides Stream.CanRead.)
Public propertyCanSeekGets a value indicating whether the current stream supports seeking. (Overrides Stream.CanSeek.)
Public propertyCanTimeoutGets a value that determines whether the current stream can time out. (Inherited from Stream.)
Public propertyCanWriteGets a value indicating whether the current stream supports writing. (Overrides Stream.CanWrite.)
Public propertyLengthGets the stream length in bytes. (Overrides Stream.Length.)
Public propertyPositionGets the position within the current stream. (Overrides Stream.Position.)
Public propertyReadTimeoutGets or sets a value, in miliseconds, that determines how long the stream will attempt to read before timing out. (Inherited from Stream.)
Public propertyWriteTimeoutGets or sets a value, in miliseconds, that determines how long the stream will attempt to write before timing out. (Inherited from Stream.)
Top

  NameDescription
Public methodBeginReadBegins an asynchronous read operation. (Consider using ReadAsync instead; see the Remarks section.) (Overrides Stream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object).)

In .NET Framework Client Profile 4, this member is inherited from Stream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object).
Public methodBeginWriteBegins an asynchronous write operation. (Consider using WriteAsync instead; see the Remarks section.) (Overrides Stream.BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object).)

In .NET Framework Client Profile 4, this member is inherited from Stream.BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object).
Public methodCloseCloses the current stream and releases any resources (such as sockets and file handles) associated with the current stream. Instead of calling this method, ensure that the stream is properly disposed. (Inherited from Stream.)
Public methodCopyTo(Stream)Reads the bytes from the current stream and writes them to another stream. (Inherited from Stream.)
Public methodCopyTo(Stream, Int32)Reads the bytes from the current stream and writes them to another stream, using a specified buffer size. (Inherited from Stream.)
Public methodCopyToAsync(Stream)Asynchronously reads the bytes from the current stream and writes them to another stream. (Inherited from Stream.)
Public methodCopyToAsync(Stream, Int32)Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified buffer size. (Inherited from Stream.)
Public methodCopyToAsync(Stream, Int32, CancellationToken)Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified buffer size and cancellation token. (Inherited from Stream.)
Public methodCreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public methodDispose()Releases all resources used by the Stream. (Inherited from Stream.)
Public methodEndReadWaits for the pending asynchronous read operation to complete. (Consider using ReadAsync instead; see the Remarks section.) (Overrides Stream.EndRead(IAsyncResult).)

In .NET Framework Client Profile 4, this member is inherited from Stream.EndRead(IAsyncResult).
Public methodEndWriteEnds an asynchronous write operation and blocks until the I/O operation is complete. (Consider using WriteAsync instead; see the Remarks section.) (Overrides Stream.EndWrite(IAsyncResult).)

In .NET Framework Client Profile 4, this member is inherited from Stream.EndWrite(IAsyncResult).
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodFlushClears all buffers for this stream and causes any buffered data to be written to the underlying device. (Overrides Stream.Flush().)
Public methodFlushAsync()Asynchronously clears all buffers for this stream and causes any buffered data to be written to the underlying device. (Inherited from Stream.)
Public methodFlushAsync(CancellationToken)Asynchronously clears all buffers for this stream, causes any buffered data to be written to the underlying device, and monitors cancellation requests. (Overrides Stream.FlushAsync(CancellationToken).)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Public methodReadCopies bytes from the current buffered stream to an array. (Overrides Stream.Read(Byte[], Int32, Int32).)
Public methodReadAsync(Byte[], Int32, Int32)Asynchronously reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read. (Inherited from Stream.)
Public methodReadAsync(Byte[], Int32, Int32, CancellationToken)Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests. (Overrides Stream.ReadAsync(Byte[], Int32, Int32, CancellationToken).)
Public methodReadByteReads a byte from the underlying stream and returns the byte cast to an int, or returns -1 if reading from the end of the stream. (Overrides Stream.ReadByte().)
Public methodSeekSets the position within the current buffered stream. (Overrides Stream.Seek(Int64, SeekOrigin).)
Public methodSetLengthSets the length of the buffered stream. (Overrides Stream.SetLength(Int64).)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodWriteCopies bytes to the buffered stream and advances the current position within the buffered stream by the number of bytes written. (Overrides Stream.Write(Byte[], Int32, Int32).)
Public methodWriteAsync(Byte[], Int32, Int32)Asynchronously writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written. (Inherited from Stream.)
Public methodWriteAsync(Byte[], Int32, Int32, CancellationToken)Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests. (Overrides Stream.WriteAsync(Byte[], Int32, Int32, CancellationToken).)
Public methodWriteByteWrites a byte to the current position in the buffered stream. (Overrides Stream.WriteByte(Byte).)
Top

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.

Important noteImportant

This type implements the IDisposable interface. When you have finished using the type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its Dispose method in a try/catch block. To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). For more information, see the “Using an Object that Implements IDisposable” section in the IDisposable interface topic.

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 examples show 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.

The first example shows the code that runs on the client, and the second example shows the code that runs on the server.

Example 1: Code that runs on the client

No code example is currently available or this language may not be supported.

Example 2: Code that runs on the server

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

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