PATH |
- Inherits from:
- Object
- Implements:
- Cloneable
- java.io.Serializable
- NSCoding
- Package:
- com.webobjects.foundation
NSData and its subclass NSMutableData provide data objects, object-oriented wrappers for byte buffers. Data objects let byte arrays take on the behavior of Foundation objects. NSData creates static data objects, and NSMutableData creates dynamic data objects.
Data objects can wrap data of any size. The object contains no information about the data itself (such as its type); the responsibility for deciding how to use the data lies with the client. In particular, it will not handle byte-order swapping when distributed between big-endian and little-endian machines.
Table 0-5 describes the NSData methods that provide the basis for all NSData's other methods; that is, all other methods are implemented in terms of these four. If you create a subclass of NSData, you need only ensure that these base methods work properly. Having done so, you can be sure that all your subclass's inherited methods operate properly.
Method | Description |
bytesNoCopy | Returns the internal byte array that contains the receiver's data. Used by mutable subclasses of NSData. |
immutableBytes | Returns an immutable byte array that contains the receiver's data. |
immutableRange | Returns an immutable NSRange object that specifies the receiver's length. |
rangeNoCopy | Returns the internal NSRange object that specifies the receiver's length. Used by mutable subclasses of NSData. |
To extract a data object that contains a subset of the bytes in another data object, use the subdataWithRange method. To determine if two data objects are equal, use the isEqualToData method, which does a byte-for-byte comparison.
The writeToStream method lets you write the contents of a data object to a stream (a java.io.OutputStream object).
NSData defines the following constant:
Constant | Type | Description |
EmptyData | NSData | An empty data object, which can be shared to save memory. |
- Cloneable
- clone
- java.io.Serializable
- NSCoding
- classForCoder
- decodeObject
- encodeWithCoder
- Constructors
- NSData
- Accessing data
- bytes
- bytesNoCopy
- immutableBytes
- subdataWithRange
- Testing data
- immutableRange
- length
- rangeNoCopy
- isEqualToData
- Storing data
- stream
- writeToStream
- Methods inherited from Object
- equals
- hashCode
- toString
- Deprecated methods
- dataWithContentsOfFile
- dataWithContentsOfMappedFile
- writeToFile
- writeToURL
public NSData()
public NSData(NSData data)
public NSData(String string)
NSData(string.getBytes())
instead.
public NSData(byte[] bytes)
public NSData( byte[] bytes, int offset, int count)
public NSData( byte[] bytes, NSRange range)
public NSData( byte[] bytes, NSRange range, boolean noCopy)
public NSData(java.io.File file) throws java.io.IOException
NSData(new FileInputStream(file),chunkSize)
instead.
public NSData( java.io.InputStream inputStream, int chunkSize) throws java.io.IOException
public NSData(java.net.URL url) throws java.io.IOException
URLConnection connection = url.openConnection(); connection.connect(); NSData myData = new NSData(connection.getInputStream(),chunkSize);
public static NSData dataWithContentsOfFile(java.io.File file) throws java.io.IOException
myData = new NSData(new FileInputStream(file), chunkSize);
public static NSData dataWithContentsOfFile(String path) throws java.io.IOException
myData = new NSData(new FileInputStream(path), chunkSize);
public static NSData dataWithContentsOfMappedFile(java.io.File file) throws java.io.IOException
myData = new NSData(new FileInputStream(file), chunkSize);
public static Object decodeObject(NSCoder coder)
See Also: NSCoding
public byte[] bytes( int offset, int count)
public byte[] bytes(NSRange range)
public byte[] bytes()
protected byte[] bytesNoCopy()
public byte[] bytesNoCopy(NSMutableRange dataRange)
public Class classForCoder()
public Object clone()
public void encodeWithCoder(NSCoder coder)
public boolean equals(Object anObject)
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.
public int hashCode()
protected byte[] immutableBytes()
protected NSRange immutableRange()
public boolean isEqualToData(NSData otherData)
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.
public int length()
protected NSRange rangeNoCopy()
public java.io.ByteArrayInputStream stream()
public NSData subdataWithRange(NSRange range)
public String toString()
public boolean writeToFile(String path)
try { FileOutputStream fileOutputStream = new FileOutputStream(path); myData.writeToStream(fileOutputStream); fileOutputStream.close(); } catch (IOException exception) { /* Do something with the exception */ }
public void writeToStream(java.io.OutputStream outputStream) throws java.io.IOException
See Also: writeToStream
public boolean writeToURL( java.net.URL url, boolean atomically)
try { FileOutputStream fileOutputStream = new FileOutputStream(url.getFile()); myData.writeToStream(fileOutputStream); fileOutputStream.close(); } catch (IOException exception) { /* Do something with the exception */ }
See Also: writeToStream
© 2001 Apple Computer, Inc. (Last Published April 17, 2001)