PATH |
NSData
- Inherits from:
- Object
- Implements:
- Cloneable
- java.io.Serializable
- NSCoding
- Package:
- com.webobjects.foundation
Class Description
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).
Constants
NSData defines the following constant:
Constant | Type | Description |
EmptyData | NSData | An empty data object, which can be shared to save memory. |
Interfaces Implemented
- Cloneable
- clone
- java.io.Serializable
- NSCoding
- classForCoder
- decodeObject
- encodeWithCoder
Method Types
- 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
Constructors
NSData
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);
Static Methods
dataWithContentsOfFile
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);
dataWithContentsOfMappedFile
public static NSData dataWithContentsOfMappedFile(java.io.File file) throws java.io.IOException
myData = new NSData(new FileInputStream(file), chunkSize);
decodeObject
public static Object decodeObject(NSCoder coder)
See Also: NSCoding
Instance Methods
bytes
public byte[] bytes( int offset, int count)
public byte[] bytes(NSRange range)
public byte[] bytes()
bytesNoCopy
protected byte[] bytesNoCopy()
public byte[] bytesNoCopy(NSMutableRange dataRange)
classForCoder
public Class classForCoder()
clone
public Object clone()
encodeWithCoder
public void encodeWithCoder(NSCoder coder)
equals
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.
hashCode
public int hashCode()
immutableBytes
protected byte[] immutableBytes()
immutableRange
protected NSRange immutableRange()
isEqualToData
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.
length
public int length()
rangeNoCopy
protected NSRange rangeNoCopy()
stream
public java.io.ByteArrayInputStream stream()
subdataWithRange
public NSData subdataWithRange(NSRange range)
toString
public String toString()
writeToFile
public boolean writeToFile(String path)
try { FileOutputStream fileOutputStream = new FileOutputStream(path); myData.writeToStream(fileOutputStream); fileOutputStream.close(); } catch (IOException exception) { /* Do something with the exception */ }
writeToStream
public void writeToStream(java.io.OutputStream outputStream) throws java.io.IOException
See Also: writeToStream
writeToURL
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)