Up

NSData class reference

Authors

Andrew Kachites McCallum (mccallum@gnu.ai.mit.edu)
Richard Frith-Macdonald (richard@brainstorm.co.uk)

Version: 1.146

Date: 2005/02/22 11:22:44

Copyright: (C) 1995, 1996, 1997, 2000, 2002 Free Software Foundation, Inc.


Contents -

  1. Software documentation for the NSData class
  2. Software documentation for the NSMutableData class
  3. Software documentation for the NSData(GNUstepExtensions) category
  4. Software documentation for the NSData(GSCategories) category
  5. Software documentation for the NSMutableData(GNUstepExtensions) category

Software documentation for the NSData class

NSData : NSObject

Declared in:
Foundation/NSData.h
Conforms to:
NSCoding
NSCopying
NSMutableCopying
Standards:

Class for storing a byte array. Methods for initializing from memory a file, or the network are provided, as well as the ability to write to a file or the network. If desired, object can take over management of a pre-allocated buffer (with malloc or similar), free'ing it when deallocated.

The data buffer at any given time has a capacity, which is the size of its allocated memory area, in bytes, and a length, which is the length of data it is currently storing.

Method summary

data 

+ (id) data;

Returns an empty data object.


dataWithBytes: length: 

+ (id) dataWithBytes: (const void*)bytes length: (unsigned int)length;

Returns an autoreleased data object containing data copied from bytes and with the specified length. Invokes -initWithBytes:length:


dataWithBytesNoCopy: length: 

+ (id) dataWithBytesNoCopy: (void*)bytes length: (unsigned int)length;

Returns an autoreleased data object encapsulating the data at bytes and with the specified length. Invokes -initWithBytesNoCopy:length:freeWhenDone: with YES


dataWithBytesNoCopy: length: freeWhenDone: 

+ (id) dataWithBytesNoCopy: (void*)aBuffer length: (unsigned int)bufferSize freeWhenDone: (BOOL)shouldFree;

Returns an autoreleased data object encapsulating the data at bytes and with the specified length. Invokes -initWithBytesNoCopy:length:freeWhenDone:


dataWithContentsOfFile: 

+ (id) dataWithContentsOfFile: (NSString*)path;

Returns a data object encapsulating the contents of the specified file. Invokes -initWithContentsOfFile:


dataWithContentsOfMappedFile: 

+ (id) dataWithContentsOfMappedFile: (NSString*)path;

Returns a data object encapsulating the contents of the specified file mapped directly into memory. Invokes -initWithContentsOfMappedFile:


dataWithContentsOfURL: 

+ (id) dataWithContentsOfURL: (NSURL*)url;

Retrieves the information at the specified url and returns an NSData instance encapsulating it.


dataWithData: 

+ (id) dataWithData: (NSData*)data;

Returns an autoreleased instance initialised by copying the contents of data.


bytes 

- (const void*) bytes;
Subclasses should override this method.

Returns a pointer to the data encapsulated by the receiver.


description 

- (NSString*) description;

Returns a short description of this object.


deserializeAlignedBytesLengthAtCursor: 

- (unsigned int) deserializeAlignedBytesLengthAtCursor: (unsigned int*)cursor;

Copies data from buffer starting from cursor. Deprecated. Use [-getBytes:] and related methods instead.


deserializeBytes: length: atCursor: 

- (void) deserializeBytes: (void*)buffer length: (unsigned int)bytes atCursor: (unsigned int*)cursor;

Copies data from buffer starting from cursor. Deprecated. Use [-getBytes:] and related methods instead.


deserializeDataAt: ofObjCType: atCursor: context: 

- (void) deserializeDataAt: (void*)data ofObjCType: (const char*)type atCursor: (unsigned int*)cursor context: (id<NSObjCTypeSerializationCallBack>)callback;

Description forthcoming.


deserializeIntAtCursor: 

- (int) deserializeIntAtCursor: (unsigned int*)cursor;

Retrieve an int from this data, which is assumed to be in network (big-endian) byte order. Cursor refers to byte position.


deserializeIntAtIndex: 

- (int) deserializeIntAtIndex: (unsigned int)index;

Retrieve an int from this data, which is assumed to be in network (big-endian) byte order. Index refers to byte position.


deserializeInts: count: atCursor: 

- (void) deserializeInts: (int*)intBuffer count: (unsigned int)numInts atCursor: (unsigned int*)cursor;

Retrieve ints from intBuffer, which is assumed to be in network (big-endian) byte order. Count refers to number of ints, but index refers to byte position.


deserializeInts: count: atIndex: 

- (void) deserializeInts: (int*)intBuffer count: (unsigned int)numInts atIndex: (unsigned int)index;

Retrieve ints from intBuffer, which is assumed to be in network (big-endian) byte order. Count refers to number of ints, but index refers to byte position.


getBytes: 

- (void) getBytes: (void*)buffer;

Copies the contents of the memory encapsulated by the receiver into the specified buffer. The buffer must be large enough to contain -length bytes of data... if it isn't then a crash is likely to occur.
Invokes -getBytes:range: with the range set to the whole of the receiver.


getBytes: length: 

- (void) getBytes: (void*)buffer length: (unsigned int)length;

Copies length bytes of data from the memory encapsulated by the receiver into the specified buffer. The buffer must be large enough to contain length bytes of data... if it isn't then a crash is likely to occur.
Invokes -getBytes:range: with the range set to iNSMakeRange(0, length)


getBytes: range: 

- (void) getBytes: (void*)buffer range: (NSRange)aRange;

Copies data from the memory encapsulated by the receiver (in the range specified by aRange) into the specified buffer.
The buffer must be large enough to contain the data... if it isn't then a crash is likely to occur.
If aRange specifies a range which does not entirely lie within the receiver, an exception is raised.


initWithBytes: length: 

- (id) initWithBytes: (const void*)aBuffer length: (unsigned int)bufferSize;

Makes a copy of bufferSize bytes of data at aBuffer, and passes it to -initWithBytesNoCopy:length:freeWhenDone: with a YES argument in order to initialise the receiver. Returns the result.


initWithBytesNoCopy: length: 

- (id) initWithBytesNoCopy: (void*)aBuffer length: (unsigned int)bufferSize;

Invokes -initWithBytesNoCopy:length:freeWhenDone: with the last argument set to YES. Returns the resulting initialised data object (which may not be the receiver).


initWithBytesNoCopy: length: freeWhenDone: 

- (id) initWithBytesNoCopy: (void*)aBuffer length: (unsigned int)bufferSize freeWhenDone: (BOOL)shouldFree;
This is a designated initialiser for the class.
Subclasses should override this method.

Initialises the receiver.
The value of aBuffer is a pointer to something to be stored.
The value of bufferSize is the number of bytes to use.
The value of shouldFree specifies whether the receiver should attempt to free the memory pointer to by aBuffer when the receiver is deallocated ... ie. it says whether the receiver owns the memory. Supplying the wrong value here will lead to memory leaks or crashes.


initWithContentsOfFile: 

- (id) initWithContentsOfFile: (NSString*)path;

Initialises the receiver with the contents of the specified file.
Returns the resulting object.
Returns nil if the file does not exist or can not be read for some reason.


initWithContentsOfMappedFile: 

- (id) initWithContentsOfMappedFile: (NSString*)path;

Initialize with data pointing to contents of file at path. Bytes are only "swapped in" as needed. File should not be moved or deleted for the life of this object.


initWithContentsOfURL: 

- (id) initWithContentsOfURL: (NSURL*)url;

Initialize with data pointing to contents of URL, which will be retrieved immediately in a blocking manner.


initWithData: 

- (id) initWithData: (NSData*)data;

Initializes by copying data's bytes into a new buffer.


isEqualToData: 

- (BOOL) isEqualToData: (NSData*)other;

Returns a boolean value indicating if the receiver and other contain identical data (using a byte by byte comparison). Assumes that the other object is an NSData instance... may raise an exception if it isn't.


length 

- (unsigned int) length;
Subclasses should override this method.

Returns the number of bytes of data encapsulated by the receiver.


subdataWithRange: 

- (NSData*) subdataWithRange: (NSRange)aRange;

Returns an NSData instance encapsulating the memory from the reciever specified by the range aRange.
If aRange specifies a range which does not entirely lie within the receiver, an exception is raised.


writeToFile: atomically: 

- (BOOL) writeToFile: (NSString*)path atomically: (BOOL)useAuxiliaryFile;

Writes a copy of the data encapsulated by the receiver to a file at path. If the useAuxiliaryFile flag is YES, this writes to a temporary file and then renames that to the file at path, thus ensuring that path exists and does not contain partially written data at any point.

On success returns YES, on failure returns NO.


writeToURL: atomically: 

- (BOOL) writeToURL: (NSURL*)anURL atomically: (BOOL)flag;

Writes a copy of the contents of the receiver to the specified URL.


Software documentation for the NSMutableData class

NSMutableData : NSData

Declared in:
Foundation/NSData.h
Standards:

Mutable version of NSData . Methods are provided for appending and replacing bytes in the buffer, which will be grown as needed.

Method summary

dataWithCapacity: 

+ (id) dataWithCapacity: (unsigned int)numBytes;

New instance with buffer of given numBytes with length of valid data set to zero. Note that capacity will be automatically increased as necessary.


dataWithLength: 

+ (id) dataWithLength: (unsigned int)length;

New instance with buffer of capacity and valid data size equal to given length in bytes. The buffer contents are set to zero. The length of valid data is set to zero. Note that buffer will be automatically increased as necessary.


appendBytes: length: 

- (void) appendBytes: (const void*)aBuffer length: (unsigned int)bufferSize;

Appends bufferSize bytes from aBuffer to data, increasing capacity if necessary.


appendData: 

- (void) appendData: (NSData*)other;

Copies and appends data from other to data, increasing capacity if necessary.


increaseLengthBy: 

- (void) increaseLengthBy: (unsigned int)extraLength;

Increases buffer length by given number of bytes, filling the new space with zeros.


initWithCapacity: 

- (id) initWithCapacity: (unsigned int)capacity;
This is a designated initialiser for the class.

Initialize with buffer capable of holding size bytes. The length of valid data is initially set to zero.


initWithLength: 

- (id) initWithLength: (unsigned int)length;

Initialize with buffer of capacity equal to length, and with the length of valid data set to length. Data is set to zero.


mutableBytes 

- (void*) mutableBytes;

Returns a pointer to the data storage of the receiver.
Modifications to the memory pointed to by this pointer will change the contents of the object. It is important that your code should not try to modify the memory beyond the number of bytes given by the -length method.

NB. if the object is released, or any method that changes its size or content is called, then the pointer previously returned by this method may cease to be valid.

This is a 'primitive' method... you need to implement it if you write a subclass of NSMutableData.


replaceBytesInRange: withBytes: 

- (void) replaceBytesInRange: (NSRange)aRange withBytes: (const void*)bytes;

Replaces the bytes of data in the specified range with a copy of the new bytes supplied.
If the location of the range specified lies beyond the end of the data ( [self length] < range.location) then a range exception is raised.
Otherwise, if the range specified extends beyond the end of the data, then the size of the data is increased to accomodate the new bytes.


replaceBytesInRange: withBytes: length: 

- (void) replaceBytesInRange: (NSRange)aRange withBytes: (const void*)bytes length: (unsigned int)length;

Replace the content of the receiver which lies in aRange with the specified length of data from the buffer pointed to by bytes.
The size of the receiver is adjusted to allow for the change.


resetBytesInRange: 

- (void) resetBytesInRange: (NSRange)aRange;

Set bytes in aRange to 0.


serializeAlignedBytesLength: 

- (void) serializeAlignedBytesLength: (unsigned int)length;

Does not act as the name suggests. Instead, serializes length itself as an int into buffer.


serializeDataAt: ofObjCType: context: 

- (void) serializeDataAt: (const void*)data ofObjCType: (const char*)type context: (id<NSObjCTypeSerializationCallBack>)callback;

Description forthcoming.


serializeInt: 

- (void) serializeInt: (int)value;

Serialize an int into this object's data buffer, swapping it to network (big-endian) byte order first.


serializeInt: atIndex: 

- (void) serializeInt: (int)value atIndex: (unsigned int)index;

Serialize an int into this object's data buffer at index (replacing anything there currently), swapping it to network (big-endian) byte order first.


serializeInts: count: 

- (void) serializeInts: (int*)intBuffer count: (unsigned int)numInts;

Serialize one or more ints into this object's data buffer, swapping them to network (big-endian) byte order first.


serializeInts: count: atIndex: 

- (void) serializeInts: (int*)intBuffer count: (unsigned int)numInts atIndex: (unsigned int)index;

Serialize one or more ints into this object's data buffer at index (replacing anything there currently), swapping them to network (big-endian) byte order first.


setData: 

- (void) setData: (NSData*)data;

Replaces contents of buffer with contents of data's buffer, increasing or shrinking capacity to match.


setLength: 

- (void) setLength: (unsigned int)size;

Sets the length of the NSMutableData object. If the length is increased, the newly allocated data area is filled with zero bytes.

This is a 'primitive' method... you need to implement it if you write a subclass of NSMutableData.


Software documentation for the NSData(GNUstepExtensions) category

NSData(GNUstepExtensions)

Declared in:
Foundation/NSData.h
Standards:

Provides some shared-memory extensions to NSData .

Method summary

dataWithSharedBytes: length: 

+ (id) dataWithSharedBytes: (const void*)bytes length: (unsigned int)length;

New instance with given bytes in shared memory.


dataWithShmID: length: 

+ (id) dataWithShmID: (int)anID length: (unsigned int)length;

New instance with given shared memory ID.


deserializeTypeTag: andCrossRef: atCursor: 

- (void) deserializeTypeTag: (unsigned char*)tag andCrossRef: (unsigned int*)ref atCursor: (unsigned int*)cursor;

Description forthcoming.


Software documentation for the NSData(GSCategories) category

NSData(GSCategories)

Declared in:
Foundation/NSData.h
Standards:

Extension methods for the NSData class- mainly conversion utilities.

Method summary

hexadecimalRepresentation 

- (NSString*) hexadecimalRepresentation;

Returns an NSString object containing an ASCII hexadecimal representation of the receiver. This means that the returned object will contain exactly twice as many characters as there are bytes as the receiver, as each byte in the receiver is represented by two hexadecimal digits.
The high order four bits of each byte is encoded before the low order four bits. Capital letters 'A' to 'F' are used to represent values from 10 to 15.
If you need the hexadecimal representation as raw byte data, use code like -

   hexData = [[sourceData hexadecimalRepresentation]
     dataUsingEncoding: NSASCIIStringEncoding];
 


initWithHexadecimalRepresentation: 

- (id) initWithHexadecimalRepresentation: (NSString*)string;

Initialises the receiver with the supplied string data which contains a hexadecimal coding of the bytes. The parsing of the string is fairly tolerant, ignoring whitespace and permitting both upper and lower case hexadecimal digits (the -hexadecimalRepresentation method produces a string using only uppercase digits with no white spaqce).
If the string does not contain one or more pairs of hexadecimal digits then an exception is raised.


md5Digest 

- (NSData*) md5Digest;

Creates an MD5 digest of the information stored in the receiver and returns it as an autoreleased 16 byte NSData object.
If you need to produce a digest of string information, you need to decide what character encoding is to be used and convert your string to a data object of that encoding type first using the [NSString -dataUsingEncoding:] method -

   myDigest = [[myString dataUsingEncoding: NSUTF8StringEncoding] md5Digest];
 
If you need to use the digest in a human readable form, you will probably want it to be seen as 32 hexadecimal digits, and can do that using the -hexadecimalRepresentation method.


uudecodeInto: name: mode: 

- (BOOL) uudecodeInto: (NSMutableData*)decoded name: (NSString**)namePtr mode: (int*)modePtr;

Decodes the source data from uuencoded and return the result.
Returns the encoded file name in namePtr if it is not null. Returns the encoded file mode in modePtr if it is not null.


uuencodeInto: name: mode: 

- (BOOL) uuencodeInto: (NSMutableData*)encoded name: (NSString*)name mode: (int)mode;

Encode the source data to uuencoded.
Uses the supplied name as the filename in the encoded data, and says that the file mode is as specified.
If no name is supplied, uses untitled as the name.


Software documentation for the NSMutableData(GNUstepExtensions) category

NSMutableData(GNUstepExtensions)

Declared in:
Foundation/NSData.h
Standards:

Provides some additional methods to NSData .

Method summary

capacity 

- (unsigned int) capacity;

Returns current capacity of data buffer.


serializeTypeTag: 

- (void) serializeTypeTag: (unsigned char)tag;

Description forthcoming.


serializeTypeTag: andCrossRef: 

- (void) serializeTypeTag: (unsigned char)tag andCrossRef: (unsigned int)xref;

Description forthcoming.


setCapacity: 

- (id) setCapacity: (unsigned int)newCapacity;

Sets current capacity of data buffer. Unlike -setLength: , this will shrink the buffer if requested.


shmID 

- (int) shmID;

Return shared memory ID, if using one, else -1.



Up