Skip to content
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
30 changes: 13 additions & 17 deletions binding/Binding/SKData.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Runtime.InteropServices;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.ComponentModel;
using System.Buffers;

namespace SkiaSharp
{
Expand All @@ -16,7 +14,7 @@ public unsafe class SKData : SKObject, ISKNonVirtualReferenceCounted

private static readonly SKData empty;

static SKData()
static SKData ()
{
empty = new SKDataStatic (SkiaApi.sk_data_new_empty ());
}
Expand Down Expand Up @@ -53,7 +51,7 @@ public static SKData CreateCopy (IntPtr bytes, ulong length)
{
if (!PlatformConfiguration.Is64Bit && length > UInt32.MaxValue)
throw new ArgumentOutOfRangeException (nameof (length), "The length exceeds the size of pointers.");
return GetObject (SkiaApi.sk_data_new_with_copy ((void*)bytes, (IntPtr) length));
return GetObject (SkiaApi.sk_data_new_with_copy ((void*)bytes, (IntPtr)length));
}

public static SKData CreateCopy (byte[] bytes) =>
Expand All @@ -75,10 +73,8 @@ public static SKData CreateCopy (ReadOnlySpan<byte> bytes)

// Create

public static SKData Create (int size)
{
return GetObject (SkiaApi.sk_data_new_uninitialized ((IntPtr) size));
}
public static SKData Create (int size) =>
GetObject (SkiaApi.sk_data_new_uninitialized ((IntPtr)size));

public static SKData Create (long size) =>
GetObject (SkiaApi.sk_data_new_uninitialized ((IntPtr)size));
Expand All @@ -87,8 +83,8 @@ public static SKData Create (ulong size)
{
if (!PlatformConfiguration.Is64Bit && size > UInt32.MaxValue)
throw new ArgumentOutOfRangeException (nameof (size), "The size exceeds the size of pointers.");
return GetObject (SkiaApi.sk_data_new_uninitialized ((IntPtr) size));

return GetObject (SkiaApi.sk_data_new_uninitialized ((IntPtr)size));
}

public static SKData Create (string filename)
Expand Down Expand Up @@ -157,23 +153,23 @@ public static SKData Create (SKStream stream, int length)
if (stream == null)
throw new ArgumentNullException (nameof (stream));

return GetObject (SkiaApi.sk_data_new_from_stream (stream.Handle, (IntPtr) length));
return GetObject (SkiaApi.sk_data_new_from_stream (stream.Handle, (IntPtr)length));
}

public static SKData Create (SKStream stream, ulong length)
{
if (stream == null)
throw new ArgumentNullException (nameof (stream));

return GetObject (SkiaApi.sk_data_new_from_stream (stream.Handle, (IntPtr) length));
return GetObject (SkiaApi.sk_data_new_from_stream (stream.Handle, (IntPtr)length));
}

public static SKData Create (SKStream stream, long length)
{
if (stream == null)
throw new ArgumentNullException (nameof (stream));

return GetObject (SkiaApi.sk_data_new_from_stream (stream.Handle, (IntPtr) length));
return GetObject (SkiaApi.sk_data_new_from_stream (stream.Handle, (IntPtr)length));
}

public static SKData Create (IntPtr address, int length)
Expand Down Expand Up @@ -211,7 +207,7 @@ public SKData Subset (ulong offset, ulong length)
if (offset > UInt32.MaxValue)
throw new ArgumentOutOfRangeException (nameof (offset), "The offset exceeds the size of pointers.");
}
return GetObject (SkiaApi.sk_data_new_subset (Handle, (IntPtr) offset, (IntPtr) length));
return GetObject (SkiaApi.sk_data_new_subset (Handle, (IntPtr)offset, (IntPtr)length));
}

// ToArray
Expand Down Expand Up @@ -279,7 +275,7 @@ private class SKDataStream : UnmanagedMemoryStream
private readonly bool disposeHost;

public unsafe SKDataStream (SKData host, bool disposeHost = false)
: base((byte *) host.Data, host.Size)
: base ((byte*)host.Data, host.Size, host.Size, FileAccess.ReadWrite)
Comment on lines -282 to +278

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is the real change here.

{
this.host = host;
this.disposeHost = disposeHost;
Expand Down
36 changes: 36 additions & 0 deletions tests/Tests/SKDataTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,42 @@ public void ValidDataProperties()
Assert.Equal(OddData, data.ToArray());
}

[SkippableFact]
public void AsStreamReturnsCorrectStreamData()
{
var data = SKData.CreateCopy(OddData);

var stream = data.AsStream();

var buffer = new byte[5];
stream.Read(buffer, 0, 5);

Assert.Equal(OddData, buffer);
}

[SkippableFact]
public void CanWriteToAsStream()
{
var data = SKData.Create(5);

var stream = data.AsStream();
stream.Write(OddData, 0, 5);

Assert.Equal(OddData, data.ToArray());
}

[SkippableFact]
public void CanCopyToAsStream()
{
var data = SKData.Create(5);

var stream = data.AsStream();
var ms = new MemoryStream(OddData);
ms.CopyTo(stream);

Assert.Equal(OddData, data.ToArray());
}

[SkippableTheory]
[InlineData(null, 0, 0, 0)]
[InlineData("", 0, 0, 0)]
Expand Down