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
Assembly: Unity.Networking.Transport.dll
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*, int)
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.  | 
      
| int | 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 bool HasFailedReads { get; }
  Property Value
| Type | Description | 
|---|---|
| bool | 
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 bool IsCreated { get; }
  Property Value
| Type | Description | 
|---|---|
| bool | 
IsLittleEndian
Show the byte order in which the current computer architecture stores data.
Declaration
public bool IsLittleEndian { get; }
  Property Value
| Type | Description | 
|---|---|
| bool | 
Remarks
Different computer architectures store data using different byte orders.
- Big-endian: the most significant byte is at the left end of a word.
 - Little-endian: means the most significant byte is at the right end of a word.
 
Length
The total size of the buffer space this reader is working with.
Declaration
public int Length { get; }
  Property Value
| Type | Description | 
|---|---|
| int | 
Methods
GetBitsRead()
Gets the number of bits read from the data stream.
Declaration
public int GetBitsRead()
  Returns
| Type | Description | 
|---|---|
| int | Number of bits read.  | 
      
GetBytesRead()
Gets the number of bytes read from the data stream.
Declaration
public int GetBytesRead()
  Returns
| Type | Description | 
|---|---|
| int | Number of bytes read.  | 
      
GetUnsafeReadOnlyPtr()
Get a pointer to the stream's data. Note that the pointer always points at the beginning of the data, no matter how much was read from the stream.
Declaration
public void* GetUnsafeReadOnlyPtr()
  Returns
| Type | Description | 
|---|---|
| void* | A pointer to the stream's data.  | 
      
Remarks
Performs a job safety check for read-only access.
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*, int)
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.  | 
      
| int | 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*, int)
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.  | 
      
| int | maxLength | Length of the buffer to write the string bytes to.  | 
      
Returns
| Type | Description | 
|---|---|
| ushort | 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 | 
|---|---|
| float | 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 | 
|---|---|
| int | 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 | 
|---|---|
| int | 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 | 
|---|---|
| long | 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*, int, byte*, ushort, 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.  | 
      
| int | maxLength | Length of the buffer to write the string bytes to.  | 
      
| byte* | baseData | Pointer to the previous value, used to compute the diff.  | 
      
| ushort | 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 | 
|---|---|
| ushort | 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 | 
|---|---|
| float | A 4-byte floating point value read from the current stream, or 0 if the end of the stream has been reached.  | 
      
ReadPackedFloatDelta(float, 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 | 
|---|---|---|
| float | baseline | The previous 4-byte floating point value.  | 
      
| NetworkCompressionModel | model | Not currently used.  | 
      
Returns
| Type | Description | 
|---|---|
| float | 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 | 
|---|---|
| int | A 4-byte signed integer read from the current stream, or 0 if the end of the stream has been reached.  | 
      
ReadPackedIntDelta(int, 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 | 
|---|---|---|
| int | 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 | 
|---|---|
| int | 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 | 
|---|---|
| long | An 8-byte signed long read from the current stream, or 0 if the end of the stream has been reached.  | 
      
ReadPackedLongDelta(long, 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 | 
|---|---|---|
| long | 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 | 
|---|---|
| long | 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 | 
|---|---|
| uint | A 4-byte unsigned integer read from the current stream, or 0 if the end of the stream has been reached.  | 
      
ReadPackedUIntDelta(uint, 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 | 
|---|---|---|
| uint | 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 | 
|---|---|
| uint | 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 | 
|---|---|
| ulong | An 8-byte unsigned long read from the current stream, or 0 if the end of the stream has been reached.  | 
      
ReadPackedULongDelta(ulong, 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 | 
|---|---|---|
| ulong | 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 | 
|---|---|
| ulong | 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(int)
Reads a specified number of bits from the data stream.
Declaration
public uint ReadRawBits(int numbits)
  Parameters
| Type | Name | Description | 
|---|---|---|
| int | numbits | A positive number of bytes to write.  | 
      
Returns
| Type | Description | 
|---|---|
| uint | 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 | 
|---|---|
| short | 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 | 
|---|---|
| short | 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 | 
|---|---|
| uint | 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 | 
|---|---|
| uint | 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 | 
|---|---|
| ulong | 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 | 
|---|---|
| ushort | 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 | 
|---|---|
| ushort | A 2-byte unsigned short read from the current stream, or 0 if the end of the stream has been reached.  | 
      
SeekSet(int)
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 | 
|---|---|---|
| int | pos | Seek position.  |