2
// CFData.cs: Implements the managed CFData
5
// Rolf Bjarne Kvinge (rolf@xamarin.com)
7
// Copyright 2012 Xamarin Inc
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:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
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.
30
using System.Runtime.InteropServices;
31
using MonoMac.Foundation;
32
using MonoMac.ObjCRuntime;
34
namespace MonoMac.CoreFoundation {
36
class CFData : INativeObject, IDisposable {
37
internal IntPtr handle;
39
public CFData (IntPtr handle)
40
: this (handle, false)
44
public CFData (IntPtr handle, bool owns)
47
CFObject.CFRetain (handle);
56
public void Dispose ()
59
GC.SuppressFinalize (this);
62
public IntPtr Handle {
63
get { return handle; }
66
protected virtual void Dispose (bool disposing)
68
if (handle != IntPtr.Zero){
69
CFObject.CFRelease (handle);
74
public static CFData FromDataNoCopy (IntPtr buffer, int length)
76
return new CFData (CFDataCreateWithBytesNoCopy (IntPtr.Zero, buffer, length, CFAllocator.null_ptr), true);
79
[DllImport (Constants.CoreFoundationLibrary)]
80
extern static IntPtr CFDataCreateWithBytesNoCopy (IntPtr allocator, IntPtr bytes, int len, IntPtr deallocator);
83
get { return CFDataGetLength (handle); }
86
[DllImport (Constants.CoreFoundationLibrary)]
87
extern static CFIndex CFDataGetLength (IntPtr data);
89
public byte[] GetBuffer ()
91
var buffer = new byte [Length];
92
var ptr = CFDataGetBytePtr (handle);
93
Marshal.Copy (ptr, buffer, 0, buffer.Length);
97
[DllImport (Constants.CoreFoundationLibrary)]
98
extern static IntPtr CFDataGetBytePtr (IntPtr data);
101
* Exposes a read-only pointer to the underlying storage.
103
public IntPtr Bytes {
104
get { return CFDataGetBytePtr (handle); }
107
[DllImport (Constants.CoreFoundationLibrary)]
108
extern static IntPtr CFDataCreate (IntPtr allocator, IntPtr bytes, CFIndex len);
110
public static CFData FromData (IntPtr buffer, int length)
112
return new CFData (CFDataCreate (IntPtr.Zero, buffer, length), true);
115
[DllImport (Constants.CoreFoundationLibrary)]
116
extern static IntPtr CFDataCreateCopy (IntPtr allocator, IntPtr data);
118
public CFData Copy ()
120
return new CFData (CFDataCreateCopy (IntPtr.Zero, Handle), true);