Struct DataStreamReader
Writes data in an endian format to deserialize data.
Namespace: Unity.Collections
Assembly: Unity.Collections.dll
Syntax
[MovedFrom(true, "Unity.Networking.Transport", null, null)]
public struct DataStreamReader
Remarks
The DataStreamReader class is the counterpart of the DataStreamWriter class and can be be used to deserialize data which was prepared with it.
DataStreamWriter writes this data in the endian format native
to the current machine architecture.
For network byte order use the so named methods.
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);
}
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.
Constructors
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 |
---|---|
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 readonly bool IsCreated { get; }
Property Value
Type | Description |
---|---|
bool |
IsLittleEndian
Show the byte order in which the current computer architecture stores data.
Declaration
public static 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 readonly 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. |
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(NativeArray<byte>)
Read and copy data into the given NativeArray of bytes, an error will be logged if not enough bytes are available.
Declaration
public void ReadBytes(NativeArray<byte> array)
Parameters
Type | Name | Description |
---|---|---|
NativeArray<byte> | array |
ReadDouble()
Reads a 8-byte floating point value from the current stream and advances the current position of the stream by four bytes.
Declaration
public double ReadDouble()
Returns
Type | Description |
---|---|
double | A 8-byte floating point value read from the current stream, or 0 if the end of the stream has been reached. |
ReadFixedString(NativeArray<byte>)
Read and copy data into the given NativeArray of bytes, an error will be logged if not enough bytes are available in the array.
Declaration
public ushort ReadFixedString(NativeArray<byte> array)
Parameters
Type | Name | Description |
---|---|---|
NativeArray<byte> | array | 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. |
ReadPackedDouble(StreamCompressionModel)
Reads a 8-byte floating point value from the data stream using a StreamCompressionModel.
Declaration
public double ReadPackedDouble(StreamCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
StreamCompressionModel | model | StreamCompressionModel model for reading value in a packed manner. |
Returns
Type | Description |
---|---|
double | A 8-byte floating point value read from the current stream, or 0 if the end of the stream has been reached. |
ReadPackedDoubleDelta(double, StreamCompressionModel)
Reads a 8-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 double ReadPackedDoubleDelta(double baseline, StreamCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
double | baseline | The previous 8-byte floating point value. |
StreamCompressionModel | model | Not currently used. |
Returns
Type | Description |
---|---|
double | A 8-byte floating point value read from the current stream, or |
ReadPackedFixedString128Delta(FixedString128Bytes, StreamCompressionModel)
Reads a FixedString128Bytes
delta value to the data stream using a StreamCompressionModel.
Declaration
public FixedString128Bytes ReadPackedFixedString128Delta(FixedString128Bytes baseline, StreamCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
FixedString128Bytes | baseline | The previous |
StreamCompressionModel | model | StreamCompressionModel model for writing value in a packed manner. |
Returns
Type | Description |
---|---|
FixedString128Bytes | A |
ReadPackedFixedString32Delta(FixedString32Bytes, StreamCompressionModel)
Reads a FixedString32Bytes
delta value to the data stream using a StreamCompressionModel.
Declaration
public FixedString32Bytes ReadPackedFixedString32Delta(FixedString32Bytes baseline, StreamCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
FixedString32Bytes | baseline | The previous |
StreamCompressionModel | model | StreamCompressionModel model for writing value in a packed manner. |
Returns
Type | Description |
---|---|
FixedString32Bytes | A |
ReadPackedFixedString4096Delta(FixedString4096Bytes, StreamCompressionModel)
Reads a FixedString4096Bytes
delta value to the data stream using a StreamCompressionModel.
Declaration
public FixedString4096Bytes ReadPackedFixedString4096Delta(FixedString4096Bytes baseline, StreamCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
FixedString4096Bytes | baseline | The previous |
StreamCompressionModel | model | StreamCompressionModel model for writing value in a packed manner. |
Returns
Type | Description |
---|---|
FixedString4096Bytes | A |
ReadPackedFixedString512Delta(FixedString512Bytes, StreamCompressionModel)
Reads a FixedString512Bytes
delta value to the data stream using a StreamCompressionModel.
Declaration
public FixedString512Bytes ReadPackedFixedString512Delta(FixedString512Bytes baseline, StreamCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
FixedString512Bytes | baseline | The previous |
StreamCompressionModel | model | StreamCompressionModel model for writing value in a packed manner. |
Returns
Type | Description |
---|---|
FixedString512Bytes | A |
ReadPackedFixedString64Delta(FixedString64Bytes, StreamCompressionModel)
Reads a FixedString64Bytes
delta value to the data stream using a StreamCompressionModel.
Declaration
public FixedString64Bytes ReadPackedFixedString64Delta(FixedString64Bytes baseline, StreamCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
FixedString64Bytes | baseline | The previous |
StreamCompressionModel | model | StreamCompressionModel model for writing value in a packed manner. |
Returns
Type | Description |
---|---|
FixedString64Bytes | A |
ReadPackedFixedStringDelta(NativeArray<byte>, NativeArray<byte>, StreamCompressionModel)
Read and copy data into the given NativeArray of bytes, an error will be logged if not enough bytes are available in the array.
Declaration
public ushort ReadPackedFixedStringDelta(NativeArray<byte> data, NativeArray<byte> baseData, StreamCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
NativeArray<byte> | data | Array for the current fixed string. |
NativeArray<byte> | baseData | Array containing the previous value, used to compute the diff. |
StreamCompressionModel | model | StreamCompressionModel 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(StreamCompressionModel)
Reads a 4-byte floating point value from the data stream using a StreamCompressionModel.
Declaration
public float ReadPackedFloat(StreamCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
StreamCompressionModel | model | StreamCompressionModel 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, StreamCompressionModel)
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, StreamCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
float | baseline | The previous 4-byte floating point value. |
StreamCompressionModel | model | Not currently used. |
Returns
Type | Description |
---|---|
float | A 4-byte floating point value read from the current stream, or |
ReadPackedInt(StreamCompressionModel)
Reads a 4-byte signed integer value from the data stream using a StreamCompressionModel.
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(StreamCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
StreamCompressionModel | model | StreamCompressionModel 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, StreamCompressionModel)
Reads a 4-byte signed integer delta value from the data stream using a StreamCompressionModel.
Declaration
public int ReadPackedIntDelta(int baseline, StreamCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
int | baseline | The previous 4-byte signed integer value, used to compute the diff. |
StreamCompressionModel | model | StreamCompressionModel 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(StreamCompressionModel)
Reads an 8-byte signed long value from the data stream using a StreamCompressionModel.
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(StreamCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
StreamCompressionModel | model | StreamCompressionModel 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, StreamCompressionModel)
Reads an 8-byte signed long delta value from the data stream using a StreamCompressionModel.
Declaration
public long ReadPackedLongDelta(long baseline, StreamCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
long | baseline | The previous 8-byte signed long value, used to compute the diff. |
StreamCompressionModel | model | StreamCompressionModel 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(StreamCompressionModel)
Reads a 4-byte unsigned integer from the current stream using a StreamCompressionModel and advances the current position the number of bits depending on the model.
Declaration
public uint ReadPackedUInt(StreamCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
StreamCompressionModel | model | StreamCompressionModel 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, StreamCompressionModel)
Reads a 4-byte unsigned integer delta value from the data stream using a StreamCompressionModel.
Declaration
public uint ReadPackedUIntDelta(uint baseline, StreamCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
uint | baseline | The previous 4-byte unsigned integer value, used to compute the diff. |
StreamCompressionModel | model | StreamCompressionModel 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(StreamCompressionModel)
Reads an 8-byte unsigned long value from the data stream using a StreamCompressionModel.
Declaration
public ulong ReadPackedULong(StreamCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
StreamCompressionModel | model | StreamCompressionModel 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, StreamCompressionModel)
Reads an 8-byte unsigned long delta value from the data stream using a StreamCompressionModel.
Declaration
public ulong ReadPackedULongDelta(ulong baseline, StreamCompressionModel model)
Parameters
Type | Name | Description |
---|---|---|
ulong | baseline | The previous 8-byte unsigned long value, used to compute the diff. |
StreamCompressionModel | model | StreamCompressionModel 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. |