Skip to content

Commit fd718a6

Browse files
Merge pull request #733 from rabbitmq/rabbitmq-dotnet-client-731
Merge pull request #729 from stebet/crossPlatformSerialization
2 parents 9765b4b + 7640f4e commit fd718a6

File tree

7 files changed

+254
-244
lines changed

7 files changed

+254
-244
lines changed

projects/client/RabbitMQ.Client/RabbitMQ.Client.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<ItemGroup>
7474
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" />
7575
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All"/>
76+
<PackageReference Include="System.Memory" Version="4.5.3" />
7677
</ItemGroup>
7778

7879
<ItemGroup Condition=" '$(Configuration)' == 'SignedRelease' ">

projects/client/RabbitMQ.Client/src/client/content/BytesWireFormatting.cs

+15-5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
// Copyright (c) 2007-2020 VMware, Inc. All rights reserved.
3939
//---------------------------------------------------------------------------
4040

41+
using System.Buffers;
4142
using System.Text;
4243
using RabbitMQ.Util;
4344

@@ -66,7 +67,7 @@ public static byte[] ReadBytes(NetworkBinaryReader reader, int count)
6667

6768
public static char ReadChar(NetworkBinaryReader reader)
6869
{
69-
return (char) reader.ReadUInt16();
70+
return (char)reader.ReadUInt16();
7071
}
7172

7273
public static double ReadDouble(NetworkBinaryReader reader)
@@ -118,7 +119,7 @@ public static void WriteBytes(NetworkBinaryWriter writer, byte[] source)
118119

119120
public static void WriteChar(NetworkBinaryWriter writer, char value)
120121
{
121-
writer.Write((ushort) value);
122+
writer.Write((ushort)value);
122123
}
123124

124125
public static void WriteDouble(NetworkBinaryWriter writer, double value)
@@ -148,9 +149,18 @@ public static void WriteSingle(NetworkBinaryWriter writer, float value)
148149

149150
public static void WriteString(NetworkBinaryWriter writer, string value)
150151
{
151-
byte[] bytes = Encoding.UTF8.GetBytes(value);
152-
writer.Write((ushort) bytes.Length);
153-
writer.Write(bytes);
152+
int maxLength = Encoding.UTF8.GetMaxByteCount(value.Length);
153+
byte[] bytes = ArrayPool<byte>.Shared.Rent(maxLength);
154+
try
155+
{
156+
int bytesUsed = Encoding.UTF8.GetBytes(value, 0, value.Length, bytes, 0);
157+
writer.Write((ushort)bytesUsed);
158+
writer.Write(bytes, 0, bytesUsed);
159+
}
160+
finally
161+
{
162+
ArrayPool<byte>.Shared.Return(bytes);
163+
}
154164
}
155165
}
156166
}

0 commit comments

Comments
 (0)