~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/maccore/src/CoreFoundation/CFData.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// CFData.cs: Implements the managed CFData
 
3
//
 
4
// Authors:
 
5
//    Rolf Bjarne Kvinge (rolf@xamarin.com)
 
6
//     
 
7
// Copyright 2012 Xamarin Inc
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
// 
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
using System;
 
30
using System.Runtime.InteropServices;
 
31
using MonoMac.Foundation;
 
32
using MonoMac.ObjCRuntime;
 
33
 
 
34
namespace MonoMac.CoreFoundation {
 
35
 
 
36
        class CFData : INativeObject, IDisposable {
 
37
                internal IntPtr handle;
 
38
 
 
39
                public CFData (IntPtr handle)
 
40
                        : this (handle, false)
 
41
                {
 
42
                }
 
43
 
 
44
                public CFData (IntPtr handle, bool owns)
 
45
                {
 
46
                        if (!owns)
 
47
                                CFObject.CFRetain (handle);
 
48
                        this.handle = handle;
 
49
                }
 
50
 
 
51
                ~CFData ()
 
52
                {
 
53
                        Dispose (false);
 
54
                }
 
55
                
 
56
                public void Dispose ()
 
57
                {
 
58
                        Dispose (true);
 
59
                        GC.SuppressFinalize (this);
 
60
                }
 
61
 
 
62
                public IntPtr Handle {
 
63
                        get { return handle; }
 
64
                }
 
65
                
 
66
                protected virtual void Dispose (bool disposing)
 
67
                {
 
68
                        if (handle != IntPtr.Zero){
 
69
                                CFObject.CFRelease (handle);
 
70
                                handle = IntPtr.Zero;
 
71
                        }
 
72
                }
 
73
                
 
74
                public static CFData FromDataNoCopy (IntPtr buffer, int length)
 
75
                {
 
76
                        return new CFData (CFDataCreateWithBytesNoCopy (IntPtr.Zero, buffer, length, CFAllocator.null_ptr), true);
 
77
                }
 
78
                
 
79
                [DllImport (Constants.CoreFoundationLibrary)]
 
80
                extern static IntPtr CFDataCreateWithBytesNoCopy (IntPtr allocator, IntPtr bytes, int len, IntPtr deallocator);
 
81
 
 
82
                public int Length {
 
83
                        get { return CFDataGetLength (handle); }
 
84
                }
 
85
 
 
86
                [DllImport (Constants.CoreFoundationLibrary)]
 
87
                extern static CFIndex CFDataGetLength (IntPtr data);
 
88
 
 
89
                public byte[] GetBuffer ()
 
90
                {
 
91
                        var buffer = new byte [Length];
 
92
                        var ptr = CFDataGetBytePtr (handle);
 
93
                        Marshal.Copy (ptr, buffer, 0, buffer.Length);
 
94
                        return buffer;
 
95
                }
 
96
 
 
97
                [DllImport (Constants.CoreFoundationLibrary)]
 
98
                extern static IntPtr CFDataGetBytePtr (IntPtr data);
 
99
 
 
100
                /*
 
101
                 * Exposes a read-only pointer to the underlying storage.
 
102
                 */
 
103
                public IntPtr Bytes {
 
104
                        get { return CFDataGetBytePtr (handle); }
 
105
                }
 
106
 
 
107
                [DllImport (Constants.CoreFoundationLibrary)]
 
108
                extern static IntPtr CFDataCreate (IntPtr allocator, IntPtr bytes, CFIndex len);
 
109
 
 
110
                public static CFData FromData (IntPtr buffer, int length)
 
111
                {
 
112
                        return new CFData (CFDataCreate (IntPtr.Zero, buffer, length), true);
 
113
                }
 
114
                
 
115
                [DllImport (Constants.CoreFoundationLibrary)]
 
116
                extern static IntPtr CFDataCreateCopy (IntPtr allocator, IntPtr data);
 
117
 
 
118
                public CFData Copy ()
 
119
                {
 
120
                        return new CFData (CFDataCreateCopy (IntPtr.Zero, Handle), true);
 
121
                }
 
122
        }
 
123
}