diff --git a/LibGit2Sharp/Core/Proxy.cs b/LibGit2Sharp/Core/Proxy.cs index 02e2adbf9..7e891d30c 100644 --- a/LibGit2Sharp/Core/Proxy.cs +++ b/LibGit2Sharp/Core/Proxy.cs @@ -85,14 +85,7 @@ public static byte[] git_blob_rawcontent(RepositorySafeHandle repo, ObjectId id, public static UnmanagedMemoryStream git_blob_rawcontent_stream(RepositorySafeHandle repo, ObjectId id, Int64 size) { - using (var obj = new ObjectSafeWrapper(id, repo)) - { - IntPtr ptr = NativeMethods.git_blob_rawcontent(obj.ObjectPtr); - unsafe - { - return new UnmanagedMemoryStream((byte*)ptr.ToPointer(), size); - } - } + return new RawContentStream(id, repo, NativeMethods.git_blob_rawcontent, size); } public static Int64 git_blob_rawsize(GitObjectSafeHandle obj) diff --git a/LibGit2Sharp/Core/RawContentStream.cs b/LibGit2Sharp/Core/RawContentStream.cs new file mode 100644 index 000000000..d15f95b92 --- /dev/null +++ b/LibGit2Sharp/Core/RawContentStream.cs @@ -0,0 +1,32 @@ +using System; +using System.IO; +using LibGit2Sharp.Core; +using LibGit2Sharp.Core.Handles; + +namespace LibGit2Sharp.Core +{ + internal class RawContentStream : UnmanagedMemoryStream + { + readonly ObjectSafeWrapper wrapper; + + internal RawContentStream(ObjectId id, RepositorySafeHandle repo, + Func bytePtrProvider, long length) + : this(new ObjectSafeWrapper(id, repo), bytePtrProvider, length) + { + } + + unsafe RawContentStream(ObjectSafeWrapper wrapper, + Func bytePtrProvider, long length) + : base((byte *)bytePtrProvider(wrapper.ObjectPtr).ToPointer(), length) + { + this.wrapper = wrapper; + } + + protected override void Dispose(bool disposing) + { + base.Dispose(disposing); + wrapper.SafeDispose(); + } + } +} + diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj index 0febc6535..c3fec4418 100644 --- a/LibGit2Sharp/LibGit2Sharp.csproj +++ b/LibGit2Sharp/LibGit2Sharp.csproj @@ -216,6 +216,7 @@ +