Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


DataStreamReader

Reads data serialized by a DataStreamWriter.
Read time 6 minutesLast updated 6 days ago

Definition

  • Type: Struct
  • Namespace: Unity.Collections
  • Assembly: UnityEngine.ManagedKernelModule
public struct DataStreamReader

Remarks

The DataStreamReader class is the counterpart of the DataStreamWriter class and can be used to deserialize data which was prepared with it.
Data is stored in the endian format native to the current machine architecture.
For network byte order use the so named methods.
Simple usage example:
using (var data = new NativeArray\<byte\>(16, Allocator.Persistent)) { var dataWriter = new DataStreamWriter(data); dataWriter.WriteInt(42); dataWriter.WriteInt(1234); dataWriter.Flush(); var dataReader = new DataStreamReader(dataWriter.AsNativeArray()); var myFirstInt = dataReader.ReadInt(); var mySecondInt = dataReader.ReadInt(); }
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

Constructor

Description

DataStreamReader(Unity.Collections.NativeArray{System.Byte})Initializes a new instance of the DataStreamReader struct with a NativeArray\<byte\>

Properties

Property

Description

HasFailedReadsIf there is a read failure this returns true. A read failure might happen if this attempts to read more than there is capacity for.
IsCreatedTrue if the reader has been pointed to a valid buffer space. This would be false if the reader was created with no arguments.
LengthThe total size of the buffer space this reader is working with.

Static Properties

Property

Description

IsLittleEndianShow the byte order in which the current computer architecture stores data.

Methods

Method

Description

FlushAligns the read pointer to the next byte-aligned position. Does nothing if already aligned.
GetBitsReadGets the number of bits read from the data stream.
GetBytesReadGets the number of bytes read from the data stream.
ReadByteReads an unsigned byte from the current stream and advances the current position of the stream by one byte.
ReadBytesRead and copy data into the given
Span
of bytes. An error will be logged if not enough bytes are available to fill the array, and DataStreamReader.HasFailedReads will then be true.
ReadDoubleReads a 8-byte floating point value from the current stream and advances the current position of the stream by four bytes.
ReadFixedStringRead and copy data into the given NativeArray of bytes, an error will be logged if not enough bytes are available in the array.
ReadFixedString128Reads a
FixedString128Bytes
value from the current stream and advances the current position of the stream by the length of the string.
ReadFixedString32Reads a
FixedString32Bytes
value from the current stream and advances the current position of the stream by the length of the string.
ReadFixedString4096Reads a
FixedString4096Bytes
value from the current stream and advances the current position of the stream by the length of the string.
ReadFixedString512Reads a
FixedString512Bytes
value from the current stream and advances the current position of the stream by the length of the string.
ReadFixedString64Reads a
FixedString64Bytes
value from the current stream and advances the current position of the stream by the length of the string.
ReadFloatReads a 4-byte floating point value from the current stream and advances the current position of the stream by four bytes.
ReadIntReads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes.
ReadIntNetworkByteOrderReads 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.
ReadLongReads an 8-byte signed long from the stream and advances the current position of the stream by eight bytes.
ReadPackedDoubleReads a 8-byte floating point value from the data stream using a StreamCompressionModel.
ReadPackedDoubleDeltaReads a 8-byte floating point value from the data stream.
ReadPackedFixedString128DeltaReads a
FixedString128Bytes
delta value to the data stream using a StreamCompressionModel.
ReadPackedFixedString32DeltaReads a
FixedString32Bytes
delta value to the data stream using a StreamCompressionModel.
ReadPackedFixedString4096DeltaReads a
FixedString4096Bytes
delta value to the data stream using a StreamCompressionModel.
ReadPackedFixedString512DeltaReads a
FixedString512Bytes
delta value to the data stream using a StreamCompressionModel.
ReadPackedFixedString64DeltaReads a
FixedString64Bytes
delta value to the data stream using a StreamCompressionModel.
ReadPackedFixedStringDeltaRead and copy data into the given NativeArray of bytes, an error will be logged if not enough bytes are available in the array.
ReadPackedFloatReads a 4-byte floating point value from the data stream using a StreamCompressionModel.
ReadPackedFloatDeltaReads a 4-byte floating point value from the data stream.
ReadPackedIntReads a 4-byte signed integer value from the data stream using a StreamCompressionModel.
ReadPackedIntDeltaReads a 4-byte signed integer delta value from the data stream using a StreamCompressionModel.
ReadPackedLongReads an 8-byte signed long value from the data stream using a StreamCompressionModel.
ReadPackedLongDeltaReads an 8-byte signed long delta value from the data stream using a StreamCompressionModel.
ReadPackedUIntReads 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.
ReadPackedUIntDeltaReads a 4-byte unsigned integer delta value from the data stream using a StreamCompressionModel.
ReadPackedULongReads an 8-byte unsigned long value from the data stream using a StreamCompressionModel.
ReadPackedULongDeltaReads an 8-byte unsigned long delta value from the data stream using a StreamCompressionModel.
ReadRawBitsReads a specified number of bits from the data stream.
ReadShortReads a 2-byte signed short from the current stream and advances the current position of the stream by two bytes.
ReadShortNetworkByteOrderReads 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.
ReadUIntReads a 4-byte unsigned integer from the current stream and advances the current position of the stream by four bytes.
ReadUIntNetworkByteOrderReads 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.
ReadULongReads an 8-byte unsigned long from the stream and advances the current position of the stream by eight bytes.
ReadUShortReads a 2-byte unsigned short from the current stream and advances the current position of the stream by two bytes.
ReadUShortNetworkByteOrderReads 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.
SeekSetSets the current position of this stream to the given value. An error will be logged if
pos
is outside the length of the stream.