Class ContinuousMemoryBuffer<T>
Provides a resizable memory buffer analogous to MemoryBuffer<T>, using a single continuous memory region instead.
Inheritance
Implements
Namespace: DisCatSharp.Common.Types
Assembly: DisCatSharp.Common.dll
Syntax
public sealed class ContinuousMemoryBuffer<T> : IMemoryBuffer<T>, IDisposable where T : struct
Type Parameters
Name | Description |
---|---|
T | Type of item to hold in the buffer. |
Constructors
| Improve this Doc View SourceContinuousMemoryBuffer(Int32, MemoryPool<Byte>, Boolean)
Creates a new buffer with a specified segment size, specified number of initially-allocated segments, and supplied memory pool.
Declaration
public ContinuousMemoryBuffer(int initialSize = 65536, MemoryPool<byte> memPool = null, bool clearOnDispose = false)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | initialSize | Initial size of the buffer in bytes. Defaults to 64KiB. |
System.Buffers.MemoryPool<System.Byte> | memPool | Memory pool to use for renting buffers. Defaults to System.Buffers.MemoryPool`1.Shared. |
System.Boolean | clearOnDispose | Determines whether the underlying buffers should be cleared on exit. If dealing with sensitive data, it might be a good idea to set this option to true. |
Properties
| Improve this Doc View SourceCapacity
Gets the total capacity of this buffer. The capacity is the number of segments allocated, multiplied by size of individual segment.
Declaration
public ulong Capacity { get; }
Property Value
Type | Description |
---|---|
System.UInt64 |
Count
Gets the number of items currently written to the buffer. This number is equal to Count divided by size of T
.
Declaration
public ulong Count { get; }
Property Value
Type | Description |
---|---|
System.UInt64 |
Length
Gets the amount of bytes currently written to the buffer. This number is never greater than Capacity.
Declaration
public ulong Length { get; }
Property Value
Type | Description |
---|---|
System.UInt64 |
Methods
| Improve this Doc View SourceClear()
Resets the buffer's pointer to the beginning, allowing for reuse.
Declaration
public void Clear()
CopyTo(Stream)
Copies all the data from this buffer to a stream.
Declaration
public void CopyTo(Stream destination)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | destination | Stream to copy this buffer's data to. |
Dispose()
Disposes of any resources claimed by this buffer.
Declaration
public void Dispose()
Read(T[], Int32, Int32, UInt64, out Int32)
Reads data from this buffer to specified destination array. This method will write either as many bytes as specified for the destination array, or however many bytes are available in this buffer, whichever is less.
Declaration
public bool Read(T[] data, int start, int count, ulong source, out int itemsWritten)
Parameters
Type | Name | Description |
---|---|---|
T[] | data | Array to read the data from this buffer into. |
System.Int32 | start | Starting position in the target array to write to. |
System.Int32 | count | Maximum number of bytes to write to target array. |
System.UInt64 | source | Starting position in this buffer to read from. |
System.Int32 | itemsWritten | Number of items written to the destination buffer. |
Returns
Type | Description |
---|---|
System.Boolean | Whether more data is available in this buffer. |
Read(ArraySegment<T>, UInt64, out Int32)
Reads data from this buffer to specified destination array slice. This method will write either as many bytes as specified in the target slice, or however many bytes are available in this buffer, whichever is less.
Declaration
public bool Read(ArraySegment<T> data, ulong source, out int itemsWritten)
Parameters
Type | Name | Description |
---|---|---|
System.ArraySegment<T> | data | |
System.UInt64 | source | |
System.Int32 | itemsWritten | Number of items written to the destination buffer. |
Returns
Type | Description |
---|---|
System.Boolean | Whether more data is available in this buffer. |
Read(Span<T>, UInt64, out Int32)
Reads data from this buffer to the specified destination buffer. This method will write either as many bytes as there are in the destination buffer, or however many bytes are available in this buffer, whichever is less.
Declaration
public bool Read(Span<T> destination, ulong source, out int itemsWritten)
Parameters
Type | Name | Description |
---|---|---|
System.Span<T> | destination | Buffer to read the data from this buffer into. |
System.UInt64 | source | Starting position in this buffer to read from. |
System.Int32 | itemsWritten | Number of items written to the destination buffer. |
Returns
Type | Description |
---|---|
System.Boolean | Whether more data is available in this buffer. |
ToArray()
Converts this buffer into a single continuous byte array.
Declaration
public T[] ToArray()
Returns
Type | Description |
---|---|
T[] | Converted byte array. |
Write(T[], Int32, Int32)
Appends data from a supplied array to this buffer, growing it if necessary.
Declaration
public void Write(T[] data, int start, int count)
Parameters
Type | Name | Description |
---|---|---|
T[] | data | Array containing data to write. |
System.Int32 | start | Index from which to start reading the data. |
System.Int32 | count | Number of bytes to read from the source. |
Write(ArraySegment<T>)
Appends data from a supplied array slice to this buffer, growing it if necessary.
Declaration
public void Write(ArraySegment<T> data)
Parameters
Type | Name | Description |
---|---|---|
System.ArraySegment<T> | data | Array slice containing data to write. |
Write(Stream)
Appends data from a supplied stream to this buffer, growing it if necessary.
Declaration
public void Write(Stream stream)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | stream | Stream to copy data from. |
Write(ReadOnlySpan<T>)
Appends data from a supplied buffer to this buffer, growing it if necessary.
Declaration
public void Write(ReadOnlySpan<T> data)
Parameters
Type | Name | Description |
---|---|---|
System.ReadOnlySpan<T> | data | Buffer containing data to write. |