Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RawContentStream.cs « Core « LibGit2Sharp - github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d02fe8b81d68f1130fab2b503ed10c4ede5499e4 (plain)
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);
        }
   }
}