Next Page > Hide TOC

Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

NSData

Inherits from
Implements
Package
com.apple.cocoa.foundation
Companion guides

Class at a Glance

An NSData object stores immutable data in the form of bytes. The size of the data is subject to a 2GB limit.

Principal Attributes

Creation

NSData

Creates a data object.

Commonly Used Methods

length

Returns the number of bytes contained by the data object.

Primitive Methods

length

Returns the number of bytes contained by the data object.

Overview

NSData and its subclass NSMutableData provide 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 Foundation objects. NSData creates static data objects, and NSMutableData creates dynamic data objects. NSData and NSMutableData are typically used for data storage and are also useful in Distributed Objects applications, where data contained in data objects can be copied or moved between applications.

The mutable subclass of NSData is NSMutableData.

Tasks

Constructors

Creating Data Objects

Accessing Data

Testing Data

Storing Data

Constructors

NSData

public NSData()

Discussion

Creates an empty data object. This method is declared primarily for the use of mutable subclasses of NSData.

public NSData(byte[] bytes, int start, int length)

Discussion

Creates a data object with length bytes from the buffer bytes, starting at start.

public NSData(byte[] bytes)

Discussion

Creates a data object with all the data in the buffer bytes.

public NSData(java.io.File aFile)

Discussion

Creates a data object with the data from the file specified by aFile.

public NSData(java.net.URL aURL)

Discussion

Creates a data object with the data from the location specified by aURL.

public NSData(NSData aData)

Discussion

Creates a data object containing the contents of another data object, aData.

public NSData(String aString)

Discussion

Deprecated. To create an NSData from a property list use propertyListFromString; to initialize an NSData from a file, pass either a java.io.file or a java.net.url object.

Static Methods

dataWithContentsOfMappedFile

Creates and returns a data object from the mapped file specified by file.

public static NSData dataWithContentsOfMappedFile(java.io.File file)

Discussion

Returns null if the data object could not be creates. Because of file mapping restrictions, this method should only be used if the file is guaranteed to exist for the duration of the data object’s existence.

This methods assumes mapped files are available from the underlying operating system. A mapped file uses virtual memory techniques to avoid copying pages of the file into memory until they are actually needed.

Instance Methods

bytes

Returns a byte array of length bytes from the receiver’s contents starting at start.

public byte[] bytes(int start, int length)

isEqualToData

Compares the receiving data object to otherData.

public boolean isEqualToData(NSData otherData)

Discussion

If the contents of otherData are equal to the contents of the receiver, this method returns true. If not, it returns false. Two data objects are equal if they hold the same number of bytes, and if the bytes at the same position in the objects are the same.

length

Returns the number of bytes contained in the receiver.

public int length()

subdataWithRange

Returns a data object containing a copy of the receiver’s bytes that fall within the limits specified by range.

public NSData subdataWithRange(NSRange range)

Discussion

If range isn’t within the receiver’s range of bytes, a RangeException is thrown.

For example, the following code excerpt initializes a data object, data2, to contain a subrange of data1:

String myString = "ABCDEFG";
range = new NSRange(2,4);
NSData data1 = new NSData(myString.getBytes());
NSData data2 = data1.subdataWithRange(range);

The result of this excerpt is that data2 contains “CDEF”.

writeToURL

Writes the bytes in the receiver to the location specified by aURL.

public boolean writeToURL(java.net.URL aURL, boolean atomically)

Discussion

If atomically is true, the data is written to a backup location, and then, assuming no errors occur, the backup location is renamed to the specified name. Otherwise, the data is written directly to the specified location. atomically is ignored if aURL is not of a type the supports atomic writes.

This method returns true if the operation succeeds; otherwise, it returns false.



Next Page > Hide TOC


© 1997, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-07-24)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.