|
38 | 38 | // Copyright (c) 2007-2020 VMware, Inc. All rights reserved.
|
39 | 39 | //---------------------------------------------------------------------------
|
40 | 40 |
|
| 41 | +using System.Buffers; |
41 | 42 | using System.Text;
|
42 | 43 | using RabbitMQ.Util;
|
43 | 44 |
|
@@ -66,7 +67,7 @@ public static byte[] ReadBytes(NetworkBinaryReader reader, int count)
|
66 | 67 |
|
67 | 68 | public static char ReadChar(NetworkBinaryReader reader)
|
68 | 69 | {
|
69 |
| - return (char) reader.ReadUInt16(); |
| 70 | + return (char)reader.ReadUInt16(); |
70 | 71 | }
|
71 | 72 |
|
72 | 73 | public static double ReadDouble(NetworkBinaryReader reader)
|
@@ -118,7 +119,7 @@ public static void WriteBytes(NetworkBinaryWriter writer, byte[] source)
|
118 | 119 |
|
119 | 120 | public static void WriteChar(NetworkBinaryWriter writer, char value)
|
120 | 121 | {
|
121 |
| - writer.Write((ushort) value); |
| 122 | + writer.Write((ushort)value); |
122 | 123 | }
|
123 | 124 |
|
124 | 125 | public static void WriteDouble(NetworkBinaryWriter writer, double value)
|
@@ -148,9 +149,18 @@ public static void WriteSingle(NetworkBinaryWriter writer, float value)
|
148 | 149 |
|
149 | 150 | public static void WriteString(NetworkBinaryWriter writer, string value)
|
150 | 151 | {
|
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 | + } |
154 | 164 | }
|
155 | 165 | }
|
156 | 166 | }
|
0 commit comments