Struct DataStreamReader
The DataStreamReader
class is the counterpart of the
DataStreamWriter
class and can be be used to deserialize
data which was prepared with it.
Inherited Members
Namespace: Unity.Networking.Transport
Syntax
public struct DataStreamReader
Remarks
Simple usage example:
using (var dataWriter = new DataStreamWriter(16, Allocator.Persistent))
{
dataWriter.Write(42);
dataWriter.Write(1234);
// Length is the actual amount of data inside the writer,
// Capacity is the total amount.
var dataReader = new DataStreamReader(dataWriter, 0, dataWriter.Length);
var context = default(DataStreamReader.Context);
var myFirstInt = dataReader.ReadInt(ref context);
var mySecondInt = dataReader.ReadInt(ref context);
}
The DataStreamReader
carries the position of the read pointer inside the struct,
taking a copy of the reader will also copy the read position. This includes passing the
reader to a method by value instead of by ref.
See the DataStreamWriter class for more information and examples.
Constructors
DataStreamReader(Byte*, Int32)
Initializes a new instance of the DataStreamReader struct with a pointer and length.
Declaration
public DataStreamReader(byte *data, int length)
Parameters
Type | Name | Description |
---|---|---|
Byte* | data | Pointer to the buffer to attach to the DataStreamReader. |
Int32 | length | Length of the buffer to attach to the DataStreamReader. |
DataStreamReader(NativeArray<Byte>)
Initializes a new instance of the DataStreamReader struct with a NativeArray<byte>.
Declaration
public DataStreamReader(NativeArray<byte> array)
Parameters
Type | Name | Description |
---|---|---|
NativeArray<Byte> | array | The buffer to attach to the DataStreamReader. |
Properties
HasFailedReads
If there is a read failure this returns true. A read failure might happen if this attempts to read more than there is capacity for.
Declaration
public readonly bool HasFailedReads { get; }
Property Value
Type | Description |
---|---|
Boolean |
IsCreated
True if the reader has been pointed to a valid buffer space. This would be false if the reader was created with no arguments.
Declaration
public readonly bool IsCreated { get; }
Property Value
Type | Description |
---|---|
Boolean |
IsLittleEndian
Show the byte order in which the current computer architecture stores data.
Declaration
public readonly bool IsLittleEndian { get; }
Property Value
Type | Description |
---|---|
Boolean |
Remarks
Different computer architectures store data using different byte orders.
Length
The total size of the buffer space this reader is working with.
Declaration
public readonly int Length { get; }
Property Value
Type | Description |
---|---|
Int32 |
Methods
GetBitsRead()
Gets the number of bits read from the data stream.
Declaration
public int GetBitsRead()
Returns
Type | Description |
---|---|
Int32 | Number of bits read. |
GetBytesRead()
Gets the number of bytes read from the data stream.
Declaration
public int GetBytesRead()
Returns
Type | Description |
---|---|
Int32 | Number of bytes read. |
ReadByte()
Reads an unsigned byte from the current stream and advances the current position of the stream by one byte.
Declaration
public byte ReadByte()
Returns
Type | Description |
---|---|
Byte | The next byte read from the current stream, or 0 if the end of the stream has been reached. |
ReadBytes(Byte*, Int32)
Read the requested number of bytes to the given pointer.
Declaration
public void ReadBytes(byte *data, int length)
Parameters
Type | Name | Description |
---|---|---|
Byte* | data | Pointer to write the data to. |
Int32 | length | Number of bytes to read. |
ReadBytes(NativeArray<Byte>)
Read and copy data into the given NativeArray of bytes. The number of bytes read is the length of the provided array.
Declaration
public void ReadBytes(NativeArray<byte> array)
Parameters
Type | Name | Description |
---|---|---|
NativeArray<Byte> | array | Array to read the data into. |
ReadFixedString(Byte*, Int32)
Read and copy a fixed string (of unknown length) into the given buffer.
Declaration
public ushort ReadFixedString(byte *data, int maxLength)
Parameters
Type | Name | Description |
---|---|---|
Byte* | data | Pointer to the buffer to write the string bytes to. |
Int32 | maxLength | Length of the buffer to write the string bytes to. |
Returns
Type | Description |
---|---|
UInt16 | Length of data read into byte array, or zero if error occurred. |
ReadFixedString128()
Reads a FixedString128Bytes
value from the current stream and advances the current position of the stream by the length of the string.
Declaration
public FixedString128Bytes ReadFixedString128()
Returns
Type | Description |
---|---|
FixedString128Bytes | A |
ReadFixedString32()
Reads a FixedString32Bytes
value from the current stream and advances the current position of the stream by the length of the string.
Declaration
public FixedString32Bytes ReadFixedString32()
Returns
Type | Description |
---|---|
FixedString32Bytes | A |
ReadFixedString4096()
Reads a FixedString4096Bytes
value from the current stream and advances the current position of the stream by the length of the string.
Declaration
public FixedString4096Bytes ReadFixedString4096()
Returns
Type | Description |
---|---|
FixedString4096Bytes | A |
ReadFixedString512()
Reads a FixedString512Bytes
value from the current stream and advances the current position of the stream by the length of the string.
Declaration
public FixedString512Bytes ReadFixedString512()
Returns
Type | Description |
---|---|
FixedString512Bytes | A |
ReadFixedString64()
Reads a FixedString64Bytes
value from the current stream and advances the current position of the stream by the length of the string.
Declaration
public FixedString64Bytes ReadFixedString64()
Returns
Type | Description |
---|---|
FixedString64Bytes | A |
ReadFloat()
Reads a 4-byte floating point value from the current stream and advances the current position of the stream by four bytes.
Declaration
public float ReadFloat()
Returns
Type | Description |
---|---|
Single | A 4-byte floating point value read from the current stream, or 0 if the end of the stream has been reached. |
ReadInt()
Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes.
Declaration
public int ReadInt()
Returns
Type | Description |
---|---|
Int32 | A 4-byte signed integer read from the current stream, or 0 if the end of the stream has been reached. |
ReadIntNetworkByteOrder()
Reads a 4-byte signed integer from the current stream in Big-endian byte order and advances the current position of the stream by four bytes. If the current endianness is in little-endian order, the byte order will be swapped.
Declaration
public int ReadIntNetworkByteOrder()
Returns
Type | Description |
---|---|
Int32 | A 4-byte signed integer read from the current stream, or 0 if the end of the stream has been reached. |
ReadLong()
Reads an 8-byte signed long from the stream and advances the current position of the stream by eight bytes.
Declaration
public long ReadLong()
Returns
Type | Description |
---|---|
Int64 | An 8-byte signed long read from the current stream, or 0 if the end of the stream has been reached. |
ReadPackedFixedString128Delta(FixedString128Bytes, NetworkCompressionModel)
Reads a FixedString128Bytes
delta value to the data stream using a NetworkCompressionModel.
Declaration
public FixedString128Bytes ReadPackedFixedString128Delta(FixedString128Bytes baseline, NetworkCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
FixedString128Bytes | baseline | The previous |
NetworkCompressionModel | model | NetworkCompressionModel model for writing value in a packed manner. |
Returns
Type | Description |
---|---|
FixedString128Bytes | A |
ReadPackedFixedString32Delta(FixedString32Bytes, NetworkCompressionModel)
Reads a FixedString32Bytes
delta value to the data stream using a NetworkCompressionModel.
Declaration
public FixedString32Bytes ReadPackedFixedString32Delta(FixedString32Bytes baseline, NetworkCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
FixedString32Bytes | baseline | The previous |
NetworkCompressionModel | model | NetworkCompressionModel model for writing value in a packed manner. |
Returns
Type | Description |
---|---|
FixedString32Bytes | A |
ReadPackedFixedString4096Delta(FixedString4096Bytes, NetworkCompressionModel)
Reads a FixedString4096Bytes
delta value to the data stream using a NetworkCompressionModel.
Declaration
public FixedString4096Bytes ReadPackedFixedString4096Delta(FixedString4096Bytes baseline, NetworkCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
FixedString4096Bytes | baseline | The previous |
NetworkCompressionModel | model | NetworkCompressionModel model for writing value in a packed manner. |
Returns
Type | Description |
---|---|
FixedString4096Bytes | A |
ReadPackedFixedString512Delta(FixedString512Bytes, NetworkCompressionModel)
Reads a FixedString512Bytes
delta value to the data stream using a NetworkCompressionModel.
Declaration
public FixedString512Bytes ReadPackedFixedString512Delta(FixedString512Bytes baseline, NetworkCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
FixedString512Bytes | baseline | The previous |
NetworkCompressionModel | model | NetworkCompressionModel model for writing value in a packed manner. |
Returns
Type | Description |
---|---|
FixedString512Bytes | A |
ReadPackedFixedString64Delta(FixedString64Bytes, NetworkCompressionModel)
Reads a FixedString64Bytes
delta value to the data stream using a NetworkCompressionModel.
Declaration
public FixedString64Bytes ReadPackedFixedString64Delta(FixedString64Bytes baseline, NetworkCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
FixedString64Bytes | baseline | The previous |
NetworkCompressionModel | model | NetworkCompressionModel model for writing value in a packed manner. |
Returns
Type | Description |
---|---|
FixedString64Bytes | A |
ReadPackedFixedStringDelta(Byte*, Int32, Byte*, UInt16, NetworkCompressionModel)
Read and copy a fixed string delta (of unknown length) into the given buffer.
Declaration
public ushort ReadPackedFixedStringDelta(byte *data, int maxLength, byte *baseData, ushort baseLength, NetworkCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
Byte* | data | Pointer to the buffer to write the string bytes to. |
Int32 | maxLength | Length of the buffer to write the string bytes to. |
Byte* | baseData | Pointer to the previous value, used to compute the diff. |
UInt16 | baseLength | Length of the previous value, used to compute the diff. |
NetworkCompressionModel | model | NetworkCompressionModel model for writing value in a packed manner. |
Returns
Type | Description |
---|---|
UInt16 | Length of data read into byte array, or zero if error occurred. |
ReadPackedFloat(NetworkCompressionModel)
Reads a 4-byte floating point value from the data stream using a NetworkCompressionModel.
Declaration
public float ReadPackedFloat(NetworkCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
NetworkCompressionModel | model | NetworkCompressionModel model for reading value in a packed manner. |
Returns
Type | Description |
---|---|
Single | A 4-byte floating point value read from the current stream, or 0 if the end of the stream has been reached. |
ReadPackedFloatDelta(Single, NetworkCompressionModel)
Reads a 4-byte floating point value from the data stream.
If the first bit is 0, the data did not change and baseline
will be returned.
Declaration
public float ReadPackedFloatDelta(float baseline, NetworkCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
Single | baseline | The previous 4-byte floating point value. |
NetworkCompressionModel | model | Not currently used. |
Returns
Type | Description |
---|---|
Single | A 4-byte floating point value read from the current stream, or |
ReadPackedInt(NetworkCompressionModel)
Reads a 4-byte signed integer value from the data stream using a NetworkCompressionModel.
Negative values de-interleaves from positive values before returning, for example (0, -1, 1, -2, 2) -> (-2, -1, 0, 1, 2)
Declaration
public int ReadPackedInt(NetworkCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
NetworkCompressionModel | model | NetworkCompressionModel model for reading value in a packed manner. |
Returns
Type | Description |
---|---|
Int32 | A 4-byte signed integer read from the current stream, or 0 if the end of the stream has been reached. |
ReadPackedIntDelta(Int32, NetworkCompressionModel)
Reads a 4-byte signed integer delta value from the data stream using a NetworkCompressionModel.
Declaration
public int ReadPackedIntDelta(int baseline, NetworkCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
Int32 | baseline | The previous 4-byte signed integer value, used to compute the diff. |
NetworkCompressionModel | model | NetworkCompressionModel model for reading value in a packed manner. |
Returns
Type | Description |
---|---|
Int32 | A 4-byte signed integer read from the current stream, or 0 if the end of the stream has been reached.
If the data did not change, this also returns 0.
|
ReadPackedLong(NetworkCompressionModel)
Reads an 8-byte signed long value from the data stream using a NetworkCompressionModel.
Negative values de-interleaves from positive values before returning, for example (0, -1, 1, -2, 2) -> (-2, -1, 0, 1, 2)
Declaration
public long ReadPackedLong(NetworkCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
NetworkCompressionModel | model | NetworkCompressionModel model for reading value in a packed manner. |
Returns
Type | Description |
---|---|
Int64 | An 8-byte signed long read from the current stream, or 0 if the end of the stream has been reached. |
ReadPackedLongDelta(Int64, NetworkCompressionModel)
Reads an 8-byte signed long delta value from the data stream using a NetworkCompressionModel.
Declaration
public long ReadPackedLongDelta(long baseline, NetworkCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
Int64 | baseline | The previous 8-byte signed long value, used to compute the diff. |
NetworkCompressionModel | model | NetworkCompressionModel model for reading value in a packed manner. |
Returns
Type | Description |
---|---|
Int64 | An 8-byte signed long read from the current stream, or 0 if the end of the stream has been reached.
If the data did not change, this also returns 0.
|
ReadPackedUInt(NetworkCompressionModel)
Reads a 4-byte unsigned integer from the current stream using a NetworkCompressionModel and advances the current position the number of bits depending on the model.
Declaration
public uint ReadPackedUInt(NetworkCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
NetworkCompressionModel | model | NetworkCompressionModel model for reading value in a packed manner. |
Returns
Type | Description |
---|---|
UInt32 | A 4-byte unsigned integer read from the current stream, or 0 if the end of the stream has been reached. |
ReadPackedUIntDelta(UInt32, NetworkCompressionModel)
Reads a 4-byte unsigned integer delta value from the data stream using a NetworkCompressionModel.
Declaration
public uint ReadPackedUIntDelta(uint baseline, NetworkCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
UInt32 | baseline | The previous 4-byte unsigned integer value, used to compute the diff. |
NetworkCompressionModel | model | NetworkCompressionModel model for reading value in a packed manner. |
Returns
Type | Description |
---|---|
UInt32 | A 4-byte unsigned integer read from the current stream, or 0 if the end of the stream has been reached.
If the data did not change, this also returns 0.
|
ReadPackedULong(NetworkCompressionModel)
Reads an 8-byte unsigned long value from the data stream using a NetworkCompressionModel.
Declaration
public ulong ReadPackedULong(NetworkCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
NetworkCompressionModel | model | NetworkCompressionModel model for reading value in a packed manner. |
Returns
Type | Description |
---|---|
UInt64 | An 8-byte unsigned long read from the current stream, or 0 if the end of the stream has been reached. |
ReadPackedULongDelta(UInt64, NetworkCompressionModel)
Reads an 8-byte unsigned long delta value from the data stream using a NetworkCompressionModel.
Declaration
public ulong ReadPackedULongDelta(ulong baseline, NetworkCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
UInt64 | baseline | The previous 8-byte unsigned long value, used to compute the diff. |
NetworkCompressionModel | model | NetworkCompressionModel model for reading value in a packed manner. |
Returns
Type | Description |
---|---|
UInt64 | An 8-byte unsigned long read from the current stream, or 0 if the end of the stream has been reached.
If the data did not change, this also returns 0.
|
ReadRawBits(Int32)
Reads a specified number of bits from the data stream.
Declaration
public uint ReadRawBits(int numbits)
Parameters
Type | Name | Description |
---|---|---|
Int32 | numbits | A positive number of bytes to write. |
Returns
Type | Description |
---|---|
UInt32 | A 4-byte unsigned integer read from the current stream, or 0 if the end of the stream has been reached. |
ReadShort()
Reads a 2-byte signed short from the current stream and advances the current position of the stream by two bytes.
Declaration
public short ReadShort()
Returns
Type | Description |
---|---|
Int16 | A 2-byte signed short read from the current stream, or 0 if the end of the stream has been reached. |
ReadShortNetworkByteOrder()
Reads a 2-byte signed short from the current stream in Big-endian byte order and advances the current position of the stream by two bytes. If the current endianness is in little-endian order, the byte order will be swapped.
Declaration
public short ReadShortNetworkByteOrder()
Returns
Type | Description |
---|---|
Int16 | A 2-byte signed short read from the current stream, or 0 if the end of the stream has been reached. |
ReadUInt()
Reads a 4-byte unsigned integer from the current stream and advances the current position of the stream by four bytes.
Declaration
public uint ReadUInt()
Returns
Type | Description |
---|---|
UInt32 | A 4-byte unsigned integer read from the current stream, or 0 if the end of the stream has been reached. |
ReadUIntNetworkByteOrder()
Reads a 4-byte unsigned integer from the current stream in Big-endian byte order and advances the current position of the stream by four bytes. If the current endianness is in little-endian order, the byte order will be swapped.
Declaration
public uint ReadUIntNetworkByteOrder()
Returns
Type | Description |
---|---|
UInt32 | A 4-byte unsigned integer read from the current stream, or 0 if the end of the stream has been reached. |
ReadULong()
Reads an 8-byte unsigned long from the stream and advances the current position of the stream by eight bytes.
Declaration
public ulong ReadULong()
Returns
Type | Description |
---|---|
UInt64 | An 8-byte unsigned long read from the current stream, or 0 if the end of the stream has been reached. |
ReadUShort()
Reads a 2-byte unsigned short from the current stream and advances the current position of the stream by two bytes.
Declaration
public ushort ReadUShort()
Returns
Type | Description |
---|---|
UInt16 | A 2-byte unsigned short read from the current stream, or 0 if the end of the stream has been reached. |
ReadUShortNetworkByteOrder()
Reads a 2-byte unsigned short from the current stream in Big-endian byte order and advances the current position of the stream by two bytes. If the current endianness is in little-endian order, the byte order will be swapped.
Declaration
public ushort ReadUShortNetworkByteOrder()
Returns
Type | Description |
---|---|
UInt16 | A 2-byte unsigned short read from the current stream, or 0 if the end of the stream has been reached. |
SeekSet(Int32)
Sets the current position of this stream to the given value.
An error will be logged if pos
is outside the length of the stream.
In addition this will reset the bit index and the bit buffer.
Declaration
public void SeekSet(int pos)
Parameters
Type | Name | Description |
---|---|---|
Int32 | pos | Seek position. |