/// <summary> /// Converts to stream. /// </summary> /// <returns>A <see cref="Stream"/> that contains this encoded instance.</returns> public Stream ToStream() { if (Data.Count > ushort.MaxValue) { throw new Exception($"Attempt to write more than {ushort.MaxValue} bytes in DataMessage."); } var stream = new MemoryStream(); var writer = new BinaryWriter(stream, Encoding.UTF8, true); writer.Write(( byte )1); writer.Write(ConnectionId.ToByteArray()); writer.Write(( byte )CompressionMode); writer.Write(( ushort )Data.Count); writer.Write(Data.Array, Data.Offset, Data.Count); stream.Position = 0; return(stream); }