From dbd09b0ffeca981c878c5ce5ec333019c85406a4 Mon Sep 17 00:00:00 2001 From: Matthew Leibowitz Date: Mon, 18 Sep 2017 16:54:11 +0200 Subject: [PATCH] SkiaSharp streams always return all the requested bytes (blocking). Closes #335 --- binding/Binding/SKManagedStream.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/binding/Binding/SKManagedStream.cs b/binding/Binding/SKManagedStream.cs index cc83c90e..26b5d436 100644 --- a/binding/Binding/SKManagedStream.cs +++ b/binding/Binding/SKManagedStream.cs @@ -11,6 +11,7 @@ using System.Runtime.InteropServices; using System.IO; using System.Collections.Generic; using System.Diagnostics; +using System.Text; #if __IOS__ using ObjCRuntime; @@ -144,10 +145,13 @@ namespace SkiaSharp { var managedStream = AsManagedStream (managedStreamPtr); var count = (int)size; - var managedBuffer = new byte[count]; - var result = managedStream.stream.Read (managedBuffer, 0, count); + byte[] managedBuffer; + using (var reader = new BinaryReader (managedStream.stream, Encoding.UTF8, true)) { + managedBuffer = reader.ReadBytes (count); + } + var result = managedBuffer.Length; if (buffer != IntPtr.Zero) { - Marshal.Copy (managedBuffer, 0, buffer, count); + Marshal.Copy (managedBuffer, 0, buffer, result); } return (IntPtr)result; } @@ -162,10 +166,13 @@ namespace SkiaSharp } var oldPos = managedStream.stream.Position; var count = (int)size; - var managedBuffer = new byte[count]; - var result = managedStream.stream.Read (managedBuffer, 0, count); + byte[] managedBuffer; + using (var reader = new BinaryReader (managedStream.stream, Encoding.UTF8, true)) { + managedBuffer = reader.ReadBytes (count); + } + var result = managedBuffer.Length; if (buffer != IntPtr.Zero) { - Marshal.Copy (managedBuffer, 0, buffer, count); + Marshal.Copy (managedBuffer, 0, buffer, result); } managedStream.stream.Position = oldPos; return (IntPtr)result;