SliceExtensions

Namespace: System · class

Helper methods for Slice

Methods

AsPipeReader

static PipeReader AsPipeReader(Slice slice)

Returns a PipeReader that will consume the content of this slice

AsSlice

static Slice AsSlice(byte[] bytes)

Returns a slice that wraps the whole array

static Slice AsSlice(ArraySegment<byte> self)

Returns a slice that is the wraps the same memory region as this ArraySegment

static Slice AsSlice(byte[] bytes, int offset)

Returns the tail of the array, starting from the specified offset

  • bytes — Underlying buffer to slice
  • offset — Offset to the first byte of the slice

REMINDER: the parameter is the offset, and not the length !

static Slice AsSlice(byte[] bytes, Range range)

Returns a slice from the subsection of the byte array

  • bytes — Underlying buffer to slice
  • range — Range of the array to return

Returns: Slice that maps the corresponding subsection of the array. If range is empty, then either Empty or Nil will be returned, in order to not keep a reference to the whole buffer.

static Slice AsSlice(ArraySegment<byte> self, int offset)

Returns a slice that is the wraps the same memory region as this ArraySegment

static Slice AsSlice(byte[] bytes, int offset, int count)

Returns a slice from the subsection of the byte array

  • bytes — Underlying buffer to slice
  • offset — Offset to the first element of the slice (if not empty)
  • count — Number of bytes to take

Returns: Slice that maps the corresponding subsection of the array. If count is 0 then either Empty or Nil will be returned, in order to not keep a reference to the whole buffer.

static Slice AsSlice(byte[] bytes, uint offset, uint count)

Returns a slice from the subsection of the byte array

  • bytes — Underlying buffer to slice
  • offset — Offset to the first element of the slice (if not empty)
  • count — Number of bytes to take

Returns: Slice that maps the corresponding subsection of the array. If count is 0, then either Empty or Nil will be returned, in order to not keep a reference to the whole buffer.

static Slice AsSlice(ArraySegment<byte> self, int offset, int count)

Returns a slice that is the wraps the same memory region as this ArraySegment

CopyToAsync

static Task CopyToAsync(Slice slice, PipeWriter output, CancellationToken ct)

Copies the content of a Slice into a PipeWriter.

ReadAllSlice

static Slice ReadAllSlice(Stream input)

Reads the entire content of a Stream into a Slice in memory.

ReadAllSliceAsync

static Task<Slice> ReadAllSliceAsync(Stream input, CancellationToken ct)

Reads the entire content of a Stream into a Slice in memory.

static Task<Slice> ReadAllSliceAsync(PipeReader input, CancellationToken ct)

Reads the entire content of a PipeReader into a Slice> in memory.

ReadSliceExactly

static Slice ReadSliceExactly(Stream input, int count)

Reads exactly count bytes from the specified stream.

  • input — Input stream
  • count — Number of bytes to read from the current position in the input

Returns: Slice that contains exactly count bytes.

This method implements the classical read loop, to handle for situations where a read for N bytes from the stream returns less than requested (ex: sockets, pipes, ...)

ReadSliceExactlyAsync

static Task<Slice> ReadSliceExactlyAsync(Stream input, int count, CancellationToken ct)

Reads exactly count bytes from the specified stream.

  • input — Input stream
  • count — Number of bytes to read from the current position in the input
  • ct — Token used to cancel the read operation

Returns: Slice that contains exactly count bytes.

This method implements the classical read loop, to handle for situations where a read for N bytes from the stream returns less than requested (ex: sockets, pipes, ...)

ToSlice

static Slice ToSlice(ReadOnlySpan<byte> source)

Creates a new slice by copying the contents of this span of bytes

  • source — Span of bytes to copy

Returns: Slice that points to a copy of the bytes in source

Returns the singleton if is empty

Span<byte> source = [ 0x12, 0x34 ];
// create a copy in memory
var slice = source.ToSlice();
Debug.Assert(slice[0] == source[0]);
// changing the source does not affect the copy
source[0] = 0x56;
Debug.Assert(slice[0] != source[0]);

static Slice ToSlice(Span<byte> source)

Creates a new slice by copying the contents of this span of bytes

  • source — Span of bytes to copy

Returns: Slice that points to a copy of the bytes in source

Returns the singleton if is empty

Span<byte> source = [ 0x12, 0x34 ];
// create a copy in memory
var slice = source.ToSlice();
Debug.Assert(slice[0] == source[0]);
// changing the source does not affect the copy
source[0] = 0x56;
Debug.Assert(slice[0] != source[0]);

static Slice ToSlice(MemoryStream stream)

Exposes the content of a MemoryStream as a Slice if possible, or returns a copy

  • stream — Stream with some content

Returns: Slice that uses the stream internal buffer, if it is publicly visible; otherwise, a copy of the stream's content.

ToSliceOwner

static SliceOwner ToSliceOwner(ReadOnlySpan<byte> source, ArrayPool<byte> pool)

Creates a new SliceOwner that wraps a copy the contents of this span of bytes, using a provided ArrayPool

  • source — Span of bytes to copy
  • pool — Pool used to allocate the backing array

Returns: SliceOwner that points to a copy of the bytes in source, using an array rented from the pool as its backing store.

The return value must be disposed for the buffer to return to the pool.

// write some arbitrary data to a buffer
ReadOnlySpan<byte> data = /*....*/;
// copy the formatted data to a rented buffer
using(var buffer = data.ToSlice(ArrayPool<byte>.Shared))
{
   // use buffer.Data or buffer.Memory or buffer.Span
}

static SliceOwner ToSliceOwner(Span<byte> source, ArrayPool<byte> pool)

Creates a new SliceOwner that wraps a copy the contents of this span of bytes, using a provided ArrayPool

  • source — Span of bytes to copy
  • pool — Pool used to allocate the backing array

Returns: SliceOwner that points to a copy of the bytes in source, using an array rented from the pool as its backing store.

The return value must be disposed for the buffer to return to the pool.

// write some arbitrary data to a buffer
Span<byte> data = stackalloc byte[128];
Random.Shared.NextInt64().TryFormatTo(data, out int written);
// copy the formatted data to a rented buffer
using(var buffer = data[..written].ToSlice(ArrayPool<byte>.Shared))
{
   // use buffer.Data or buffer.Memory or buffer.Span
}

ToSliceReader

static SliceReader ToSliceReader(byte[] self)

Return a SliceReader that will expose the content of a buffer

static SliceReader ToSliceReader(byte[] self, int count)

Return a SliceReader that will expose the start of a buffer

static SliceReader ToSliceReader(byte[] self, Range range)

Return a SliceReader that will expose a subsection of a buffer

static SliceReader ToSliceReader(byte[] self, int offset, int count)

Return a SliceReader that will expose a subsection of a buffer

ToStream

static MemoryStream ToStream(Slice slice)

Return a stream that can read from the current slice.