Struct DataStreamWriter
Writes data in an endian format to serialize data.
Namespace: Unity.Collections
Assembly: solution.dll
Syntax
[MovedFrom(true, "Unity.Networking.Transport", "Unity.Networking.Transport", null)]
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(nativeArrayOfBytes.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. 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. 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
byte[] someBytes = Encoding.ASCII.GetBytes("some string");
dataWriter.Write(someBytes, someBytes.Length);
// Update payload size to correct value
payloadSizeMark.WriteUShort((ushort)(dataWriter.Length - headerSize));
}
Constructors
Name | Description |
---|---|
DataStreamWriter(byte*, int) | Initializes a new instance of the DataStreamWriter struct with a memory we don't own |
DataStreamWriter(int, AllocatorHandle) | Initializes a new instance of the DataStreamWriter struct. |
DataStreamWriter(NativeArray<byte>) | Initializes a new instance of the DataStreamWriter struct with a NativeArray<byte> |
Fields
Name | Description |
---|---|
m_SendHandleData | Used for sending data asynchronously. |
Properties
Name | Description |
---|---|
Capacity | The total size of the data buffer, see 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. |
IsLittleEndian | Show the byte order in which the current computer architecture stores data. |
Length | The size of the buffer used. See Capacity for the total size. |
LengthInBits | The size of the buffer used in bits. See Length for the length in bytes. |
Methods
Name | 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() | Causes any buffered bits to be written to the data buffer. Note this needs to be invoked after using methods that writes directly to the bit buffer. |
WriteByte(byte) | Writes an unsigned byte to the current stream and advances the stream position by one byte. |
WriteBytes(NativeArray<byte>) | Copy NativeArray of bytes into the writers data buffer. |
WriteDouble(double) | Writes a 8-byte floating point value to the data stream. |
WriteFixedString128(FixedString128Bytes) | Writes a |
WriteFixedString32(FixedString32Bytes) | Writes a |
WriteFixedString4096(FixedString4096Bytes) | Writes a |
WriteFixedString512(FixedString512Bytes) | Writes a |
WriteFixedString64(FixedString64Bytes) | Writes a |
WriteFloat(float) | Writes a 4-byte floating point value to the data stream. |
WriteInt(int) | Writes a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes. |
WriteIntNetworkByteOrder(int) | 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(long) | Writes an 8-byte signed long from the stream and advances the current position of the stream by eight bytes. |
WritePackedDouble(double, StreamCompressionModel) | Writes a 8-byte floating point value to the data stream using a StreamCompressionModel. |
WritePackedDoubleDelta(double, double, StreamCompressionModel) | 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(FixedString128Bytes, FixedString128Bytes, StreamCompressionModel) | Writes a delta |
WritePackedFixedString32Delta(FixedString32Bytes, FixedString32Bytes, StreamCompressionModel) | Writes a |
WritePackedFixedString4096Delta(FixedString4096Bytes, FixedString4096Bytes, StreamCompressionModel) | Writes a delta |
WritePackedFixedString512Delta(FixedString512Bytes, FixedString512Bytes, StreamCompressionModel) | Writes a delta |
WritePackedFixedString64Delta(FixedString64Bytes, FixedString64Bytes, StreamCompressionModel) | Writes a delta |
WritePackedFloat(float, StreamCompressionModel) | Writes a 4-byte floating point value to the data stream using a StreamCompressionModel. |
WritePackedFloatDelta(float, float, StreamCompressionModel) | 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(int, StreamCompressionModel) | 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(int, int, StreamCompressionModel) | Writes a delta 4-byte signed integer value to the data stream using a StreamCompressionModel. |
WritePackedLong(long, StreamCompressionModel) | Writes a 8-byte signed long value to the data stream using a StreamCompressionModel. |
WritePackedLongDelta(long, long, StreamCompressionModel) | Writes a delta 8-byte signed long value to the data stream using a StreamCompressionModel. |
WritePackedUInt(uint, StreamCompressionModel) | Writes a 4-byte unsigned integer value to the data stream using a StreamCompressionModel. |
WritePackedUIntDelta(uint, uint, StreamCompressionModel) | 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(ulong, StreamCompressionModel) | Writes an 8-byte unsigned long value to the data stream using a StreamCompressionModel. |
WritePackedULongDelta(ulong, ulong, StreamCompressionModel) | 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(uint, int) | Appends a specified number of bits to the data stream. |
WriteShort(short) | Writes a 2-byte signed short to the current stream and advances the stream position by two bytes. |
WriteShortNetworkByteOrder(short) | 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(uint) | Reads a 4-byte unsigned integer from the current stream and advances the current position of the stream by four bytes. |
WriteUIntNetworkByteOrder(uint) | 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(ulong) | Reads an 8-byte unsigned long from the stream and advances the current position of the stream by eight bytes. |
WriteUShort(ushort) | Writes a 2-byte unsigned short to the current stream and advances the stream position by two bytes. |
WriteUShortNetworkByteOrder(ushort) | 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. |