Important: The information in this document is obsolete and should not be used for new development.
Inherits from | |
Implements | |
Package | com.apple.cocoa.foundation |
Companion guides |
An NSData object stores immutable data in the form of bytes. The size of the data is subject to a 2GB limit.
A count of the number of bytes in the data object
The sequence of bytes contained in the data object
NSData
Creates a data object.
length
Returns the number of bytes contained by the data object.
length
Returns the number of bytes contained by the data object.
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.
public NSData
()
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)
Creates a data object with length bytes from the buffer bytes, starting at start.
public NSData
(byte[] bytes)
Creates a data object with all the data in the buffer bytes.
public NSData
(java.io.File aFile)
Creates a data object with the data from the file specified by aFile.
public NSData
(java.net.URL aURL)
Creates a data object with the data from the location specified by aURL.
public NSData
(NSData aData)
Creates a data object containing the contents of another data object, aData.
public NSData
(String aString)
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.
Creates and returns a data object from the mapped file specified by file.
public static NSData dataWithContentsOfMappedFile
(java.io.File file)
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.
Returns a byte array of length bytes from the receiver’s contents starting at start.
public byte[] bytes
(int start, int length)
Compares the receiving data object to otherData.
public boolean isEqualToData
(NSData otherData)
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.
Returns the number of bytes contained in the receiver.
public int length
()
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)
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
”.
Writes the bytes in the receiver to the location specified by aURL.
public boolean writeToURL
(java.net.URL aURL, boolean atomically)
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
.
© 1997, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-07-24)