Skip to content

Commit 1a20890

Browse files
committed
Use ZLibStream in .NET 6. Fixes #957
1 parent 5777b87 commit 1a20890

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/MySqlConnector/Protocol/Serialization/CompressedPayloadHandler.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ private ValueTask<int> ReadBytesAsync(Memory<byte> buffer, ProtocolErrorBehavior
127127
}
128128
else
129129
{
130+
#if NET6_0_OR_GREATER
131+
var uncompressedData = new byte[uncompressedLength];
132+
using var compressedStream = new MemoryStream(payloadReadBytes.Array!, payloadReadBytes.Offset, payloadReadBytes.Count);
133+
using var decompressingStream = new ZLibStream(compressedStream, CompressionMode.Decompress);
134+
var bytesRead = decompressingStream.Read(uncompressedData, 0, uncompressedLength);
135+
m_remainingData = new(uncompressedData, 0, bytesRead);
136+
#else
130137
// check CMF (Compression Method and Flags) and FLG (Flags) bytes for expected values
131138
var cmf = payloadReadBytes.Array![payloadReadBytes.Offset];
132139
var flg = payloadReadBytes.Array[payloadReadBytes.Offset + 1];
@@ -162,6 +169,7 @@ private ValueTask<int> ReadBytesAsync(Memory<byte> buffer, ProtocolErrorBehavior
162169
default :
163170
ValueTaskExtensions.FromException<int>(new NotSupportedException("Invalid Adler-32 checksum of uncompressed data."));
164171
}
172+
#endif
165173
}
166174

167175
var bytesToRead = Math.Min(m_remainingData.Count, buffer.Length);
@@ -186,6 +194,10 @@ private ValueTask<int> CompressAndWrite(ArraySegment<byte> remainingUncompressed
186194
{
187195
using var compressedStream = new MemoryStream();
188196

197+
#if NET6_0_OR_GREATER
198+
using (var zlibStream = new ZLibStream(compressedStream, CompressionLevel.Optimal, leaveOpen: true))
199+
zlibStream.Write(remainingUncompressedData.Array!, remainingUncompressedData.Offset, remainingUncompressedBytes);
200+
#else
189201
// write CMF: 32K window + deflate algorithm
190202
compressedStream.WriteByte(0x78);
191203

@@ -200,6 +212,7 @@ private ValueTask<int> CompressAndWrite(ArraySegment<byte> remainingUncompressed
200212
compressedStream.WriteByte((byte) ((checksum >> 16) & 0xFF));
201213
compressedStream.WriteByte((byte) ((checksum >> 8) & 0xFF));
202214
compressedStream.WriteByte((byte) (checksum & 0xFF));
215+
#endif
203216

204217
if (!compressedStream.TryGetBuffer(out compressedData))
205218
throw new InvalidOperationException("Couldn't get compressed stream buffer.");

src/MySqlConnector/Utilities/Adler32.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0.
33
// https://github.com/SixLabors/ImageSharp/blob/master/src/ImageSharp/Formats/Png/Zlib/Adler32.cs
44

5+
#if !NET6_0_OR_GREATER
56
using System;
67
#if NETCOREAPP3_0_OR_GREATER
78
using System.Runtime.Intrinsics;
@@ -245,3 +246,4 @@ private static unsafe uint CalculateScalar(ReadOnlySpan<byte> buffer, uint offse
245246
}
246247
}
247248
}
249+
#endif

0 commit comments

Comments
 (0)