Class VersatileBufferStream
Provides a stream abstraction over one or more VersatileBuffer instances.
public class VersatileBufferStream : BitStream, IDisposable, IAsyncDisposable
- Inheritance
-
VersatileBufferStream
- Implements
- Inherited Members
Remarks
This class allows reading and writing data across multiple underlying buffers as if they were a single contiguous stream. It supports bit-level operations through the BitStream base class.
Constructors
VersatileBufferStream()
Creates instance of an empty stream
public VersatileBufferStream()
VersatileBufferStream(VersatileBuffer)
Creates the stream using the specified packet buffer
public VersatileBufferStream(VersatileBuffer packet)
Parameters
packetVersatileBufferPacket buffer containing the real data
VersatileBufferStream(VersatileBuffer, int, int)
Creates the stream using the specified packet buffer, offset and count bytes
public VersatileBufferStream(VersatileBuffer packet, int offset, int count)
Parameters
packetVersatileBufferPacket buffer containing the real data
offsetintOffset of the data in the buffer
countintNumber of bytes to use
Properties
AutoGrowth
Gets or sets a value indicating whether the stream automatically grows when data being written exceeds the remaining space.
public bool AutoGrowth { get; set; }
Property Value
BitPosition
Gets or sets the current bit position within the current byte (0-7).
public override int BitPosition { get; set; }
Property Value
Exceptions
- ArgumentOutOfRangeException
Thrown when the value is less than 0 or greater than or equal to 8.
CanRead
Gets a value indicating whether the stream supports reading. Always returns true.
public override bool CanRead { get; }
Property Value
CanSeek
Gets a value indicating whether the stream supports seeking. Always returns true.
public override bool CanSeek { get; }
Property Value
CanWrite
Gets a value indicating whether the stream supports writing. Always returns true.
public override bool CanWrite { get; }
Property Value
Length
Gets the total length of the stream in bytes.
public override long Length { get; }
Property Value
Position
Gets or sets stream position
public override long Position { get; set; }
Property Value
Methods
Append(VersatileBuffer, int, int)
Append specified packet buffer to current stream. This function doesn't do any actual memory copy. Instead it just adds new buffer to the underlying buffer list.
public void Append(VersatileBuffer buffer, int offset, int count)
Parameters
bufferVersatileBufferPacket buffer to append
offsetintOffset in the packet buffer
countintNumber of bytes to use from the packet buffer
CopyTo(Stream, int)
Copies the specified number of bytes from the current position to the target stream.
public int CopyTo(Stream stream, int count)
Parameters
Returns
- int
The number of bytes actually copied.
CopyTo(Stream, long, int, bool)
Copies specified number of bytes from current position to specified stream.
public int CopyTo(Stream stream, long fromPosition, int count, bool setFinalPosition)
Parameters
streamStreamTarget stream receiving the data
fromPositionlongStart copying from
countintNumber of bytes to copy
setFinalPositionboolWhether stream position should be set to a final position after the reading finished
Returns
- int
Number of bytes actually copied
Flush()
Flush the stream (not applicable for current class)
public override void Flush()
MoveTo(VersatileBufferStream)
Copies data from the beginning of the stream till current position to the output stream and then resets current stream position to 0, all under the lock. Necessary to serve as endless input buffer, eg for endless HTTP request.
public int MoveTo(VersatileBufferStream stream)
Parameters
streamVersatileBufferStreamTarget stream receiving the data
Returns
- int
Number of bytes actually copied
PeekByte()
Peek one byte from the stream
public override int PeekByte()
Returns
- int
Byte read at current stream position
Read(byte[], int, int)
Read specified number of bytes to a byte buffer
public override int Read(byte[] buffer, int offset, int count)
Parameters
bufferbyte[]Byte buffer to hold read data
offsetintOffset of the read buffer
countintNumber of bytes to read
Returns
- int
Number of bytes actually read
Read(Span<byte>)
Read from current position to a specified buffer
public override int Read(Span<byte> buffer)
Parameters
Returns
- int
Number of bytes actually read
ReadByte()
Read one byte from the stream
public override int ReadByte()
Returns
- int
Byte read at current stream position
ReadFrom(long, byte[], int, int)
Reads data from the specified position into a byte array without changing the current stream position.
public int ReadFrom(long fromPosition, byte[] buffer, int offset, int count)
Parameters
fromPositionlongThe position in the stream to read from.
bufferbyte[]The byte array to store the read data.
offsetintThe offset in the buffer to start writing data.
countintThe number of bytes to read.
Returns
- int
The number of bytes actually read.
Exceptions
- ArgumentNullException
Thrown when
bufferisnull.
Seek(long, SeekOrigin)
Seek to a specified position
public override long Seek(long offset, SeekOrigin origin)
Parameters
offsetlongTarget offset
originSeekOriginTarget origin
Returns
- long
Position after applying the specified offset
SeekBits(long, SeekOrigin)
Seeks to a specified bit position within the stream.
public override long SeekBits(long offset, SeekOrigin origin)
Parameters
offsetlongThe bit offset relative to the origin.
originSeekOriginThe reference point for the offset.
Returns
- long
The new byte position after seeking.
Exceptions
- ArgumentOutOfRangeException
Thrown when the resulting position is outside the stream bounds.
SetLength(long)
Sets the length of the stream by truncating data beyond the specified value.
public override void SetLength(long value)
Parameters
valuelongThe new length of the stream. Must be less than or equal to the current length.
Exceptions
- ArgumentOutOfRangeException
Thrown when
valueis greater than the current length.
SliceTo(VersatileBufferStream)
Copies all data from current stream to specified stream without actual memory copy, only by referencing the buffers.
public int SliceTo(VersatileBufferStream stream)
Parameters
streamVersatileBufferStreamTarget stream receiving the data
Returns
- int
Number of bytes actually copied
SliceTo(VersatileBufferStream, long, int)
Copies data range from current stream to specified stream without actual memory copy, only by referencing the buffers.
public int SliceTo(VersatileBufferStream stream, long fromPosition, int count)
Parameters
streamVersatileBufferStreamTarget stream receiving the data
fromPositionlongStart copying from
countintNumber of bytes to copy
Returns
- int
Number of bytes actually copied
TrimBegin()
Drops all data from the beginning of the stream up to the current position.
public void TrimBegin()
TrimBegin(long)
Drop all data from the beginning of the stream till current position
public void TrimBegin(long trimPosition)
Parameters
trimPositionlong
UnlockedCopyTo(Stream, long, int)
Copies specified number of bytes from current position to specified output stream. Operation doesn't affect any local variables or properties and must be performed on finalized stream which will not be changed for sure during this operation.
public int UnlockedCopyTo(Stream stream, long fromPosition, int count)
Parameters
streamStreamTarget stream receiving the data
fromPositionlongPosition to copy from
countintNumber of bytes to copy
Returns
- int
Number of bytes actually copied
Write(byte[], int, int)
Write specified number of bytes from the byte buffer to the stream
public override void Write(byte[] buffer, int offset, int count)