Class NetDataWriter
Inheritance
Namespace: LiteNetLib.Utils
Assembly: LiteNetLib.dll
Syntax
public class NetDataWriter
Constructors
NetDataWriter()
Declaration
public NetDataWriter()
NetDataWriter(bool)
Declaration
public NetDataWriter(bool autoResize)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | autoResize |
NetDataWriter(bool, int)
Declaration
public NetDataWriter(bool autoResize, int initialSize)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | autoResize | |
| int | initialSize |
Fields
_data
Declaration
protected byte[] _data
Field Value
| Type | Description |
|---|---|
| byte[] |
_position
Declaration
protected int _position
Field Value
| Type | Description |
|---|---|
| int |
Properties
Capacity
Gets the total capacity of the internal byte buffer.
Declaration
public int Capacity { get; }
Property Value
| Type | Description |
|---|---|
| int | The length of the underlying _data array. |
Data
Gets the underlying byte array used by this writer.
Declaration
public byte[] Data { get; }
Property Value
| Type | Description |
|---|---|
| byte[] | The internal _data array. |
Remarks
Accessing this directly allows for external manipulation but bypasses bounds checking.
Length
Gets the current number of bytes written to the buffer.
Declaration
public int Length { get; }
Property Value
| Type | Description |
|---|---|
| int | The current _position. |
Methods
AsReadOnlySpan()
Returns a ReadOnlySpan<T> representing the currently used portion of the internal buffer.
Declaration
public ReadOnlySpan<byte> AsReadOnlySpan()
Returns
| Type | Description |
|---|---|
| ReadOnlySpan<byte> | A ReadOnlySpan<T> from index 0 to _position. |
Remarks
Provides a high-performance, zero-allocation view of the data. The span becomes invalid if the internal buffer is resized or if _position changes.
CopyData()
Creates a byte array containing the current data from the internal buffer.
Declaration
public byte[] CopyData()
Returns
| Type | Description |
|---|---|
| byte[] |
Remarks
This method performs a heap allocation and copies the data using BlockCopy(Array, int, Array, int, int).
EnsureFit(int)
Ensures the internal buffer can accommodate additionalSize more bytes.
Declaration
public void EnsureFit(int additionalSize)
Parameters
| Type | Name | Description |
|---|---|---|
| int | additionalSize | The number of additional bytes to fit. |
Remarks
This checks against the current _position. If the capacity is insufficient, the buffer grows to either the required size or doubles its current size.
FromBytes(byte[], bool)
Creates NetDataWriter from existing ByteArray
Declaration
public static NetDataWriter FromBytes(byte[] bytes, bool copy)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | bytes | Source byte array |
| bool | copy | Copy array to new location or use existing |
Returns
| Type | Description |
|---|---|
| NetDataWriter |
FromBytes(byte[], int, int)
Creates NetDataWriter from existing ByteArray (always copied data)
Declaration
public static NetDataWriter FromBytes(byte[] bytes, int offset, int length)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | bytes | Source byte array |
| int | offset | Offset of array |
| int | length | Length of array |
Returns
| Type | Description |
|---|---|
| NetDataWriter |
FromBytes(Span<byte>)
Creates NetDataWriter from the given bytes.
Declaration
public static NetDataWriter FromBytes(Span<byte> bytes)
Parameters
| Type | Name | Description |
|---|---|---|
| Span<byte> | bytes |
Returns
| Type | Description |
|---|---|
| NetDataWriter |
FromString(string)
Creates a new NetDataWriter and serializes a string into it.
Declaration
public static NetDataWriter FromString(string value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value | The string to serialize. |
Returns
| Type | Description |
|---|---|
| NetDataWriter | A new NetDataWriter instance containing the serialized string. |
Put(bool)
Declaration
public void Put(bool value)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | value | The bool value to write. |
Put(byte)
Serializes a byte value.
Declaration
public void Put(byte value)
Parameters
| Type | Name | Description |
|---|---|---|
| byte | value | The byte value to write. |
Put(byte[])
Serializes an entire byte array.
Declaration
public void Put(byte[] data)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | data | The source array. |
Put(byte[], int, int)
Serializes a segment of a byte array.
Declaration
public void Put(byte[] data, int offset, int length)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | data | The source array. |
| int | offset | The starting index in the source array. |
| int | length | The number of bytes to write. |
Put(char)
Declaration
public void Put(char value)
Parameters
| Type | Name | Description |
|---|---|---|
| char | value | The char value to write. |
Put(double)
Serializes a double value.
Declaration
public void Put(double value)
Parameters
| Type | Name | Description |
|---|---|---|
| double | value | The double value to write. |
Put(Guid)
Serializes a Guid value.
Declaration
public void Put(Guid value)
Parameters
| Type | Name | Description |
|---|---|---|
| Guid | value | The Guid value to write. |
Put(short)
Serializes a short value.
Declaration
public void Put(short value)
Parameters
| Type | Name | Description |
|---|---|---|
| short | value | The short value to write. |
Put(int)
Serializes an int value.
Declaration
public void Put(int value)
Parameters
| Type | Name | Description |
|---|---|---|
| int | value | The int value to write. |
Put(long)
Serializes a long value.
Declaration
public void Put(long value)
Parameters
| Type | Name | Description |
|---|---|---|
| long | value | The long value to write. |
Put(IPEndPoint)
Serializes an IPEndPoint.
Declaration
public void Put(IPEndPoint endPoint)
Parameters
| Type | Name | Description |
|---|---|---|
| IPEndPoint | endPoint | The network endpoint to write. |
Remarks
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Thrown when the AddressFamily is not InterNetwork or InterNetworkV6. |
Put(ReadOnlySpan<byte>)
Serializes a ReadOnlySpan<T> of bytes to the internal buffer.
Declaration
public void Put(ReadOnlySpan<byte> data)
Parameters
| Type | Name | Description |
|---|---|---|
| ReadOnlySpan<byte> | data | The span of data to write. |
Put(sbyte)
Serializes an sbyte value.
Declaration
public void Put(sbyte value)
Parameters
| Type | Name | Description |
|---|---|---|
| sbyte | value | The sbyte value to write. |
Put(float)
Serializes a float value.
Declaration
public void Put(float value)
Parameters
| Type | Name | Description |
|---|---|---|
| float | value | The float value to write. |
Put(string, int)
Serializes a string using a 2-byte ushort length header.
Declaration
public void Put(string value, int maxLength = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value | The string to write to the buffer. |
| int | maxLength | The maximum number of characters to write. If the string is longer, it will be truncated. |
Remarks
Put(ushort)
Serializes a ushort value.
Declaration
public void Put(ushort value)
Parameters
| Type | Name | Description |
|---|---|---|
| ushort | value | The ushort value to write. |
Put(uint)
Serializes a uint value.
Declaration
public void Put(uint value)
Parameters
| Type | Name | Description |
|---|---|---|
| uint | value | The uint value to write. |
Put(ulong)
Serializes a ulong value.
Declaration
public void Put(ulong value)
Parameters
| Type | Name | Description |
|---|---|---|
| ulong | value | The ulong value to write. |
PutArray(Array, int)
Declaration
public void PutArray(Array arr, int sz)
Parameters
| Type | Name | Description |
|---|---|---|
| Array | arr | The source array to serialize. |
| int | sz | The size of a single element in bytes. |
Remarks
If the array is null, a length of 0 is written.
The total payload size is calculated as length * sz.
PutArray(bool[])
Serializes an array of unmanaged values to the internal buffer.
Declaration
public void PutArray(bool[] value)
Parameters
| Type | Name | Description |
|---|---|---|
| bool[] | value |
PutArray(double[])
Serializes an array of unmanaged values to the internal buffer.
Declaration
public void PutArray(double[] value)
Parameters
| Type | Name | Description |
|---|---|---|
| double[] | value |
PutArray(short[])
Serializes an array of unmanaged values to the internal buffer.
Declaration
public void PutArray(short[] value)
Parameters
| Type | Name | Description |
|---|---|---|
| short[] | value |
PutArray(int[])
Serializes an array of unmanaged values to the internal buffer.
Declaration
public void PutArray(int[] value)
Parameters
| Type | Name | Description |
|---|---|---|
| int[] | value |
PutArray(long[])
Serializes an array of unmanaged values to the internal buffer.
Declaration
public void PutArray(long[] value)
Parameters
| Type | Name | Description |
|---|---|---|
| long[] | value |
PutArray(float[])
Serializes an array of unmanaged values to the internal buffer.
Declaration
public void PutArray(float[] value)
Parameters
| Type | Name | Description |
|---|---|---|
| float[] | value |
PutArray(string[])
Serializes an array of string values.
Declaration
public void PutArray(string[] value)
Parameters
| Type | Name | Description |
|---|---|---|
| string[] | value | The array of string elements to write. |
Remarks
PutArray(string[], int)
Serializes an array of string values with a maximum length constraint per element.
Declaration
public void PutArray(string[] value, int strMaxLength)
Parameters
| Type | Name | Description |
|---|---|---|
| string[] | value | The array of string elements to write. |
| int | strMaxLength | The maximum allowed length for each individual string. |
PutArray(ushort[])
Serializes an array of unmanaged values to the internal buffer.
Declaration
public void PutArray(ushort[] value)
Parameters
| Type | Name | Description |
|---|---|---|
| ushort[] | value |
PutArray(uint[])
Serializes an array of unmanaged values to the internal buffer.
Declaration
public void PutArray(uint[] value)
Parameters
| Type | Name | Description |
|---|---|---|
| uint[] | value |
PutArray(ulong[])
Serializes an array of unmanaged values to the internal buffer.
Declaration
public void PutArray(ulong[] value)
Parameters
| Type | Name | Description |
|---|---|---|
| ulong[] | value |
PutArray<T>(T[])
Serializes an array of objects implementing INetSerializable.
Declaration
public void PutArray<T>(T[] value) where T : INetSerializable, new()
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | value | The array of objects to serialize. |
Type Parameters
| Name | Description |
|---|---|
| T | A type that implements INetSerializable and has a parameterless constructor. |
PutBytesWithLength(byte[])
Declaration
public void PutBytesWithLength(byte[] data)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | data | The source array. |
PutBytesWithLength(byte[], int, ushort)
Declaration
public void PutBytesWithLength(byte[] data, int offset, ushort length)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | data | The source byte array. |
| int | offset | The starting index in the source array. |
| ushort | length | The number of bytes to write. |
PutEnum<T>(T)
Writes an enum value of type T to the internal data buffer at the current position.
Automatically resizes the buffer if LiteNetLib.Utils.NetDataWriter._autoResize is enabled.
Advances the position by the size of T.
Declaration
public void PutEnum<T>(T value) where T : unmanaged, Enum
Parameters
| Type | Name | Description |
|---|---|---|
| T | value | The enum value to write. |
Type Parameters
| Name | Description |
|---|---|
| T | An unmanaged enum type to write. |
PutLargeString(string)
Declaration
public void PutLargeString(string value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value | The string to write. |
Remarks
Recommended for strings that may exceed the 65535 byte limit of standard ushort length headers.
Uses UTF8.
PutNullableUnmanaged<T>(T?)
Writes a nullable value of type T into the internal byte buffer at the current position,
first writing a bool indicating whether the value is present,
and then writing the value itself if it exists.
Advances the position by 1 byte for the presence flag plus
the size of T if the value is present.
Declaration
public void PutNullableUnmanaged<T>(T? value) where T : unmanaged
Parameters
| Type | Name | Description |
|---|---|---|
| T? | value | The nullable value to write into the buffer. If null, only a false flag is written. |
Type Parameters
| Name | Description |
|---|---|
| T | An unmanaged value type to write into the buffer. |
PutSBytesWithLength(sbyte[])
Declaration
public void PutSBytesWithLength(sbyte[] data)
Parameters
| Type | Name | Description |
|---|---|---|
| sbyte[] | data | The source array. |
PutSBytesWithLength(sbyte[], int, ushort)
Declaration
public void PutSBytesWithLength(sbyte[] data, int offset, ushort length)
Parameters
| Type | Name | Description |
|---|---|---|
| sbyte[] | data | The source sbyte array. |
| int | offset | The starting index in the source array. |
| ushort | length | The number of elements to write. |
PutSpan<T>(Span<T>)
Serializes a Span<T> of unmanaged values to the internal buffer.
Declaration
public void PutSpan<T>(Span<T> span) where T : unmanaged
Parameters
| Type | Name | Description |
|---|---|---|
| Span<T> | span | The span of data to write. |
Type Parameters
| Name | Description |
|---|---|
| T | The unmanaged type of the span elements. |
Remarks
Writes a 2-byte ushort length header followed by the raw binary data.
PutUnmanagedArray<T>(T[])
Serializes an array of unmanaged values.
Declaration
public void PutUnmanagedArray<T>(T[] arr) where T : unmanaged
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | arr | The array to serialize. |
Type Parameters
| Name | Description |
|---|---|
| T | The unmanaged type of the array elements. |
PutUnmanaged<T>(T)
Writes a value of type T into the internal byte buffer at the current position,
advancing the position by the size of T.
Declaration
public void PutUnmanaged<T>(T value) where T : unmanaged
Parameters
| Type | Name | Description |
|---|---|---|
| T | value | The value to write into the buffer. |
Type Parameters
| Name | Description |
|---|---|
| T | An unmanaged value type to write into the buffer. |
Put<T>(T)
Serializes an object implementing INetSerializable.
Declaration
public void Put<T>(T obj) where T : INetSerializable
Parameters
| Type | Name | Description |
|---|---|---|
| T | obj | The object instance to serialize. |
Type Parameters
| Name | Description |
|---|---|
| T | A type that implements the INetSerializable interface. |
Remarks
This method calls the Serialize(NetDataWriter) method on the provided obj.
Reset()
Resets the _position to 0, effectively clearing the buffer for reuse.
Declaration
public void Reset()
Reset(int)
Resets the _position to 0 and ensures the internal buffer has at least the specified size.
Declaration
public void Reset(int size)
Parameters
| Type | Name | Description |
|---|---|---|
| int | size | The minimum capacity required for the internal buffer. |
Remarks
If the current buffer is smaller than size, ResizeIfNeed(int) will allocate a larger byte array.
ResizeIfNeed(int)
Ensures the internal buffer is at least newSize.
Declaration
public void ResizeIfNeed(int newSize)
Parameters
| Type | Name | Description |
|---|---|---|
| int | newSize | The required minimum size of the buffer. |
Remarks
If an allocation is necessary, the buffer grows to either newSize
or doubles its current size, whichever is larger.
SetPosition(int)
Sets position of NetDataWriter to rewrite previous values
Declaration
public int SetPosition(int position)
Parameters
| Type | Name | Description |
|---|---|---|
| int | position | new byte position |
Returns
| Type | Description |
|---|---|
| int | previous position of data writer |