internal static byte[] CreateMessageBody(MultiArray <byte> message, Guid correlationId) { var body = new byte[16 + message.Count]; #if COMPATIBILITY correlationId.ToByteArray().CopyTo(body, 0); #else correlationId.TryWriteBytes(body.AsSpan(0, 16)); #endif message.CopyTo(body, 16); return(body); }
internal static byte[] CreateRawMessageBody(MultiArray <byte> message, Guid correlationId, string name) { var nameLength = Encoding.GetByteCount(name); var body = new byte[16 + 4 + nameLength + message.Count]; var bodySpan = body.AsSpan(); #if COMPATIBILITY correlationId.ToByteArray().CopyTo(body, 0); MemoryMarshal.Write(bodySpan.Slice(16, 4), ref nameLength); Encoding.GetBytes(name).CopyTo(bodySpan.Slice(20, nameLength)); #else correlationId.TryWriteBytes(bodySpan.Slice(0, 16)); BitConverter.TryWriteBytes(bodySpan.Slice(16, 4), nameLength); Encoding.GetBytes(name, bodySpan.Slice(20, nameLength)); #endif message.CopyTo(body, 20 + nameLength); return(body); }
public void CopyTo(Stream stream) { if (disposedValue) { ThrowDisposedValue(); } var hasDataType = !string.IsNullOrEmpty(_dataType); var hasMimeType = !string.IsNullOrEmpty(_serializerMimeType); var dataTypeLength = hasDataType ? Encoding.UTF8.GetByteCount(_dataType) : 0; var serializerMimeTypeLength = hasMimeType ? Encoding.UTF8.GetByteCount(_serializerMimeType) : 0; #if COMPATIBILITY byte[] buffer; //DataType buffer = BitConverter.GetBytes(hasDataType ? dataTypeLength : -1); stream.WriteBytes(buffer); if (hasDataType) { buffer = Encoding.UTF8.GetBytes(_dataType); stream.Write(buffer); } //MimeType buffer = BitConverter.GetBytes(hasMimeType ? serializerMimeTypeLength : -1); stream.WriteBytes(buffer); if (hasMimeType) { buffer = Encoding.UTF8.GetBytes(_serializerMimeType); stream.Write(buffer); } //Data buffer = BitConverter.GetBytes(_data.Count); stream.WriteBytes(buffer); _data.CopyTo(stream); #else Span <byte> intBuffer = stackalloc byte[4]; //DataType BitConverter.TryWriteBytes(intBuffer, hasDataType ? dataTypeLength : -1); stream.Write(intBuffer); if (hasDataType) { Span <byte> dataTypeBuffer = stackalloc byte[dataTypeLength]; Encoding.UTF8.GetBytes(_dataType, dataTypeBuffer); stream.Write(dataTypeBuffer); } //MimeType BitConverter.TryWriteBytes(intBuffer, hasMimeType ? serializerMimeTypeLength : -1); stream.Write(intBuffer); if (hasMimeType) { Span <byte> mimeTypeBuffer = stackalloc byte[serializerMimeTypeLength]; Encoding.UTF8.GetBytes(_serializerMimeType, mimeTypeBuffer); stream.Write(mimeTypeBuffer); } //Data BitConverter.TryWriteBytes(intBuffer, _data.Count); stream.Write(intBuffer); _data.CopyTo(stream); #endif }