Decompress data in Span<byte> with Deflate algorithm
As of January 2018, there isn't any build-in support for ReadOnlySpan<byte>
, but as Stephen Toub pointed out on GitHub it can be solved with the UnmanagedMemoryStream
private unsafe byte[] Decompress(ReadOnlySpan<byte> buffer, int decompressedSize) {
fixed (byte* pBuffer = &buffer[0]) {
using (var stream = new UnmanagedMemoryStream(pBuffer, buffer.Length)) {
using (var deflateStream = new DeflateStream(stream, CompressionMode.Decompress)) {
var data = ArrayPool<byte>.Shared.Rent(decompressedSize);
deflateStream.Read(data, 0, decompressedSize);
return data;
}
}
}
}