From 2f18086d763d19ce61839bf78b164f1bf605a27e Mon Sep 17 00:00:00 2001 From: Andrius Bentkus Date: Sun, 10 Feb 2013 20:22:00 +0100 Subject: [PATCH] Add RawContentStream. Holds the rawcontent objectsafe wrapper. --- LibGit2Sharp/Core/Proxy.cs | 9 +------- LibGit2Sharp/Core/RawContentStream.cs | 32 +++++++++++++++++++++++++++ LibGit2Sharp/LibGit2Sharp.csproj | 1 + 3 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 LibGit2Sharp/Core/RawContentStream.cs 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 @@ +