Skip to main content

Unity.Netcode.FastBufferWriter

Implements#

System.IDisposable

Inherited Members#

System.ValueType.Equals(System.Object)

System.ValueType.GetHashCode()

System.ValueType.ToString()

System.Object.Equals(System.Object, System.Object)

System.Object.GetType()

System.Object.ReferenceEquals(System.Object, System.Object)

Syntax#
public struct FastBufferWriter : IDisposable

Constructors#

FastBufferWriter(Int32, Allocator, Int32)#

Create a FastBufferWriter.

Declaration#
public FastBufferWriter(int size, Allocator allocator, int maxSize = -1)
Parameters#
TypeNameDescription
System.Int32sizeSize of the buffer to create
AllocatorallocatorAllocator to use in creating it
System.Int32maxSizeMaximum size the buffer can grow to. If less than size, buffer cannot grow.

Properties#

Capacity#

The current total buffer size

Declaration#
public readonly int Capacity { get; }
Property Value#
TypeDescription
System.Int32

IsInitialized#

Gets a value indicating whether the writer has been initialized and a handle allocated.

Declaration#
public readonly bool IsInitialized { get; }
Property Value#
TypeDescription
System.Boolean

Length#

The total amount of bytes that have been written to the stream

Declaration#
public readonly int Length { get; }
Property Value#
TypeDescription
System.Int32

MaxCapacity#

The maximum possible total buffer size

Declaration#
public readonly int MaxCapacity { get; }
Property Value#
TypeDescription
System.Int32

Position#

The current write position

Declaration#
public readonly int Position { get; }
Property Value#
TypeDescription
System.Int32

Methods#

CopyFrom(FastBufferWriter)#

Copy the contents of another writer into this writer. The contents will be copied from the beginning of the other writer to its current position. They will be copied to this writer starting at this writer's current position.

Declaration#
public void CopyFrom(FastBufferWriter other)
Parameters#
TypeNameDescription
FastBufferWriterotherWriter to copy to

CopyTo(FastBufferWriter)#

Copy the contents of this writer into another writer. The contents will be copied from the beginning of this writer to its current position. They will be copied to the other writer starting at the other writer's current position.

Declaration#
public void CopyTo(FastBufferWriter other)
Parameters#
TypeNameDescription
FastBufferWriterotherWriter to copy to

Dispose()#

Frees the allocated buffer

Declaration#
public void Dispose()

EnterBitwiseContext()#

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

Declaration#
public BitWriter EnterBitwiseContext()
Returns#
TypeDescription
BitWriterA BitWriter

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*

GetWriteSize(String, Boolean)#

Get the required size to write a string

Declaration#
public static int GetWriteSize(string s, bool oneByteChars = false)
Parameters#
TypeNameDescription
System.StringsThe string to write
System.BooleanoneByteCharsWhether or not to use one byte per character. This will only allow ASCII
Returns#
TypeDescription
System.Int32

GetWriteSize\<T>()#

Get the size required to write an unmanaged value of type T

Declaration#
public static int GetWriteSize<T>()
where T : struct
Returns#
TypeDescription
System.Int32
Type Parameters#
NameDescription
T

GetWriteSize\<T>(in T)#

Get the size required to write an unmanaged value

Declaration#
public static int GetWriteSize<T>(in T value)
where T : struct
Parameters#
TypeNameDescription
Tvalue
Returns#
TypeDescription
System.Int32
Type Parameters#
NameDescription
T

GetWriteSize\<T>(T[], Int32, Int32)#

Get the required size to write an unmanaged array

Declaration#
public static int GetWriteSize<T>(T[] array, int count = -1, int offset = 0)
where T : struct
Parameters#
TypeNameDescription
T[]arrayThe array to write
System.Int32countThe amount of elements to write
System.Int32offsetWhere in the array to start
Returns#
TypeDescription
System.Int32
Type Parameters#
NameDescription
T

Seek(Int32)#

Move the write position in the stream. Note that moving forward past the current length will extend the buffer's Length value even if you don't write.

Declaration#
public void Seek(int where)
Parameters#
TypeNameDescription
System.Int32whereAbsolute value to move the position to, truncated to Capacity

ToArray()#

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

Declaration#
public byte[] ToArray()
Returns#
TypeDescription
System.Byte[]

Truncate(Int32)#

Truncate the stream by setting Length to the specified value. If Position is greater than the specified value, it will be moved as well.

Declaration#
public void Truncate(int where = -1)
Parameters#
TypeNameDescription
System.Int32whereThe value to truncate to. If -1, the current position will be used.

TryBeginWrite(Int32)#

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

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

Declaration#
public bool TryBeginWrite(int bytes)
Parameters#
TypeNameDescription
System.Int32bytesAmount of bytes to write
Returns#
TypeDescription
System.BooleanTrue if the write is allowed, false otherwise
Exceptions#
TypeCondition
System.InvalidOperationExceptionIf called while in a bitwise context

TryBeginWriteInternal(Int32)#

Internal version of TryBeginWrite. Differs from TryBeginWrite only in that it won't ever move the AllowedWriteMark backward.

Declaration#
public bool TryBeginWriteInternal(int bytes)
Parameters#
TypeNameDescription
System.Int32bytes
Returns#
TypeDescription
System.Boolean
Exceptions#
TypeCondition
System.InvalidOperationException

TryBeginWriteValue\<T>(in T)#

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

Unsafe write operations will throw OverflowException in editor and development builds if you go past the point you've marked using TryBeginWrite(). In release builds, OverflowException will not be thrown for performance reasons, since the point of using TryBeginWrite is to avoid bounds checking in the following operations in release builds. Instead, attempting to write past the marked position in release builds will write to random memory and cause undefined behavior, likely including instability and crashes.

Declaration#
public bool TryBeginWriteValue<T>(in T value)
where T : struct
Parameters#
TypeNameDescription
TvalueThe value you want to write
Returns#
TypeDescription
System.BooleanTrue if the write is allowed, false otherwise
Type Parameters#
NameDescription
T
Exceptions#
TypeCondition
System.InvalidOperationExceptionIf called while in a bitwise context

WriteByte(Byte)#

Write a byte to the stream.

Declaration#
public void WriteByte(byte value)
Parameters#
TypeNameDescription
System.BytevalueValue to write

WriteBytes(Byte*, Int32, Int32)#

Write multiple bytes to the stream

Declaration#
public void WriteBytes(byte *value, int size, int offset = 0)
Parameters#
TypeNameDescription
System.Byte*valueValue to write
System.Int32sizeNumber of bytes to write
System.Int32offsetOffset into the buffer to begin writing

WriteBytes(Byte[], Int32, Int32)#

Write multiple bytes to the stream

Declaration#
public void WriteBytes(byte[] value, int size = -1, int offset = 0)
Parameters#
TypeNameDescription
System.Byte[]valueValue to write
System.Int32sizeNumber of bytes to write
System.Int32offsetOffset into the buffer to begin writing

WriteByteSafe(Byte)#

Write a byte to the stream.

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

Declaration#
public void WriteByteSafe(byte value)
Parameters#
TypeNameDescription
System.BytevalueValue to write

WriteBytesSafe(Byte*, Int32, Int32)#

Write multiple bytes to the stream

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

Declaration#
public void WriteBytesSafe(byte *value, int size, int offset = 0)
Parameters#
TypeNameDescription
System.Byte*valueValue to write
System.Int32sizeNumber of bytes to write
System.Int32offsetOffset into the buffer to begin writing

WriteBytesSafe(Byte[], Int32, Int32)#

Write multiple bytes to the stream

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

Declaration#
public void WriteBytesSafe(byte[] value, int size = -1, int offset = 0)
Parameters#
TypeNameDescription
System.Byte[]valueValue to write
System.Int32sizeNumber of bytes to write
System.Int32offsetOffset into the buffer to begin writing

WriteNetworkSerializable\<T>(in T)#

Write an INetworkSerializable

Declaration#
public void WriteNetworkSerializable<T>(in T value)
where T : INetworkSerializable
Parameters#
TypeNameDescription
TvalueThe value to write
Type Parameters#
NameDescription
T

WriteNetworkSerializable\<T>(T[], Int32, Int32)#

Write an array of INetworkSerializables

Declaration#
public void WriteNetworkSerializable<T>(T[] array, int count = -1, int offset = 0)
where T : INetworkSerializable
Parameters#
TypeNameDescription
T[]arrayThe value to write
System.Int32count
System.Int32offset
Type Parameters#
NameDescription
T

WritePartialValue\<T>(T, Int32, Int32)#

Write a partial value. The specified number of bytes is written from the value and the rest is ignored.

Declaration#
public void WritePartialValue<T>(T value, int bytesToWrite, int offsetBytes = 0)
where T : struct
Parameters#
TypeNameDescription
TvalueValue to write
System.Int32bytesToWriteNumber of bytes
System.Int32offsetBytesOffset into the value to begin reading the bytes
Type Parameters#
NameDescription
T
Exceptions#
TypeCondition
System.InvalidOperationException
System.OverflowException

WriteValue(in Color)#

Declaration#
public void WriteValue(in Color value)
Parameters#
TypeNameDescription
Colorvalue

WriteValue(Color[])#

Declaration#
public void WriteValue(Color[] value)
Parameters#
TypeNameDescription
Color[]value

WriteValue(in Color32)#

Declaration#
public void WriteValue(in Color32 value)
Parameters#
TypeNameDescription
Color32value

WriteValue(Color32[])#

Declaration#
public void WriteValue(Color32[] value)
Parameters#
TypeNameDescription
Color32[]value

WriteValue(in Quaternion)#

Declaration#
public void WriteValue(in Quaternion value)
Parameters#
TypeNameDescription
Quaternionvalue

WriteValue(Quaternion[])#

Declaration#
public void WriteValue(Quaternion[] value)
Parameters#
TypeNameDescription
Quaternion[]value

WriteValue(in Ray)#

Declaration#
public void WriteValue(in Ray value)
Parameters#
TypeNameDescription
Rayvalue

WriteValue(Ray[])#

Declaration#
public void WriteValue(Ray[] value)
Parameters#
TypeNameDescription
Ray[]value

WriteValue(in Ray2D)#

Declaration#
public void WriteValue(in Ray2D value)
Parameters#
TypeNameDescription
Ray2Dvalue

WriteValue(Ray2D[])#

Declaration#
public void WriteValue(Ray2D[] value)
Parameters#
TypeNameDescription
Ray2D[]value

WriteValue(String, Boolean)#

Writes a string

Declaration#
public void WriteValue(string s, bool oneByteChars = false)
Parameters#
TypeNameDescription
System.StringsThe string to write
System.BooleanoneByteCharsWhether or not to use one byte per character. This will only allow ASCII

WriteValue(in Vector2)#

Declaration#
public void WriteValue(in Vector2 value)
Parameters#
TypeNameDescription
Vector2value

WriteValue(Vector2[])#

Declaration#
public void WriteValue(Vector2[] value)
Parameters#
TypeNameDescription
Vector2[]value

WriteValue(in Vector3)#

Declaration#
public void WriteValue(in Vector3 value)
Parameters#
TypeNameDescription
Vector3value

WriteValue(Vector3[])#

Declaration#
public void WriteValue(Vector3[] value)
Parameters#
TypeNameDescription
Vector3[]value

WriteValue(in Vector4)#

Declaration#
public void WriteValue(in Vector4 value)
Parameters#
TypeNameDescription
Vector4value

WriteValue(Vector4[])#

Declaration#
public void WriteValue(Vector4[] value)
Parameters#
TypeNameDescription
Vector4[]value

WriteValue\<T>(in T, FastBufferWriter.ForEnums)#

Declaration#
public void WriteValue<T>(in T value, FastBufferWriter.ForEnums unused = default(FastBufferWriter.ForEnums))
where T : struct, Enum
Parameters#
TypeNameDescription
Tvalue
FastBufferWriter.ForEnumsunused
Type Parameters#
NameDescription
T

WriteValue\<T>(in T, FastBufferWriter.ForNetworkSerializable)#

Declaration#
public void WriteValue<T>(in T value, FastBufferWriter.ForNetworkSerializable unused = default(FastBufferWriter.ForNetworkSerializable))
where T : INetworkSerializable
Parameters#
TypeNameDescription
Tvalue
FastBufferWriter.ForNetworkSerializableunused
Type Parameters#
NameDescription
T

WriteValue\<T>(in T, FastBufferWriter.ForPrimitives)#

Declaration#
public void WriteValue<T>(in T value, FastBufferWriter.ForPrimitives unused = default(FastBufferWriter.ForPrimitives))
where T : struct, IComparable, IConvertible, IComparable<T>, IEquatable<T>
Parameters#
TypeNameDescription
Tvalue
FastBufferWriter.ForPrimitivesunused
Type Parameters#
NameDescription
T

WriteValue\<T>(in T, FastBufferWriter.ForStructs)#

Declaration#
public void WriteValue<T>(in T value, FastBufferWriter.ForStructs unused = default(FastBufferWriter.ForStructs))
where T : struct, INetworkSerializeByMemcpy
Parameters#
TypeNameDescription
Tvalue
FastBufferWriter.ForStructsunused
Type Parameters#
NameDescription
T

WriteValue\<T>(T[], FastBufferWriter.ForEnums)#

Declaration#
public void WriteValue<T>(T[] value, FastBufferWriter.ForEnums unused = default(FastBufferWriter.ForEnums))
where T : struct, Enum
Parameters#
TypeNameDescription
T[]value
FastBufferWriter.ForEnumsunused
Type Parameters#
NameDescription
T

WriteValue\<T>(T[], FastBufferWriter.ForNetworkSerializable)#

Declaration#
public void WriteValue<T>(T[] value, FastBufferWriter.ForNetworkSerializable unused = default(FastBufferWriter.ForNetworkSerializable))
where T : INetworkSerializable
Parameters#
TypeNameDescription
T[]value
FastBufferWriter.ForNetworkSerializableunused
Type Parameters#
NameDescription
T

WriteValue\<T>(T[], FastBufferWriter.ForPrimitives)#

Declaration#
public void WriteValue<T>(T[] value, FastBufferWriter.ForPrimitives unused = default(FastBufferWriter.ForPrimitives))
where T : struct, IComparable, IConvertible, IComparable<T>, IEquatable<T>
Parameters#
TypeNameDescription
T[]value
FastBufferWriter.ForPrimitivesunused
Type Parameters#
NameDescription
T

WriteValue\<T>(T[], FastBufferWriter.ForStructs)#

Declaration#
public void WriteValue<T>(T[] value, FastBufferWriter.ForStructs unused = default(FastBufferWriter.ForStructs))
where T : struct, INetworkSerializeByMemcpy
Parameters#
TypeNameDescription
T[]value
FastBufferWriter.ForStructsunused
Type Parameters#
NameDescription
T

WriteValueSafe(in Color)#

Declaration#
public void WriteValueSafe(in Color value)
Parameters#
TypeNameDescription
Colorvalue

WriteValueSafe(Color[])#

Declaration#
public void WriteValueSafe(Color[] value)
Parameters#
TypeNameDescription
Color[]value

WriteValueSafe(in Color32)#

Declaration#
public void WriteValueSafe(in Color32 value)
Parameters#
TypeNameDescription
Color32value

WriteValueSafe(Color32[])#

Declaration#
public void WriteValueSafe(Color32[] value)
Parameters#
TypeNameDescription
Color32[]value

WriteValueSafe(in Quaternion)#

Declaration#
public void WriteValueSafe(in Quaternion value)
Parameters#
TypeNameDescription
Quaternionvalue

WriteValueSafe(Quaternion[])#

Declaration#
public void WriteValueSafe(Quaternion[] value)
Parameters#
TypeNameDescription
Quaternion[]value

WriteValueSafe(in Ray)#

Declaration#
public void WriteValueSafe(in Ray value)
Parameters#
TypeNameDescription
Rayvalue

WriteValueSafe(Ray[])#

Declaration#
public void WriteValueSafe(Ray[] value)
Parameters#
TypeNameDescription
Ray[]value

WriteValueSafe(in Ray2D)#

Declaration#
public void WriteValueSafe(in Ray2D value)
Parameters#
TypeNameDescription
Ray2Dvalue

WriteValueSafe(Ray2D[])#

Declaration#
public void WriteValueSafe(Ray2D[] value)
Parameters#
TypeNameDescription
Ray2D[]value

WriteValueSafe(String, Boolean)#

Writes a string

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

Declaration#
public void WriteValueSafe(string s, bool oneByteChars = false)
Parameters#
TypeNameDescription
System.StringsThe string to write
System.BooleanoneByteCharsWhether or not to use one byte per character. This will only allow ASCII

WriteValueSafe(in Vector2)#

Declaration#
public void WriteValueSafe(in Vector2 value)
Parameters#
TypeNameDescription
Vector2value

WriteValueSafe(Vector2[])#

Declaration#
public void WriteValueSafe(Vector2[] value)
Parameters#
TypeNameDescription
Vector2[]value

WriteValueSafe(in Vector3)#

Declaration#
public void WriteValueSafe(in Vector3 value)
Parameters#
TypeNameDescription
Vector3value

WriteValueSafe(Vector3[])#

Declaration#
public void WriteValueSafe(Vector3[] value)
Parameters#
TypeNameDescription
Vector3[]value

WriteValueSafe(in Vector4)#

Declaration#
public void WriteValueSafe(in Vector4 value)
Parameters#
TypeNameDescription
Vector4value

WriteValueSafe(Vector4[])#

Declaration#
public void WriteValueSafe(Vector4[] value)
Parameters#
TypeNameDescription
Vector4[]value

WriteValueSafe\<T>(in T, FastBufferWriter.ForEnums)#

Declaration#
public void WriteValueSafe<T>(in T value, FastBufferWriter.ForEnums unused = default(FastBufferWriter.ForEnums))
where T : struct, Enum
Parameters#
TypeNameDescription
Tvalue
FastBufferWriter.ForEnumsunused
Type Parameters#
NameDescription
T

WriteValueSafe\<T>(in T, FastBufferWriter.ForNetworkSerializable)#

Declaration#
public void WriteValueSafe<T>(in T value, FastBufferWriter.ForNetworkSerializable unused = default(FastBufferWriter.ForNetworkSerializable))
where T : INetworkSerializable
Parameters#
TypeNameDescription
Tvalue
FastBufferWriter.ForNetworkSerializableunused
Type Parameters#
NameDescription
T

WriteValueSafe\<T>(in T, FastBufferWriter.ForPrimitives)#

Declaration#
public void WriteValueSafe<T>(in T value, FastBufferWriter.ForPrimitives unused = default(FastBufferWriter.ForPrimitives))
where T : struct, IComparable, IConvertible, IComparable<T>, IEquatable<T>
Parameters#
TypeNameDescription
Tvalue
FastBufferWriter.ForPrimitivesunused
Type Parameters#
NameDescription
T

WriteValueSafe\<T>(in T, FastBufferWriter.ForStructs)#

Declaration#
public void WriteValueSafe<T>(in T value, FastBufferWriter.ForStructs unused = default(FastBufferWriter.ForStructs))
where T : struct, INetworkSerializeByMemcpy
Parameters#
TypeNameDescription
Tvalue
FastBufferWriter.ForStructsunused
Type Parameters#
NameDescription
T

WriteValueSafe\<T>(T[], FastBufferWriter.ForEnums)#

Declaration#
public void WriteValueSafe<T>(T[] value, FastBufferWriter.ForEnums unused = default(FastBufferWriter.ForEnums))
where T : struct, Enum
Parameters#
TypeNameDescription
T[]value
FastBufferWriter.ForEnumsunused
Type Parameters#
NameDescription
T

WriteValueSafe\<T>(T[], FastBufferWriter.ForNetworkSerializable)#

Declaration#
public void WriteValueSafe<T>(T[] value, FastBufferWriter.ForNetworkSerializable unused = default(FastBufferWriter.ForNetworkSerializable))
where T : INetworkSerializable
Parameters#
TypeNameDescription
T[]value
FastBufferWriter.ForNetworkSerializableunused
Type Parameters#
NameDescription
T

WriteValueSafe\<T>(T[], FastBufferWriter.ForPrimitives)#

Declaration#
public void WriteValueSafe<T>(T[] value, FastBufferWriter.ForPrimitives unused = default(FastBufferWriter.ForPrimitives))
where T : struct, IComparable, IConvertible, IComparable<T>, IEquatable<T>
Parameters#
TypeNameDescription
T[]value
FastBufferWriter.ForPrimitivesunused
Type Parameters#
NameDescription
T

WriteValueSafe\<T>(T[], FastBufferWriter.ForStructs)#

Declaration#
public void WriteValueSafe<T>(T[] value, FastBufferWriter.ForStructs unused = default(FastBufferWriter.ForStructs))
where T : struct, INetworkSerializeByMemcpy
Parameters#
TypeNameDescription
T[]value
FastBufferWriter.ForStructsunused
Type Parameters#
NameDescription
T

Implements#

System.IDisposable