Version: Unity 6.7 Beta (6000.7)
LanguageEnglish
  • C#

DataStreamWriter

struct in Unity.Collections

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Writes data in an endian format to serialize data.

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)); }

Static Properties

Property Description
IsLittleEndian Show the byte order in which the current computer architecture stores data.

Properties

Property Description
Capacity The total size of the data buffer, see DataStreamWriter.Length for the size of space used in the buffer.
HasFailedWrites If 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.
IsCreated True if there is a valid data buffer present. This would be false if the writer was created with no arguments.
Length The size of the buffer used. See DataStreamWriter.Capacity for the total size.
LengthInBits The size of the buffer used in bits. See DataStreamWriter.Length for the length in bytes.
m_SendHandleData Used for sending data asynchronously.

Constructors

Constructor Description
DataStreamWriter Initializes a new instance of the DataStreamWriter struct.

Public Methods

Method Description
AsNativeArray Convert internal data buffer to NativeArray for use in entities APIs.
Clear Moves the write position to the start of the data buffer used.
Flush Advances the write position to the next byte boundary, writing zero bits into any padding.
WriteByte Writes an unsigned byte to the current stream and advances the stream position by one byte.
WriteBytes Copy NativeArray of bytes into the writer's data buffer.
WriteDouble Writes a 8-byte floating point value to the data stream.
WriteFixedString128 Writes a FixedString128Bytes value to the data stream.
WriteFixedString32 Writes a FixedString32Bytes value to the data stream.
WriteFixedString4096 Writes a FixedString4096Bytes value to the data stream.
WriteFixedString512 Writes a FixedString512Bytes value to the data stream.
WriteFixedString64 Writes a FixedString64Bytes value to the data stream.
WriteFloat Writes a 4-byte floating point value to the data stream.
WriteInt Writes a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes.
WriteIntNetworkByteOrder Writes 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.
WriteLong Writes an 8-byte signed long from the stream and advances the current position of the stream by eight bytes.
WritePackedDouble Writes a 8-byte floating point value to the data stream using a StreamCompressionModel.
WritePackedDoubleDelta Writes a 8-byte floating point value to the data stream. If the data did not change a zero bit is prepended, otherwise a 1 bit is prepended. When reading back the data, the first bit is then checked for whether the data was changed or not.
WritePackedFixedString128Delta Writes a delta FixedString128Bytes value to the data stream using a StreamCompressionModel.
WritePackedFixedString32Delta Writes a FixedString32Bytes delta value to the data stream using a StreamCompressionModel.
WritePackedFixedString4096Delta Writes a delta FixedString4096Bytes value to the data stream using a StreamCompressionModel.
WritePackedFixedString512Delta Writes a delta FixedString512Bytes value to the data stream using a StreamCompressionModel.
WritePackedFixedString64Delta Writes a delta FixedString64Bytes value to the data stream using a StreamCompressionModel.
WritePackedFloat Writes a 4-byte floating point value to the data stream using a StreamCompressionModel.
WritePackedFloatDelta Writes a 4-byte floating point value to the data stream. If the data did not change a zero bit is prepended, otherwise a 1 bit is prepended. When reading back the data, the first bit is then checked for whether the data was changed or not.
WritePackedInt Writes 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)
WritePackedIntDelta Writes a delta 4-byte signed integer value to the data stream using a StreamCompressionModel.
WritePackedLong Writes a 8-byte signed long value to the data stream using a StreamCompressionModel.
WritePackedLongDelta Writes a delta 8-byte signed long value to the data stream using a StreamCompressionModel.
WritePackedUInt Writes a 4-byte unsigned integer value to the data stream using a StreamCompressionModel.
WritePackedUIntDelta Writes 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.
WritePackedULong Writes an 8-byte unsigned long value to the data stream using a StreamCompressionModel.
WritePackedULongDelta Writes 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.
WriteRawBits Appends a specified number of bits to the data stream.
WriteShort Writes a 2-byte signed short to the current stream and advances the stream position by two bytes.
WriteShortNetworkByteOrder Writes 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.
WriteUInt Reads a 4-byte unsigned integer from the current stream and advances the current position of the stream by four bytes.
WriteUIntNetworkByteOrder Writes 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.
WriteULong Reads an 8-byte unsigned long from the stream and advances the current position of the stream by eight bytes.
WriteUShort Writes a 2-byte unsigned short to the current stream and advances the stream position by two bytes.
WriteUShortNetworkByteOrder Writes 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.