android: VS 16.2.0 regression: CopyToAsync from DeflateStream to GzipStream throws NotImplementedException
Steps to Reproduce
- Use the ZipArchive and ZipArchiveEntry classes to open a stream to an entry in a zip file
- Set up a GZipStream compressing to a MemoryStream
- Copy the zip entry stream to the GZipStream
- Observe the NotImplementedException
Repro project: https://github.com/mfeingol/repros/tree/master/Xamarin.Android-3397-CopyToAsync
Expected Behavior
Stream copy is successful, as it was in prior versions of the Mono runtime.
Actual Behavior
NotImplementedException:
at System.IO.Compression.DeflateStream.WriteAsyncMemory (System.ReadOnlyMemory`1[T] source, System.Threading.CancellationToken cancellationToken) [0x00000] in <ca7419b40e504a6dbe088f6fe95d09aa>:0
at (wrapper remoting-invoke-with-check) System.IO.Compression.DeflateStream.WriteAsyncMemory(System.ReadOnlyMemory`1<byte>,System.Threading.CancellationToken)
at System.IO.Compression.GZipStream.WriteAsync (System.ReadOnlyMemory`1[T] buffer, System.Threading.CancellationToken cancellationToken) [0x00026] in <ca7419b40e504a6dbe088f6fe95d09aa>:0
at System.IO.Stream.CopyToAsyncInternal (System.IO.Stream destination, System.Int32 bufferSize, System.Threading.CancellationToken cancellationToken) [0x000c7] in <ff07eae8184a40a08e79049bbcb31a0e>:0
at CopyToAsync.MainPage.Repro (System.Object sender, System.EventArgs args) [0x0008a] in C:\Users\mfeingol\source\Repos\CopyToAsync\CopyToAsync\CopyToAsync\MainPage.xaml.cs:35
Example code:
ZipArchive archive = new ZipArchive(ZipFile, ZipArchiveMode.Read, true, Encoding.UTF8);
ZipArchiveEntry entry = archive.GetEntry("a4.jpg");
Stream src = entry.Open();
MemoryStream dest = new MemoryStream();
using (GZipStream gzip = new GZipStream(dest, CompressionLevel.Optimal, true))
await src.CopyToAsync(gzip);
byte[] compressed = dest.ToArray();
Version Information
VS 16.2.0, fresh project from new project wizard, latest Xamarin.Forms Nuget packages (not that it’s relevant)
Log File
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 3
- Comments: 19 (11 by maintainers)
So, there is a slight difference that changes the code path to call DeflateStream.WriteAsyncMemory.
In 5.18 (2018-08), Mono was using the referencesource version of Stream, which opened its own buffer and called this WriteAsync method in GZipStream:
https://github.com/mono/corefx/blob/c976905c1de06786292a6fb01656b43650f262c9/src/System.IO.Compression/src/System/IO/Compression/GZipStream.cs#L177-L181
In 6.0 (2019-02), we changed changed to the corefx one via this commit:
https://github.com/mono/mono/commit/46a28303e219bf3ede81bc5989b0add250212860
And now, that code rents a shared buffer and calls this method in GZipStream:
https://github.com/mono/corefx/blob/c976905c1de06786292a6fb01656b43650f262c9/src/System.IO.Compression/src/System/IO/Compression/GZipStream.cs#L183-L197
This is a regression. To fix, I’m not sure if there’s anything stopping us from taking the corefx version, but I’m sure we can at the very least take the WriteAsyncMemory piece.
A new Preview version has now been published that includes a fix for this item. The fix is not yet included in a Release version. I will update this item again when a Release version is available that includes the fix.
Fix included in Xamarin.Android 10.0.0.43.
Fix included on Windows in Visual Studio 2019 version 16.3 Preview 4. To try the Preview version that includes the fix, check for the latest updates in Visual Studio Preview.
Fix included on macOS in Visual Studio 2019 for Mac version 8.3 Preview 5. To try the Preview version that includes the fix, check for the latest updates on the Preview updater channel.
@czuvich I haven’t tried it, but I think if you derive your own GZipStream, it’ll call the old WriteAsync. That’s because typeof should return your class as opposed to GZipStream.
Note: https://github.com/mono/corefx/blob/c976905c1de06786292a6fb01656b43650f262c9/src/System.IO.Compression/src/System/IO/Compression/GZipStream.cs#L183-L197
@mfeingol yes I can reproduce it, 😞 I just don’t have an explanation.
I think we’ll need someone on the Mono team to investigate.