forked from libgit2/libgit2sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRawContentStream.cs
More file actions
68 lines (59 loc) · 1.83 KB
/
RawContentStream.cs
File metadata and controls
68 lines (59 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.IO;
using LibGit2Sharp.Core.Handles;
namespace LibGit2Sharp.Core
{
internal class RawContentStream : UnmanagedMemoryStream
{
private readonly GitObjectSafeHandle handle;
private readonly ICollection<IDisposable> linkedResources;
internal unsafe RawContentStream(
GitObjectSafeHandle handle,
Func<GitObjectSafeHandle, IntPtr> bytePtrProvider,
Func<GitObjectSafeHandle, long> sizeProvider,
ICollection<IDisposable> linkedResources = null)
: base((byte*)Wrap(handle, bytePtrProvider, linkedResources).ToPointer(),
Wrap(handle, sizeProvider, linkedResources))
{
this.handle = handle;
this.linkedResources = linkedResources;
}
private static T Wrap<T>(
GitObjectSafeHandle handle,
Func<GitObjectSafeHandle, T> provider,
IEnumerable<IDisposable> linkedResources)
{
T value;
try
{
value = provider(handle);
}
catch
{
Dispose(handle, linkedResources);
throw;
}
return value;
}
private static void Dispose(
GitObjectSafeHandle handle,
IEnumerable<IDisposable> linkedResources)
{
handle.SafeDispose();
if (linkedResources == null)
{
return;
}
foreach (IDisposable linkedResource in linkedResources)
{
linkedResource.Dispose();
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
Dispose(handle, linkedResources);
}
}
}