Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 7 additions & 7 deletions binding/HarfBuzzSharp/Blob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.IO;
using System.Runtime.InteropServices;

namespace HarfBuzzSharp
{
Expand Down Expand Up @@ -70,15 +71,14 @@ public static Blob FromFile (string fileName)

public static unsafe Blob FromStream (Stream stream)
{
Comment thread
jeremy-visionaid marked this conversation as resolved.
// TODO: check to see if we can avoid the second copy (the ToArray)
var length = (int)(stream.Length - stream.Position);
Comment thread
jeremy-visionaid marked this conversation as resolved.

Comment thread
jeremy-visionaid marked this conversation as resolved.
Comment thread
jeremy-visionaid marked this conversation as resolved.
using var ms = new MemoryStream ();
stream.CopyTo (ms);
var data = ms.ToArray ();
var dataPtr = Marshal.AllocCoTaskMem (length);

fixed (byte* dataPtr = data) {
return new Blob ((IntPtr)dataPtr, data.Length, MemoryMode.ReadOnly, () => ms.Dispose ());
}
using var ums = new UnmanagedMemoryStream ((byte*)dataPtr, length, length, FileAccess.ReadWrite);
stream.CopyTo (ums);
Comment thread
jeremy-visionaid marked this conversation as resolved.
Outdated

return new Blob (dataPtr, length, MemoryMode.ReadOnly, () => Marshal.FreeCoTaskMem (dataPtr));
}

private static IntPtr Create (IntPtr data, int length, MemoryMode mode, ReleaseDelegate releaseProc)
Expand Down
19 changes: 15 additions & 4 deletions tests/Tests/HarfBuzzSharp/HBBlobTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,21 @@ public void ShouldCreateFromFileName()
[SkippableFact]
public void ShouldCreateFromStream()
{
using (var blob = Blob.FromStream(File.Open(Path.Combine(PathToFonts, "Funkster.ttf"), FileMode.Open, FileAccess.Read)))
{
Assert.Equal(236808, blob.Length);
}
using var stream = File.Open(Path.Combine(PathToFonts, "Funkster.ttf"), FileMode.Open, FileAccess.Read);
using var blob = Blob.FromStream(stream);
Assert.Equal(236808, blob.Length);
}

[SkippableFact]
public void CreateFromStreamIsGCSafe()
{
using var stream = File.Open(Path.Combine(PathToFonts, "Funkster.ttf"), FileMode.Open, FileAccess.Read);
using var blob = Blob.FromStream(stream);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
var data = new byte[blob.Length];
blob.AsSpan().CopyTo(data);
Comment thread
jeremy-visionaid marked this conversation as resolved.
Outdated
}

[SkippableFact]
Expand Down
Loading