diff --git a/LibGit2Sharp.Tests/OdbBackendFixture.cs b/LibGit2Sharp.Tests/OdbBackendFixture.cs index cd8429ffc..db2e37852 100644 --- a/LibGit2Sharp.Tests/OdbBackendFixture.cs +++ b/LibGit2Sharp.Tests/OdbBackendFixture.cs @@ -240,7 +240,7 @@ protected override OdbBackendOperations SupportedOperations } } - public override int Read(ObjectId oid, out Stream data, out ObjectType objectType) + public override int Read(ObjectId oid, out UnmanagedMemoryStream data, out ObjectType objectType) { data = null; objectType = default(ObjectType); @@ -264,7 +264,7 @@ public override int Read(ObjectId oid, out Stream data, out ObjectType objectTyp return (int)ReturnCode.GIT_OK; } - public override int ReadPrefix(string shortSha, out ObjectId id, out Stream data, out ObjectType objectType) + public override int ReadPrefix(string shortSha, out ObjectId id, out UnmanagedMemoryStream data, out ObjectType objectType) { id = null; data = null; diff --git a/LibGit2Sharp/OdbBackend.cs b/LibGit2Sharp/OdbBackend.cs index 04b43a72e..b174ce781 100644 --- a/LibGit2Sharp/OdbBackend.cs +++ b/LibGit2Sharp/OdbBackend.cs @@ -36,19 +36,37 @@ protected abstract OdbBackendOperations SupportedOperations /// Call this method from your implementations of Read and ReadPrefix to allocate a buffer in /// which to return the object's data. /// - /// Number of bytes to allocate - /// An Stream for you to write to and then return. Do not dispose this object before returning it. - protected unsafe Stream Allocate(long bytes) + /// The bytes to be copied to the stream. + /// + /// A Stream already filled with the content of provided the byte array. + /// Do not dispose this object before returning it. + /// + protected UnmanagedMemoryStream AllocateAndBuildFrom(byte[] bytes) { - if (bytes < 0 || - (UIntPtr.Size == sizeof(int) && bytes > int.MaxValue)) + var stream = Allocate(bytes.Length); + + stream.Write(bytes, 0, bytes.Length); + + return stream; + } + + /// + /// Call this method from your implementations of Read and ReadPrefix to allocate a buffer in + /// which to return the object's data. + /// + /// Number of bytes to allocate + /// A Stream for you to write to and then return. Do not dispose this object before returning it. + protected unsafe UnmanagedMemoryStream Allocate(long size) + { + if (size < 0 || + (UIntPtr.Size == sizeof(int) && size > int.MaxValue)) { - throw new ArgumentOutOfRangeException("bytes"); + throw new ArgumentOutOfRangeException("size"); } - IntPtr buffer = Proxy.git_odb_backend_malloc(this.GitOdbBackendPointer, new UIntPtr((ulong)bytes)); + IntPtr buffer = Proxy.git_odb_backend_malloc(this.GitOdbBackendPointer, new UIntPtr((ulong)size)); - return new UnmanagedMemoryStream((byte*)buffer, 0, bytes, FileAccess.ReadWrite); + return new UnmanagedMemoryStream((byte*)buffer, 0, size, FileAccess.ReadWrite); } /// @@ -56,7 +74,7 @@ protected unsafe Stream Allocate(long bytes) /// public abstract int Read( ObjectId id, - out Stream data, + out UnmanagedMemoryStream data, out ObjectType objectType); /// @@ -65,7 +83,7 @@ public abstract int Read( public abstract int ReadPrefix( string shortSha, out ObjectId oid, - out Stream data, + out UnmanagedMemoryStream data, out ObjectType objectType); /// @@ -242,21 +260,18 @@ private unsafe static int Read( return (int)GitErrorCode.Error; } - Stream dataStream = null; + UnmanagedMemoryStream memoryStream = null; try { ObjectType objectType; - int toReturn = odbBackend.Read(new ObjectId(oid), out dataStream, out objectType); + int toReturn = odbBackend.Read(new ObjectId(oid), out memoryStream, out objectType); if (toReturn != 0) { return toReturn; } - // Caller is expected to give us back a stream created with the Allocate() method. - var memoryStream = dataStream as UnmanagedMemoryStream; - if (memoryStream == null) { return (int)GitErrorCode.Error; @@ -276,9 +291,9 @@ private unsafe static int Read( } finally { - if (dataStream != null) + if (memoryStream != null) { - dataStream.Dispose(); + memoryStream.Dispose(); } } @@ -305,7 +320,7 @@ private unsafe static int ReadPrefix( return (int)GitErrorCode.Error; } - Stream dataStream = null; + UnmanagedMemoryStream memoryStream = null; try { @@ -314,16 +329,13 @@ private unsafe static int ReadPrefix( ObjectId oid; ObjectType objectType; - int toReturn = odbBackend.ReadPrefix(shortSha, out oid, out dataStream, out objectType); + int toReturn = odbBackend.ReadPrefix(shortSha, out oid, out memoryStream, out objectType); if (toReturn != 0) { return toReturn; } - // Caller is expected to give us back a stream created with the Allocate() method. - var memoryStream = dataStream as UnmanagedMemoryStream; - if (memoryStream == null) { return (int)GitErrorCode.Error; @@ -343,9 +355,9 @@ private unsafe static int ReadPrefix( } finally { - if (null != dataStream) + if (memoryStream != null) { - dataStream.Dispose(); + memoryStream.Dispose(); } }