Show / Hide Table of Contents

    Class CustomBufferedStream

    A custom network stream inherited from stream with an underlying read buffer supporting both read/write of UTF-8 encoded string or raw bytes asynchronously from last read position.

    Inheritance
    Object
    MarshalByRefObject
    Stream
    CustomBufferedStream
    Implements
    IDisposable
    ICustomStreamReader
    Inherited Members
    Stream.Null
    Stream.CopyToAsync(Stream)
    Stream.CopyToAsync(Stream, Int32)
    Stream.CopyTo(Stream)
    Stream.CopyTo(Stream, Int32)
    Stream.Close()
    Stream.Dispose()
    Stream.FlushAsync()
    Stream.CreateWaitHandle()
    Stream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object)
    Stream.EndRead(IAsyncResult)
    Stream.ReadAsync(Byte[], Int32, Int32)
    Stream.BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object)
    Stream.EndWrite(IAsyncResult)
    Stream.WriteAsync(Byte[], Int32, Int32)
    Stream.Synchronized(Stream)
    Stream.ObjectInvariant()
    MarshalByRefObject.MemberwiseClone(Boolean)
    MarshalByRefObject.GetLifetimeService()
    MarshalByRefObject.InitializeLifetimeService()
    MarshalByRefObject.CreateObjRef(Type)
    Object.ToString()
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.ReferenceEquals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Namespace: StreamExtended.Network
    Assembly: StreamExtended.dll
    Syntax
    public class CustomBufferedStream : Stream, IDisposable, ICustomStreamReader

    Constructors

    CustomBufferedStream(Stream, IBufferPool, Int32, Boolean)

    Initializes a new instance of the CustomBufferedStream class.

    Declaration
    public CustomBufferedStream(Stream baseStream, IBufferPool bufferPool, int bufferSize, bool leaveOpen = false)
    Parameters
    Type Name Description
    Stream baseStream

    The base stream.

    IBufferPool bufferPool

    Bufferpool.

    Int32 bufferSize

    Size of the buffer.

    Boolean leaveOpen

    true to leave the stream open after disposing the CustomBufferedStream object; otherwise, false.

    Properties

    Available

    Gets the available data size.

    Declaration
    public int Available { get; }
    Property Value
    Type Description
    Int32

    BufferSize

    Declaration
    public int BufferSize { get; }
    Property Value
    Type Description
    Int32

    CanRead

    When overridden in a derived class, gets a value indicating whether the current stream supports reading.

    Declaration
    public override bool CanRead { get; }
    Property Value
    Type Description
    Boolean
    Overrides
    Stream.CanRead

    CanSeek

    When overridden in a derived class, gets a value indicating whether the current stream supports seeking.

    Declaration
    public override bool CanSeek { get; }
    Property Value
    Type Description
    Boolean
    Overrides
    Stream.CanSeek

    CanTimeout

    Gets a value that determines whether the current stream can time out.

    Declaration
    public override bool CanTimeout { get; }
    Property Value
    Type Description
    Boolean
    Overrides
    Stream.CanTimeout

    CanWrite

    When overridden in a derived class, gets a value indicating whether the current stream supports writing.

    Declaration
    public override bool CanWrite { get; }
    Property Value
    Type Description
    Boolean
    Overrides
    Stream.CanWrite

    DataAvailable

    Gets a value indicating whether data is available.

    Declaration
    public bool DataAvailable { get; }
    Property Value
    Type Description
    Boolean

    IsClosed

    Declaration
    public bool IsClosed { get; }
    Property Value
    Type Description
    Boolean

    Length

    When overridden in a derived class, gets the length in bytes of the stream.

    Declaration
    public override long Length { get; }
    Property Value
    Type Description
    Int64
    Overrides
    Stream.Length

    Position

    When overridden in a derived class, gets or sets the position within the current stream.

    Declaration
    public override long Position { get; set; }
    Property Value
    Type Description
    Int64
    Overrides
    Stream.Position

    ReadTimeout

    Gets or sets a value, in miliseconds, that determines how long the stream will attempt to read before timing out.

    Declaration
    public override int ReadTimeout { get; set; }
    Property Value
    Type Description
    Int32
    Overrides
    Stream.ReadTimeout

    WriteTimeout

    Gets or sets a value, in miliseconds, that determines how long the stream will attempt to write before timing out.

    Declaration
    public override int WriteTimeout { get; set; }
    Property Value
    Type Description
    Int32
    Overrides
    Stream.WriteTimeout

    Methods

    CopyToAsync(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.

    Declaration
    public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken = default(CancellationToken))
    Parameters
    Type Name Description
    Stream destination

    The stream to which the contents of the current stream will be copied.

    Int32 bufferSize

    The size, in bytes, of the buffer. This value must be greater than zero. The default size is 81920.

    CancellationToken cancellationToken

    The token to monitor for cancellation requests. The default value is None.

    Returns
    Type Description
    Task

    A task that represents the asynchronous copy operation.

    Overrides
    Stream.CopyToAsync(Stream, Int32, CancellationToken)

    Dispose(Boolean)

    Releases the unmanaged resources used by the Stream and optionally releases the managed resources.

    Declaration
    protected override void Dispose(bool disposing)
    Parameters
    Type Name Description
    Boolean disposing

    true to release both managed and unmanaged resources; false to release only unmanaged resources.

    Overrides
    Stream.Dispose(Boolean)

    FillBuffer()

    Fills the buffer.

    Declaration
    public bool FillBuffer()
    Returns
    Type Description
    Boolean

    FillBufferAsync(CancellationToken)

    Fills the buffer asynchronous.

    Declaration
    public Task<bool> FillBufferAsync(CancellationToken cancellationToken = default(CancellationToken))
    Parameters
    Type Name Description
    CancellationToken cancellationToken

    The cancellation token.

    Returns
    Type Description
    Task<Boolean>

    Flush()

    When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.

    Declaration
    public override void Flush()
    Overrides
    Stream.Flush()

    FlushAsync(CancellationToken)

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

    Declaration
    public override Task FlushAsync(CancellationToken cancellationToken = default(CancellationToken))
    Parameters
    Type Name Description
    CancellationToken cancellationToken

    The token to monitor for cancellation requests. The default value is None.

    Returns
    Type Description
    Task

    A task that represents the asynchronous flush operation.

    Overrides
    Stream.FlushAsync(CancellationToken)

    OnDataRead(Byte[], Int32, Int32)

    Declaration
    protected virtual void OnDataRead(byte[] buffer, int offset, int count)
    Parameters
    Type Name Description
    Byte[] buffer
    Int32 offset
    Int32 count

    OnDataWrite(Byte[], Int32, Int32)

    Declaration
    protected virtual void OnDataWrite(byte[] buffer, int offset, int count)
    Parameters
    Type Name Description
    Byte[] buffer
    Int32 offset
    Int32 count

    PeekByteAsync(Int32, CancellationToken)

    Peeks a byte asynchronous.

    Declaration
    public Task<int> PeekByteAsync(int index, CancellationToken cancellationToken = default(CancellationToken))
    Parameters
    Type Name Description
    Int32 index

    The index.

    CancellationToken cancellationToken

    The cancellation token.

    Returns
    Type Description
    Task<Int32>

    PeekByteFromBuffer(Int32)

    Peeks a byte from buffer.

    Declaration
    public byte PeekByteFromBuffer(int index)
    Parameters
    Type Name Description
    Int32 index

    The index.

    Returns
    Type Description
    Byte
    Exceptions
    Type Condition
    Exception

    Index is out of buffer size

    PeekBytesAsync(Int32, Int32, CancellationToken)

    Peeks bytes asynchronous.

    Declaration
    public Task<byte[]> PeekBytesAsync(int index, int size, CancellationToken cancellationToken = default(CancellationToken))
    Parameters
    Type Name Description
    Int32 index

    The index.

    Int32 size
    CancellationToken cancellationToken

    The cancellation token.

    Returns
    Type Description
    Task<Byte[]>

    Read(Byte[], Int32, Int32)

    When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

    Declaration
    public override int Read(byte[] buffer, int offset, int count)
    Parameters
    Type Name Description
    Byte[] buffer

    An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.

    Int32 offset

    The zero-based byte offset in buffer at which to begin storing the data read from the current stream.

    Int32 count

    The maximum number of bytes to be read from the current stream.

    Returns
    Type Description
    Int32

    The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.

    Overrides
    Stream.Read(Byte[], Int32, Int32)

    ReadAndIgnoreAllLinesAsync(CancellationToken)

    Read until the last new line, ignores the result

    Declaration
    public Task ReadAndIgnoreAllLinesAsync(CancellationToken cancellationToken = default(CancellationToken))
    Parameters
    Type Name Description
    CancellationToken cancellationToken
    Returns
    Type Description
    Task

    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.

    Declaration
    public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken = default(CancellationToken))
    Parameters
    Type Name Description
    Byte[] buffer

    The buffer to write the data into.

    Int32 offset

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

    Int32 count

    The maximum number of bytes to read.

    CancellationToken cancellationToken

    The token to monitor for cancellation requests. The default value is None.

    Returns
    Type Description
    Task<Int32>

    A task that represents the asynchronous read operation. The value of the 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.

    Overrides
    Stream.ReadAsync(Byte[], Int32, Int32, CancellationToken)

    ReadByte()

    Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.

    Declaration
    public override int ReadByte()
    Returns
    Type Description
    Int32

    The unsigned byte cast to an Int32, or -1 if at the end of the stream.

    Overrides
    Stream.ReadByte()

    ReadByteFromBuffer()

    Reads a byte from buffer.

    Declaration
    public byte ReadByteFromBuffer()
    Returns
    Type Description
    Byte
    Exceptions
    Type Condition
    Exception

    Buffer is empty

    ReadLineAsync(CancellationToken)

    Read a line from the byte stream

    Declaration
    public Task<string> ReadLineAsync(CancellationToken cancellationToken = default(CancellationToken))
    Parameters
    Type Name Description
    CancellationToken cancellationToken
    Returns
    Type Description
    Task<String>

    Seek(Int64, SeekOrigin)

    When overridden in a derived class, sets the position within the current stream.

    Declaration
    public override long Seek(long offset, SeekOrigin origin)
    Parameters
    Type Name Description
    Int64 offset

    A byte offset relative to the origin parameter.

    SeekOrigin origin

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

    Returns
    Type Description
    Int64

    The new position within the current stream.

    Overrides
    Stream.Seek(Int64, SeekOrigin)

    SetLength(Int64)

    When overridden in a derived class, sets the length of the current stream.

    Declaration
    public override void SetLength(long value)
    Parameters
    Type Name Description
    Int64 value

    The desired length of the current stream in bytes.

    Overrides
    Stream.SetLength(Int64)

    Write(Byte[], Int32, Int32)

    When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

    Declaration
    public override void Write(byte[] buffer, int offset, int count)
    Parameters
    Type Name Description
    Byte[] buffer

    An array of bytes. This method copies count bytes from buffer to the current stream.

    Int32 offset

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

    Int32 count

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

    Overrides
    Stream.Write(Byte[], Int32, Int32)

    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.

    Declaration
    public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken = default(CancellationToken))
    Parameters
    Type Name Description
    Byte[] buffer

    The buffer to write data from.

    Int32 offset

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

    Int32 count

    The maximum number of bytes to write.

    CancellationToken cancellationToken

    The token to monitor for cancellation requests. The default value is None.

    Returns
    Type Description
    Task

    A task that represents the asynchronous write operation.

    Overrides
    Stream.WriteAsync(Byte[], Int32, Int32, CancellationToken)

    WriteByte(Byte)

    Writes a byte to the current position in the stream and advances the position within the stream by one byte.

    Declaration
    public override void WriteByte(byte value)
    Parameters
    Type Name Description
    Byte value

    The byte to write to the stream.

    Overrides
    Stream.WriteByte(Byte)

    Events

    DataRead

    Declaration
    public event EventHandler<DataEventArgs> DataRead
    Event Type
    Type Description
    EventHandler<DataEventArgs>

    DataWrite

    Declaration
    public event EventHandler<DataEventArgs> DataWrite
    Event Type
    Type Description
    EventHandler<DataEventArgs>

    Implements

    System.IDisposable
    ICustomStreamReader

    See Also

    Stream
    Back to top Generated by DocFX