Skip to content

Commit d765ac9

Browse files
committed
Reserve space in buffer for blobs. Fixes #220
1 parent a882d4d commit d765ac9

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/MySqlConnector/MySqlClient/MySqlParameter.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,21 @@ internal void AppendSqlString(BinaryWriter writer, StatementPreparerOptions opti
9393
{
9494
writer.WriteUtf8("{0}".FormatInvariant(Value));
9595
}
96-
else if (Value is byte[])
96+
else if (Value is byte[] byteArray)
9797
{
98-
writer.WriteUtf8("_binary'");
98+
// determine the number of bytes to be written
99+
const string c_prefix = "_binary'";
100+
var length = byteArray.Length + c_prefix.Length + 1;
99101
foreach (var by in (byte[]) Value)
102+
{
103+
if (by == 0x27 || by == 0x5C)
104+
length++;
105+
}
106+
107+
((MemoryStream) writer.BaseStream).Capacity = (int) writer.BaseStream.Length + length;
108+
109+
writer.WriteUtf8(c_prefix);
110+
foreach (var by in byteArray)
100111
{
101112
if (by == 0x27 || by == 0x5C)
102113
writer.Write((byte) 0x5C);

0 commit comments

Comments
 (0)