Derived from | |
Framework | CoreFoundation/CoreFoundation.h |
Declared in | CFData.h |
Companion guides |
CFData and its derived mutable type, CFMutableData, provide support for data objects, object-oriented wrappers for byte buffers. Data objects let simple allocated buffers (that is, data with no embedded pointers) take on the behavior of Core Foundation objects. CFData creates static data objects, and CFMutableData creates dynamic data objects. Data objects are typically used for raw data storage.
You use the CFDataCreate
and CFDataCreateCopy
functions to create static data objects. These functions make a new copy of the supplied data. To create a data object that uses the supplied buffer instead of making a separate copy, use the CFDataCreateWithBytesNoCopy
function. You use the CFDataGetBytes
function to retrieve the bytes and the CFDataGetLength
function to get the length of the bytes.
CFData is “toll-free bridged” with its Cocoa Foundation counterpart, NSData. What this means is that the Core Foundation type is interchangeable in function or method calls with the bridged Foundation object. In other words, in a method where you see an NSData *
parameter, you can pass in a CFDataRef
, and in a function where you see a CFDataRef
parameter, you can pass in an NSData
instance. This also applies to concrete subclasses of NSData
. See Interchangeable Data Types for more information on toll-free bridging.
Returns the type identifier for the CFData opaque type.
CFTypeID CFDataGetTypeID ( void );
The type identifier for the CFData opaque type.
CFMutableData objects have the same type identifier as CFData objects.
CFData.h
Creates an immutable CFData object using data copied from a specified byte buffer.
CFDataRef CFDataCreate ( CFAllocatorRef allocator, const UInt8 *bytes, CFIndex length );
The allocator to use to allocate memory for the new object. Pass NULL
or kCFAllocatorDefault
to use the current default allocator.
A pointer to the byte buffer that contains the raw data to be copied into theData.
The number of bytes in the buffer (bytes).
A new CFData object, or NULL
if there was a problem creating the object. Ownership follows the Create Rule.
You must supply a count of the bytes in the buffer. This function always copies the bytes in the provided buffer into internal storage.
CFData.h
Creates an immutable copy of a CFData object.
CFDataRef CFDataCreateCopy ( CFAllocatorRef allocator, CFDataRef theData );
The allocator to use to allocate memory for the new object. Pass NULL
or kCFAllocatorDefault
to use the current default allocator.
The CFData object to copy.
An immutable copy of theData, or NULL if there was a problem creating the object. Ownership follows the Create Rule.
The resulting object has the same byte contents as the original object, but it is always immutable. If the specified allocator and the allocator of the original object are the same, and the string is already immutable, this function may simply increment the retain count without making a true copy. To the caller, however, the resulting object is a true immutable copy, except the operation was more efficient.
Use this function when you need to pass a CFData object into another function by value (not reference).
CFData.h
Creates an immutable CFData object from an external (client-owned) byte buffer.
CFDataRef CFDataCreateWithBytesNoCopy ( CFAllocatorRef allocator, const UInt8 *bytes, CFIndex length, CFAllocatorRef bytesDeallocator );
The allocator to use to allocate memory for the new object. Pass NULL
or kCFAllocatorDefault
to use the current default allocator.
A pointer to the byte buffer to be used as the backing store of the CFData object.
The number of bytes in the buffer bytes.
The allocator to use to deallocate the external buffer when the CFData object is deallocated. If the default allocator is suitable for this purpose, pass NULL
or kCFAllocatorDefault
. If you do not want the created CFData object to deallocate the buffer (that is, you assume responsibility for freeing it yourself), pass kCFAllocatorNull
.
A new CFData object, or NULL
if there was a problem creating the object. Ownership follows the Create Rule.
This function creates an immutable CFData object from a buffer of unstructured bytes. Unless the situation warrants otherwise, the created object does not copy the external buffer to internal storage but instead uses the buffer as its backing store. However, you should never count on the object using the external buffer since it could copy the buffer to internal storage or might even dump the buffer altogether and use alternative means for storing the bytes.
CFData.h
Returns a read-only pointer to the bytes of a CFData object.
const UInt8 * CFDataGetBytePtr ( CFDataRef theData );
The CFData object to examine.
A read-only pointer to the bytes associated with theData.
This function is guaranteed to return a pointer to a CFData object's internal bytes. CFData, unlike CFString, does not hide its internal storage.
CFData.h
Copies the byte contents of a CFData object to an external buffer.
void CFDataGetBytes ( CFDataRef theData, CFRange range, UInt8 *buffer );
The CFData object to examine.
The range of bytes in theData to get. To get all of the contents, pass CFRangeMake(0,CFDataGetLength(theData))
.
A pointer to the byte buffer of length range.length
that is allocated on the stack or heap. On return, the buffer contains the requested range of bytes.
CFData.h
Returns the number of bytes contained by a CFData object.
CFIndex CFDataGetLength ( CFDataRef theData );
The CFData object to examine.
An index that specifies the number of bytes in theData.
CFData.h
A reference to an immutable CFData object.
typedef const struct __CFData *CFDataRef;
CFData.h
© 2003, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-02-07)