Class ByteBuffer

Namespace
Elegy.Common.Utilities
Assembly
Elegy.Common.dll
public class ByteBuffer : IByteBuffer
Inheritance
ByteBuffer
Implements
Inherited Members

Constructors

ByteBuffer(byte[])

Initialises the byte buffer with an existing buffer of data.

public ByteBuffer(byte[] data)

Parameters

data byte[]

ByteBuffer(int)

Initialises the byte buffer so it can be filled up to size bytes.

public ByteBuffer(int size)

Parameters

size int

Properties

Data

Buffer data.

public IReadOnlyList<byte> Data { get; }

Property Value

IReadOnlyList<byte>

DataSpan

Span version of Data.

public ReadOnlySpan<byte> DataSpan { get; }

Property Value

ReadOnlySpan<byte>

Position

Cursor position.

public int Position { get; }

Property Value

int

Methods

Advance(int)

Advances the cursor bytes number of bytes.

public void Advance(int bytes)

Parameters

bytes int

ReadBool()

Reads a boolean value and advances 1 byte.

public bool ReadBool()

Returns

bool

ReadChar()

Reads an ASCII character value and advances 1 byte.

public char ReadChar()

Returns

char

ReadEnum<T>()

Reads an enum value and advances 1 byte.

public T ReadEnum<T>() where T : struct, Enum

Returns

T

Type Parameters

T

ReadF16()

Reads a half-precision float value and advances 2 bytes.

public Half ReadF16()

Returns

Half

ReadF32()

Reads a single-precision float value and advances 4 bytes.

public float ReadF32()

Returns

float

ReadF64()

Reads a double-precision float value and advances 8 bytes.

public double ReadF64()

Returns

double

ReadI16()

Reads a signed short value and advances 2 bytes.

public short ReadI16()

Returns

short

ReadI32()

Reads a signed integer value and advances 4 bytes.

public int ReadI32()

Returns

int

ReadI64()

Reads a signed long value and advances 8 bytes.

public long ReadI64()

Returns

long

ReadObject<T>()

Reads/deserialises an object that implements IByteSerialisable.

public T ReadObject<T>() where T : IByteSerialisable, new()

Returns

T

Type Parameters

T

ReadString(Encoding, StringLength)

Reads a string from the buffer.

public string ReadString(Encoding stringEncoding, StringLength stringLength)

Parameters

stringEncoding Encoding

What type of encoding to use.

stringLength StringLength

Choose between 256 (Short), 65k (Medium) and 2B max characters (Long).

Returns

string

Exceptions

IndexOutOfRangeException

ReadStringAscii(StringLength)

Reads an ASCII-encoded string from the buffer.

public string ReadStringAscii(StringLength stringLength)

Parameters

stringLength StringLength

Returns

string

ReadStringUtf8(StringLength)

Reads a UTF-8-encoded string from the buffer.

public string ReadStringUtf8(StringLength stringLength)

Parameters

stringLength StringLength

Returns

string

ReadU16()

Reads an unsigned short value and advances 2 bytes.

public ushort ReadU16()

Returns

ushort

ReadU32()

Reads an unsigned integer value and advances 4 bytes.

public uint ReadU32()

Returns

uint

ReadU64()

Reads an unsigned long value and advances 8 bytes.

public ulong ReadU64()

Returns

ulong

ReadU8()

Reads an unsigned byte value and advances 1 byte.

public byte ReadU8()

Returns

byte

Read<T>()

Reads generic data from the buffer.

public T Read<T>() where T : unmanaged

Returns

T

Type Parameters

T

The type of data that gets read.

ResetData()

Clears the data buffer.

public void ResetData()

ResetPosition()

Resets the cursor to the beginning of the buffer.

public void ResetPosition()

WriteBool(bool)

Writes a boolean value and advances 1 byte.

public void WriteBool(bool value)

Parameters

value bool

WriteChar(char)

Writes an ASCII character value and advances 1 byte.

public void WriteChar(char value)

Parameters

value char

WriteEnum<T>(T)

Writes an enum value and advances 1 byte.

public void WriteEnum<T>(T value) where T : struct, Enum, IConvertible

Parameters

value T

Type Parameters

T

WriteF16(Half)

Writes a half-precision float value and advances 2 bytes.

public void WriteF16(Half value)

Parameters

value Half

WriteF32(float)

Writes a single-precision float value and advances 4 bytes.

public void WriteF32(float value)

Parameters

value float

WriteF64(double)

Writes a double-precision float value and advances 8 bytes.

public void WriteF64(double value)

Parameters

value double

WriteI16(short)

Writes a signed short value and advances 2 bytes.

public void WriteI16(short value)

Parameters

value short

WriteI32(int)

Writes a signed integer value and advances 4 bytes.

public void WriteI32(int value)

Parameters

value int

WriteI64(long)

Writes a signed long value and advances 8 bytes.

public void WriteI64(long value)

Parameters

value long

WriteObject<T>(T)

Writes/serialises an object that implements IByteSerialisable.

public void WriteObject<T>(T value) where T : IByteSerialisable

Parameters

value T

Type Parameters

T

WriteString(string, Encoding, StringLength)

Writes a string in the format: number of characters, raw string contents.

public void WriteString(string value, Encoding stringEncoding, StringLength stringLength)

Parameters

value string

The value to encode.

stringEncoding Encoding

What type of string encoding to use (ASCII, UTF-8).

stringLength StringLength

Choose between 256 (Short), 65k (Medium) and 2B max characters (Long).

WriteStringAscii(string, StringLength)

Writes an ASCII-encoded string.

public void WriteStringAscii(string value, StringLength stringLength)

Parameters

value string
stringLength StringLength

WriteStringUtf8(string, StringLength)

Writes a UTF-8-encoded string.

public void WriteStringUtf8(string value, StringLength stringLength)

Parameters

value string
stringLength StringLength

WriteU16(ushort)

Writes an unsigned short value and advances 2 bytes.

public void WriteU16(ushort value)

Parameters

value ushort

WriteU32(uint)

Writes an unsigned integer value and advances 4 bytes.

public void WriteU32(uint value)

Parameters

value uint

WriteU64(ulong)

Writes an unsigned long value and advances 8 bytes.

public void WriteU64(ulong value)

Parameters

value ulong

WriteU8(byte)

Writes an unsigned byte value and advances 1 byte.

public void WriteU8(byte value)

Parameters

value byte

Write<T>(T)

Writes generic data into the buffer.

public void Write<T>(T value) where T : unmanaged

Parameters

value T

Type Parameters

T

The type of data that gets written.