Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


DataStreamWriter

Writes data in an endian format to serialize data.
Read time 6 minutesLast updated 11 days ago

Definition

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

Remarks

Data streams can be used to serialize data (e.g. over the network). The DataStreamWriter and DataStreamReader classes work together to serialize data for sending and then to deserialize when receiving.
DataStreamWriter writes data in the endian format native to the current machine architecture. For network byte order use the so named methods.
The reader can be used to deserialize the data from a NativeArray\<byte\>, writing data to a NativeArray\<byte\> and reading it back can be done like this:
using (var data = new NativeArray\<byte\>(16, Allocator.Persistent)) { var dataWriter = new DataStreamWriter(data); dataWriter.WriteInt(42); dataWriter.WriteInt(1234); // Length is the actual amount of data inside the writer, // Capacity is the total amount. var dataReader = new DataStreamReader(data.GetSubArray(0, dataWriter.Length)); var myFirstInt = dataReader.ReadInt(); var mySecondInt = dataReader.ReadInt(); }
There are a number of functions for various data types. If a copy of the writer is stored it can be used to overwrite the data later on at any bit alignment. This is particularly useful when the size of the data is written at the start and you want to write it at the end when you know the value. DataStreamWriter.IsLittleEndian using (var data = new NativeArray\<byte\>(16, Allocator.Persistent)) { var dataWriter = new DataStreamWriter(data); // My header data var headerSizeMark = dataWriter; dataWriter.WriteUShort((ushort)0); var payloadSizeMark = dataWriter; dataWriter.WriteUShort((ushort)0); dataWriter.WriteInt(42); dataWriter.WriteInt(1234); var headerSize = data.Length; // Update header size to correct value headerSizeMark.WriteUShort((ushort)headerSize); // My payload data dataWriter.WriteInt(99); // Update payload size to correct value payloadSizeMark.WriteUShort((ushort)(dataWriter.Length - headerSize)); }

Fields

Value

Description

m_SendHandleDataUsed for sending data asynchronously.

Constructors

Constructor

Description

DataStreamWriter(System.Byte*,System.Int32)Initializes a new instance of the DataStreamWriter struct with a memory we don't own
DataStreamWriter(System.Int32,Unity.Collections.AllocatorManager.AllocatorHandle)Initializes a new instance of the DataStreamWriter struct.
DataStreamWriter(Unity.Collections.NativeArray{System.Byte})Initializes a new instance of the DataStreamWriter struct with a NativeArray\<byte\>

Properties

Property

Description

CapacityThe total size of the data buffer, see DataStreamWriter.Length for the size of space used in the buffer.
HasFailedWritesIf there is a write failure this returns true. A failure might happen if an attempt is made to write more than there is capacity for.
IsCreatedTrue if there is a valid data buffer present. This would be false if the writer was created with no arguments.
LengthThe size of the buffer used. See DataStreamWriter.Capacity for the total size.
LengthInBitsThe size of the buffer used in bits. See DataStreamWriter.Length for the length in bytes.

Static Properties

Property

Description

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

Methods

Method

Description

AsNativeArrayConvert internal data buffer to NativeArray for use in entities APIs.
ClearMoves the write position to the start of the data buffer used.
FlushAdvances the write position to the next byte boundary, writing zero bits into any padding.
WriteByteWrites an unsigned byte to the current stream and advances the stream position by one byte.
WriteBytesCopy
Span
of bytes into the writer's data buffer.
WriteDoubleWrites a 8-byte floating point value to the data stream.
WriteFixedString128Writes a
FixedString128Bytes
value to the data stream.
WriteFixedString32Writes a
FixedString32Bytes
value to the data stream.
WriteFixedString4096Writes a
FixedString4096Bytes
value to the data stream.
WriteFixedString512Writes a
FixedString512Bytes
value to the data stream.
WriteFixedString64Writes a
FixedString64Bytes
value to the data stream.
WriteFloatWrites a 4-byte floating point value to the data stream.
WriteIntWrites a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes.
WriteIntNetworkByteOrderWrites a 4-byte signed integer from the current stream using Big-endian byte order and advances the current position of the stream by four bytes. If the current machine is in little-endian order, the byte order will be swapped.
WriteLongWrites an 8-byte signed long from the stream and advances the current position of the stream by eight bytes.
WritePackedDoubleWrites a 8-byte floating point value to the data stream using a StreamCompressionModel.
WritePackedDoubleDeltaWrites a 8-byte floating point value to the data stream.
WritePackedFixedString128DeltaWrites a delta
FixedString128Bytes
value to the data stream using a StreamCompressionModel.
WritePackedFixedString32DeltaWrites a
FixedString32Bytes
delta value to the data stream using a StreamCompressionModel.
WritePackedFixedString4096DeltaWrites a delta
FixedString4096Bytes
value to the data stream using a StreamCompressionModel.
WritePackedFixedString512DeltaWrites a delta
FixedString512Bytes
value to the data stream using a StreamCompressionModel.
WritePackedFixedString64DeltaWrites a delta
FixedString64Bytes
value to the data stream using a StreamCompressionModel.
WritePackedFloatWrites a 4-byte floating point value to the data stream using a StreamCompressionModel.
WritePackedFloatDeltaWrites a 4-byte floating point value to the data stream.
WritePackedIntWrites a 4-byte signed integer value to the data stream using a StreamCompressionModel. Negative values are interleaved between positive values, i.e. (0, -1, 1, -2, 2)
WritePackedIntDeltaWrites a delta 4-byte signed integer value to the data stream using a StreamCompressionModel.
WritePackedLongWrites a 8-byte signed long value to the data stream using a StreamCompressionModel.
WritePackedLongDeltaWrites a delta 8-byte signed long value to the data stream using a StreamCompressionModel.
WritePackedUIntWrites a 4-byte unsigned integer value to the data stream using a StreamCompressionModel.
WritePackedUIntDeltaWrites a delta 4-byte unsigned integer value to the data stream using a StreamCompressionModel. Note that the Uint values are cast to an Int after computing the diff.
WritePackedULongWrites an 8-byte unsigned long value to the data stream using a StreamCompressionModel.
WritePackedULongDeltaWrites a delta 8-byte unsigned long value to the data stream using a StreamCompressionModel. Note that the unsigned long values are cast to a signed long after computing the diff.
WriteRawBitsAppends a specified number of bits to the data stream.
WriteShortWrites a 2-byte signed short to the current stream and advances the stream position by two bytes.
WriteShortNetworkByteOrderWrites a 2-byte signed short to the current stream using Big-endian byte order and advances the stream position by two bytes. If the stream is in little-endian order, the byte order will be swapped.
WriteUIntReads a 4-byte unsigned integer from the current stream and advances the current position of the stream by four bytes.
WriteUIntNetworkByteOrderWrites a 4-byte unsigned integer from the current stream using Big-endian byte order and advances the current position of the stream by four bytes. If the stream is in little-endian order, the byte order will be swapped.
WriteULongReads an 8-byte unsigned long from the stream and advances the current position of the stream by eight bytes.
WriteUShortWrites a 2-byte unsigned short to the current stream and advances the stream position by two bytes.
WriteUShortNetworkByteOrderWrites a 2-byte unsigned short to the current stream using Big-endian byte order and advances the stream position by two bytes. If the stream is in little-endian order, the byte order will be swapped.