8000 Introduce OdbBackend.AllocateAndBuildFrom() · apfunk/libgit2sharp@5dcf507 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5dcf507

Browse files
committed
Introduce OdbBackend.AllocateAndBuildFrom()
1 parent dd9d358 commit 5dcf507

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

LibGit2Sharp/OdbBackend.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,37 @@ protected abstract OdbBackendOperations SupportedOperations
3636
/// Call this method from your implementations of Read and ReadPrefix to allocate a buffer in
3737
/// which to return the object's data.
3838
/// </summary>
39-
/// <param name="bytes">Number of bytes to allocate</param>
40-
/// <returns>An Stream for you to write to and then return. Do not dispose this object before returning it.</returns>
41-
protected unsafe UnmanagedMemoryStream Allocate(long bytes)
39+
/// <param name="bytes">The bytes to be copied to the stream.</param>
40+
/// <returns>
41+
/// A Stream already filled with the content of provided the byte array.
42+
/// Do not dispose this object before returning it.
43+
/// </returns>
44+
protected UnmanagedMemoryStream AllocateAndBuildFrom(byte[] bytes)
4245
{
43-
if (bytes < 0 ||
44-
(UIntPtr.Size == sizeof(int) && bytes > int.MaxValue))
46+
var stream = Allocate(bytes.Length);
47+
48+
stream.Write(bytes, 0, bytes.Length);
49+
50+
return stream;
51+
}
52+
53+
/// <summary>
54+
/// Call this method from your implementations of Read and ReadPrefix to allocate a buffer in
55+
/// which to return the object's data.
56+
/// </summary>
57+
/// <param name="size">Number of bytes to allocate</param>
58+
/// <returns>A Stream for you to write to and then return. Do not dispose this object before returning it.</returns>
59+
protected unsafe UnmanagedMemoryStream Allocate(long size)
60+
{
61+
if (size < 0 ||
62+
(UIntPtr.Size == sizeof(int) && size > int.MaxValue))
4563
{
46-
throw new ArgumentOutOfRangeException("bytes");
64+
throw new ArgumentOutOfRangeException("size");
4765
}
4866

49-
IntPtr buffer = Proxy.git_odb_backend_malloc(this.GitOdbBackendPointer, new UIntPtr((ulong)bytes));
67+
IntPtr buffer = Proxy.git_odb_backend_malloc(this.GitOdbBackendPointer, new UIntPtr((ulong)size));
5068

51-
return new UnmanagedMemoryStream((byte*)buffer, 0, bytes, FileAccess.ReadWrite);
69+
return new UnmanagedMemoryStream((byte*)buffer, 0, size, FileAccess.ReadWrite);
5270
}
5371

5472
/// <summary>

0 commit comments

Comments
 (0)
0