BufferedStream Class

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

Syntax

Declaration

[ComVisible(true)]
public sealed class BufferedStream : Stream, IDisposable

Inheritance Hierarchy

Constructors summary

Initializes a new instance of the BufferedStream class with a default buffer size of 4096 bytes.

Initializes a new instance of the BufferedStream class with the specified buffer size.

Properties summary

Gets a value indicating whether the current stream supports reading.

Gets a value indicating whether the current stream supports seeking.

Gets a value indicating whether the current stream supports writing.

Gets the stream length in bytes.

Gets the position within the current stream.

Methods summary

Clears all buffers for this stream and causes any buffered data to be written to the underlying device.

Asynchronously clears all buffers for this stream, causes any buffered data to be written to the underlying device, and monitors cancellation requests.

Copies bytes from the current buffered stream to an array.

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.

Reads 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.

Sets the position within the current buffered stream.

Sets the length of the buffered stream.

Copies bytes to the buffered stream and advances the current position within the buffered stream by the number of bytes written.

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.

Writes a byte to the current position in the buffered stream.

Constructors

  • BufferedStream(Stream)

    Initializes a new instance of the BufferedStream class with a default buffer size of 4096 bytes.

    public BufferedStream(Stream stream)

    Parameters

    • stream

      The current stream.

    Exceptions

  • BufferedStream(Stream, Int32)

    Initializes a new instance of the BufferedStream class with the specified buffer size.

    public BufferedStream(Stream stream, int bufferSize)

    Parameters

    • stream

      The current stream.

    • bufferSize

      The buffer size in bytes.

    Exceptions

Properties

  • CanRead

    Gets a value indicating whether the current stream supports reading.

    public override bool CanRead { get; }

    Property Value

    • true if the stream supports reading; false if the stream is closed or was opened with write-only access.

    Overrides

  • CanSeek

    Gets a value indicating whether the current stream supports seeking.

    public override bool CanSeek { get; }

    Property Value

    • true if the stream supports seeking; false if the stream is closed or if the stream was constructed from an operating system handle such as a pipe or output to the console.

    Overrides

  • CanWrite

    Gets a value indicating whether the current stream supports writing.

    public override bool CanWrite { get; }

    Property Value

    • true if the stream supports writing; false if the stream is closed or was opened with read-only access.

    Overrides

  • Length

    Gets the stream length in bytes.

    public override long Length { get; }

    Property Value

    • The stream length in bytes.

    Exceptions

    Overrides

  • Position

    Gets the position within the current stream.

    public override long Position { get; set; }

    Property Value

    • The position within the current stream.

    Exceptions

    Overrides

Methods

  • Dispose(Boolean)
    protected override void Dispose(bool disposing)

    Parameters

    Overrides

  • Flush()

    Clears all buffers for this stream and causes any buffered data to be written to the underlying device.

    public override void Flush()

    Exceptions

    Overrides

  • FlushAsync(CancellationToken)

    Asynchronously clears all buffers for this stream, causes any buffered data to be written to the underlying device, and monitors cancellation requests.

    public override Task FlushAsync(CancellationToken cancellationToken)

    Parameters

    • cancellationToken

      The token to monitor for cancellation requests.

    Returns

    • A task that represents the asynchronous flush operation.

    Exceptions

    Overrides

  • Read(Byte[], Int32, Int32)

    Copies bytes from the current buffered stream to an array.

    public override int Read(byte[] array, int offset, int count)

    Parameters

    • array

      The buffer to which bytes are to be copied.

    • offset

      The byte offset in the buffer at which to begin reading bytes.

    • count

      The number of bytes to be read.

    Returns

    • The total number of bytes read into array. This can be less than the number of bytes requested if that many bytes are not currently available, or 0 if the end of the stream has been reached before any data can be read.

    Exceptions

    Overrides

  • ReadAsync(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.

    public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)

    Parameters

    • buffer

      The buffer to write the data into.

    • offset

      The byte offset in buffer at which to begin writing data from the stream.

    • count

      The maximum number of bytes to read.

    • cancellationToken

      The token to monitor for cancellation requests.

    Returns

    • A task that represents the asynchronous read operation. The value of the TResult parameter contains the total number of bytes read into the buffer. The result value can be less than the number of bytes requested if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if the end of the stream has been reached.

    Exceptions

    Overrides

  • ReadByte()

    Reads 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.

    public override int ReadByte()

    Returns

    • The byte cast to an int, or -1 if reading from the end of the stream.

    Exceptions

    Overrides

  • Seek(Int64, SeekOrigin)

    Sets the position within the current buffered stream.

    public override long Seek(long offset, SeekOrigin origin)

    Parameters

    • offset

      A byte offset relative to origin.

    • origin

      A value of type SeekOrigin indicating the reference point from which to obtain the new position.

    Returns

    • The new position within the current buffered stream.

    Exceptions

    Overrides

  • SetLength(Int64)

    Sets the length of the buffered stream.

    public override void SetLength(long value)

    Parameters

    • value

      An integer indicating the desired length of the current buffered stream in bytes.

    Exceptions

    Overrides

  • Write(Byte[], Int32, Int32)

    Copies bytes to the buffered stream and advances the current position within the buffered stream by the number of bytes written.

    public override void Write(byte[] array, int offset, int count)

    Parameters

    • array

      The byte array from which to copy count bytes to the current buffered stream.

    • offset

      The offset in the buffer at which to begin copying bytes to the current buffered stream.

    • count

      The number of bytes to be written to the current buffered stream.

    Exceptions

    Overrides

  • WriteAsync(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.

    public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)

    Parameters

    • buffer

      The buffer to write data from.

    • offset

      The zero-based byte offset in buffer from which to begin copying bytes to the stream.

    • count

      The maximum number of bytes to write.

    • cancellationToken

      The token to monitor for cancellation requests.

    Returns

    • A task that represents the asynchronous write operation.

    Exceptions

    Overrides

  • WriteByte(Byte)

    Writes a byte to the current position in the buffered stream.

    public override void WriteByte(byte value)

    Parameters

    • value

      A byte to write to the stream.

    Exceptions

    Overrides

Details

Namespace

System.IO

Assembly

System.IO.dll

See Also