Skip to main content

Unity.Netcode.FastBufferReader

Implements#

System.IDisposable

Inherited Members#

ValueType.Equals(Object)

ValueType.GetHashCode()

ValueType.ToString()

Object.Equals(Object, Object)

Object.GetType()

Object.ReferenceEquals(Object, Object)

Namespace: System.Dynamic.ExpandoObject#
Assembly: MLAPI.dll#
Syntax#
public struct FastBufferReader : IDisposable

FastBufferReader(NativeArray\<Byte>, Allocator, Int32, Int32)#

Create a FastBufferReader from a NativeArray.

A new buffer will be created using the given allocator and the value will be copied in. FastBufferReader will then own the data.

The exception to this is when the allocator passed in is Allocator.None. In this scenario, ownership of the data remains with the caller and the reader will point at it directly. When created with Allocator.None, FastBufferReader will allocate some internal data using Allocator.Temp, so it should be treated as if it's a ref struct and not allowed to outlive the context in which it was created (it should neither be returned from that function nor stored anywhere in heap memory).

Declaration#

public FastBufferReader(NativeArray<byte> buffer, Allocator allocator, int length = -1, int offset = 0)

Parameters#

TypeNameDescription
NativeArray\<System.Byte>buffer
Allocatorallocator
System.Int32length
System.Int32offset

FastBufferReader(ArraySegment\<Byte>, Allocator, Int32, Int32)#

Create a FastBufferReader from an ArraySegment.

A new buffer will be created using the given allocator and the value will be copied in. FastBufferReader will then own the data.

Allocator.None is not supported for byte[]. If you need this functionality, use a fixed() block and ensure the FastBufferReader isn't used outside that block.

Declaration#

public FastBufferReader(ArraySegment<byte> buffer, Allocator allocator, int length = -1, int offset = 0)

Parameters#

TypeNameDescription
System.ArraySegment\<System.Byte>bufferThe buffer to copy from
AllocatorallocatorThe allocator to use
System.Int32lengthThe number of bytes to copy (all if this is -1)
System.Int32offsetThe offset of the buffer to start copying from

FastBufferReader(Byte*, Allocator, Int32, Int32)#

Create a FastBufferReader from an existing byte buffer.

A new buffer will be created using the given allocator and the value will be copied in. FastBufferReader will then own the data.

The exception to this is when the allocator passed in is Allocator.None. In this scenario, ownership of the data remains with the caller and the reader will point at it directly. When created with Allocator.None, FastBufferReader will allocate some internal data using Allocator.Temp, so it should be treated as if it's a ref struct and not allowed to outlive the context in which it was created (it should neither be returned from that function nor stored anywhere in heap memory).

Declaration#

public FastBufferReader(byte *buffer, Allocator allocator, int length, int offset = 0)

Parameters#

TypeNameDescription
System.Byte*bufferThe buffer to copy from
AllocatorallocatorThe allocator to use
System.Int32lengthThe number of bytes to copy
System.Int32offsetThe offset of the buffer to start copying from

FastBufferReader(Byte[], Allocator, Int32, Int32)#

Create a FastBufferReader from an existing byte array.

A new buffer will be created using the given allocator and the value will be copied in. FastBufferReader will then own the data.

Allocator.None is not supported for byte[]. If you need this functionality, use a fixed() block and ensure the FastBufferReader isn't used outside that block.

Declaration#

public FastBufferReader(byte[] buffer, Allocator allocator, int length = -1, int offset = 0)

Parameters#

TypeNameDescription
System.Byte[]bufferThe buffer to copy from
AllocatorallocatorThe allocator to use
System.Int32lengthThe number of bytes to copy (all if this is -1)
System.Int32offsetThe offset of the buffer to start copying from

FastBufferReader(FastBufferWriter, Allocator, Int32, Int32)#

Create a FastBufferReader from a FastBufferWriter.

A new buffer will be created using the given allocator and the value will be copied in. FastBufferReader will then own the data.

The exception to this is when the allocator passed in is Allocator.None. In this scenario, ownership of the data remains with the caller and the reader will point at it directly. When created with Allocator.None, FastBufferReader will allocate some internal data using Allocator.Temp, so it should be treated as if it's a ref struct and not allowed to outlive the context in which it was created (it should neither be returned from that function nor stored anywhere in heap memory).

Declaration#

public FastBufferReader(FastBufferWriter writer, Allocator allocator, int length = -1, int offset = 0)

Parameters#

TypeNameDescription
FastBufferWriterwriterThe writer to copy from
AllocatorallocatorThe allocator to use
System.Int32lengthThe number of bytes to copy (all if this is -1)
System.Int32offsetThe offset of the buffer to start copying from

#

Length#

Get the total length of the buffer

Declaration#

public readonly int Length { get; }

Property Value#

TypeDescription
System.Int32

Position#

Get the current read position

Declaration#

public readonly int Position { get; }

Property Value#

TypeDescription
System.Int32

#

Dispose()#

Frees the allocated buffer

Declaration#

public void Dispose()

EnterBitwiseContext()#

Retrieve a BitReader to be able to perform bitwise operations on the buffer. No bytewise operations can be performed on the buffer until bitReader.Dispose() has been called. At the end of the operation, FastBufferReader will remain byte-aligned.

Declaration#

public BitReader EnterBitwiseContext()

Returns#

TypeDescription
BitReaderA BitReader

GetUnsafePtr()#

Gets a direct pointer to the underlying buffer

Declaration#

public byte *GetUnsafePtr()

Returns#

TypeDescription
System.Byte*

GetUnsafePtrAtCurrentPosition()#

Gets a direct pointer to the underlying buffer at the current read position

Declaration#

public byte *GetUnsafePtrAtCurrentPosition()

Returns#

TypeDescription
System.Byte*

ReadByte(out Byte)#

Read a byte to the stream.

Declaration#

public void ReadByte(out byte value)

Parameters#

TypeNameDescription
System.BytevalueStores the read value

ReadBytes(Byte*, Int32, Int32)#

Read multiple bytes to the stream

Declaration#

public void ReadBytes(byte *value, int size, int offset = 0)

Parameters#

TypeNameDescription
System.Byte*valuePointer to the destination buffer
System.Int32sizeNumber of bytes to read - MUST BE \<= BUFFER SIZE
System.Int32offsetOffset of the byte buffer to store into

ReadBytes(ref Byte[], Int32, Int32)#

Read multiple bytes from the stream

Declaration#

public void ReadBytes(ref byte[] value, int size, int offset = 0)

Parameters#

TypeNameDescription
System.Byte[]valuePointer to the destination buffer
System.Int32sizeNumber of bytes to read - MUST BE \<= BUFFER SIZE
System.Int32offsetOffset of the byte buffer to store into

ReadByteSafe(out Byte)#

Read a byte to the stream.

"Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

Declaration#

public void ReadByteSafe(out byte value)

Parameters#

TypeNameDescription
System.BytevalueStores the read value

ReadBytesSafe(Byte*, Int32, Int32)#

Read multiple bytes to the stream

"Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

Declaration#

public void ReadBytesSafe(byte *value, int size, int offset = 0)

Parameters#

TypeNameDescription
System.Byte*valuePointer to the destination buffer
System.Int32sizeNumber of bytes to read - MUST BE \<= BUFFER SIZE
System.Int32offsetOffset of the byte buffer to store into

ReadBytesSafe(ref Byte[], Int32, Int32)#

Read multiple bytes from the stream

"Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

Declaration#

public void ReadBytesSafe(ref byte[] value, int size, int offset = 0)

Parameters#

TypeNameDescription
System.Byte[]valuePointer to the destination buffer
System.Int32sizeNumber of bytes to read - MUST BE \<= BUFFER SIZE
System.Int32offsetOffset of the byte buffer to store into

ReadNetworkSerializable\<T>(out T)#

Read an INetworkSerializable

Declaration#

public void ReadNetworkSerializable<T>(out T value)
where T : INetworkSerializable, new()

Parameters#

TypeNameDescription
TvalueINetworkSerializable instance

Type Parameters#

NameDescription
T

Exceptions#

TypeCondition
System.NotImplementedException

ReadNetworkSerializable\<T>(out T[])#

Read an array of INetworkSerializables

Declaration#

public void ReadNetworkSerializable<T>(out T[] value)
where T : INetworkSerializable, new()

Parameters#

TypeNameDescription
T[]valueINetworkSerializable instance

Type Parameters#

NameDescription
T

Exceptions#

TypeCondition
System.NotImplementedException

ReadPartialValue\<T>(out T, Int32, Int32)#

Read a partial value. The value is zero-initialized and then the specified number of bytes is read into it.

Declaration#

public void ReadPartialValue<T>(out T value, int bytesToRead, int offsetBytes = 0)
where T : struct

Parameters#

TypeNameDescription
TvalueValue to read
System.Int32bytesToReadNumber of bytes
System.Int32offsetBytesOffset into the value to write the bytes

Type Parameters#

NameDescription
T

Exceptions#

TypeCondition
System.InvalidOperationException
System.OverflowException

ReadValue(out String, Boolean)#

Reads a string NOTE: ALLOCATES

Declaration#

public void ReadValue(out string s, bool oneByteChars = false)

Parameters#

TypeNameDescription
System.StringsStores the read string
System.BooleanoneByteCharsWhether or not to use one byte per character. This will only allow ASCII

ReadValue\<T>(out T)#

Read a value of any unmanaged type to the buffer. It will be copied from the buffer exactly as it existed in memory on the writing end.

Declaration#

public void ReadValue<T>(out T value)
where T : struct

Parameters#

TypeNameDescription
TvalueThe read value

Type Parameters#

NameDescription
TAny unmanaged type

ReadValue\<T>(out T[])#

Writes an unmanaged array NOTE: ALLOCATES

Declaration#

public void ReadValue<T>(out T[] array)
where T : struct

Parameters#

TypeNameDescription
T[]arrayStores the read array

Type Parameters#

NameDescription
T

ReadValueSafe(out String, Boolean)#

Reads a string. NOTE: ALLOCATES

"Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

Declaration#

public void ReadValueSafe(out string s, bool oneByteChars = false)

Parameters#

TypeNameDescription
System.StringsStores the read string
System.BooleanoneByteCharsWhether or not to use one byte per character. This will only allow ASCII

ReadValueSafe\<T>(out T)#

Read a value of any unmanaged type to the buffer. It will be copied from the buffer exactly as it existed in memory on the writing end.

"Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

Declaration#

public void ReadValueSafe<T>(out T value)
where T : struct

Parameters#

TypeNameDescription
TvalueThe read value

Type Parameters#

NameDescription
TAny unmanaged type

ReadValueSafe\<T>(out T[])#

Reads an unmanaged array NOTE: ALLOCATES

"Safe" version - automatically performs bounds checking. Less efficient than bounds checking for multiple reads at once by calling TryBeginRead.

Declaration#

public void ReadValueSafe<T>(out T[] array)
where T : struct

Parameters#

TypeNameDescription
T[]arrayStores the read array

Type Parameters#

NameDescription
T

Seek(Int32)#

Move the read position in the stream

Declaration#

public void Seek(int where)

Parameters#

TypeNameDescription
System.Int32whereAbsolute value to move the position to, truncated to Length

ToArray()#

Returns an array representation of the underlying byte buffer. !!Allocates a new array!!

Declaration#

public byte[] ToArray()

Returns#

TypeDescription
System.Byte[]

TryBeginRead(Int32)#

Allows faster serialization by batching bounds checking. When you know you will be reading multiple fields back-to-back and you know the total size, you can call TryBeginRead() once on the total size, and then follow it with calls to ReadValue() instead of ReadValueSafe() for faster serialization.

Unsafe read operations will throw OverflowException in editor and development builds if you go past the point you've marked using TryBeginRead(). In release builds, OverflowException will not be thrown for performance reasons, since the point of using TryBeginRead is to avoid bounds checking in the following operations in release builds.

Declaration#

public bool TryBeginRead(int bytes)

Parameters#

TypeNameDescription
System.Int32bytesAmount of bytes to read

Returns#

TypeDescription
System.BooleanTrue if the read is allowed, false otherwise

Exceptions#

TypeCondition
System.InvalidOperationExceptionIf called while in a bitwise context

TryBeginReadValue\<T>(in T)#

Allows faster serialization by batching bounds checking. When you know you will be reading multiple fields back-to-back and you know the total size, you can call TryBeginRead() once on the total size, and then follow it with calls to ReadValue() instead of ReadValueSafe() for faster serialization.

Unsafe read operations will throw OverflowException in editor and development builds if you go past the point you've marked using TryBeginRead(). In release builds, OverflowException will not be thrown for performance reasons, since the point of using TryBeginRead is to avoid bounds checking in the following operations in release builds.

Declaration#

public bool TryBeginReadValue<T>(in T value)
where T : struct

Parameters#

TypeNameDescription
TvalueThe value you want to read

Returns#

TypeDescription
System.BooleanTrue if the read is allowed, false otherwise

Type Parameters#

NameDescription
T

Exceptions#

TypeCondition
System.InvalidOperationExceptionIf called while in a bitwise context

Implements#

System.IDisposable