Skip to content

Commit

Permalink
A skip is basically a read to a null buffer (#2265)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow authored Sep 29, 2022
1 parent 023ee8e commit 3610666
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 5 deletions.
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);

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

0 comments on commit 3610666

Please sign in to comment.