Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A skip is basically a read to a null buffer #2265

Merged
merged 1 commit into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions binding/Binding/SKManagedStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,22 @@ protected override void DisposeManaged ()

private IntPtr OnReadManagedStream (IntPtr buffer, IntPtr size)
{
if (buffer == IntPtr.Zero)
throw new ArgumentNullException (nameof (buffer));
if ((int)size < 0)
throw new ArgumentOutOfRangeException (nameof (size));

if (size == IntPtr.Zero)
return IntPtr.Zero;

// NOTE: some skips still requires a read as some streams cannot seek
using var managedBuffer = Utils.RentArray<byte> ((int)size);
var len = stream.Read (managedBuffer.Array, 0, managedBuffer.Length);
Comment on lines +96 to 98
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not perfect, but I will fix this in a separate PR. The annoying thing is that some streams like and say they are seekable but are not or they allow seeking past the end of the stream causing other issues...


var src = managedBuffer.Span.Slice (0, len);
var dst = buffer.AsSpan (managedBuffer.Length);
src.CopyTo (dst);
if (buffer != IntPtr.Zero) {
// read
var src = managedBuffer.Span.Slice (0, len);
var dst = buffer.AsSpan (managedBuffer.Length);
src.CopyTo (dst);
}

if (!stream.CanSeek && (int)size > 0 && len <= (int)size)
isAsEnd = true;
Expand Down
Binary file added tests/Content/images/tomato.bmp
Binary file not shown.
15 changes: 15 additions & 0 deletions tests/Tests/SKBitmapTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -739,5 +739,20 @@ public void CanDecodePotentiallyCorruptPngFiles(string filename)

Assert.NotNull(bitmap);
}

[Theory]
[InlineData("tomato.bmp")]
[InlineData("baboon.jpg")]
[InlineData("baboon.png")]
[InlineData("animated-heart.gif")]
public void CanDecodeImageStreams(string filename)
{
var path = Path.Combine(PathToImages, filename);

using var stream = File.OpenRead(path);
using var bitmap = SKBitmap.Decode(stream);

Assert.NotNull(bitmap);
}
}
}
72 changes: 72 additions & 0 deletions tests/Tests/SKManagedStreamTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,83 @@ public void ReadIsCorrect(int dataSize, int readSize, int finalPos, int expected
var actualReadSize = managedStream.Read(buffer, readSize);

Assert.Equal(expectedReadSize, actualReadSize);
Assert.Equal(finalPos, stream.Position);
Assert.Equal(finalPos, managedStream.Position);
Assert.Equal(data.Take(readSize), buffer.Take(actualReadSize));
Assert.All(buffer.Skip(actualReadSize), i => Assert.Equal(0, i));
}

[SkippableTheory]
[InlineData(1024, 0, 0, 0)]
[InlineData(1024, 1, 1, 1)]
[InlineData(1024, 10, 10, 10)]
[InlineData(1024, 100, 100, 100)]
[InlineData(1024, 1000, 1000, 1000)]
[InlineData(1024, 10000, 1024, 1024)]
public void SkipIsCorrect(int dataSize, int readSize, int finalPos, int expectedReadSize)
{
var data = new byte[dataSize];
for (var i = 0; i < data.Length; i++)
{
data[i] = (byte)(i % byte.MaxValue);
}

var stream = new MemoryStream(data);
var managedStream = new SKManagedStream(stream);

var actualReadSize = managedStream.Skip(readSize);

Assert.Equal(expectedReadSize, actualReadSize);
Assert.Equal(finalPos, stream.Position);
Assert.Equal(finalPos, managedStream.Position);
}

[SkippableTheory]
[InlineData(1024, 0, 0, 0)]
[InlineData(1024, 1, 1, 1)]
[InlineData(1024, 10, 10, 10)]
[InlineData(1024, 100, 100, 100)]
[InlineData(1024, 1000, 1000, 1000)]
[InlineData(1024, 10000, 1024, 1024)]
public void SkipNonSeekableIsCorrect(int dataSize, int readSize, int finalPos, int expectedReadSize)
{
var data = new byte[dataSize];
for (var i = 0; i < data.Length; i++)
{
data[i] = (byte)(i % byte.MaxValue);
}

var stream = new MemoryStream(data);
var nonSeekable = new NonSeekableReadOnlyStream(stream);
var managedStream = new SKManagedStream(nonSeekable);

var actualReadSize = managedStream.Skip(readSize);

Assert.Equal(expectedReadSize, actualReadSize);
Assert.Equal(finalPos, stream.Position);
}

[SkippableFact]
public void SkipOffsetChunkCorrectly()
{
var data = new byte[1024];
for (int i = 0; i < data.Length; i++)
{
data[i] = (byte)(i % byte.MaxValue);
}

var stream = new MemoryStream(data);
var skManagedStream = new SKManagedStream(stream);

var offset = 768;

skManagedStream.Position = offset;

var taken = skManagedStream.Skip(data.Length);

Assert.Equal(data.Length - offset, taken);
}

[SkippableFact]
public void ManagedStreamReadsChunkCorrectly()
{
Expand Down