|
WebObjects 5.2.3 | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.webobjects.foundation.NSData
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.
The following table 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 to ensure that only these base methods work properly. Having done so, you can be sure that all the 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 subdataWithRange
. To determine if
two data objects are equal, use isEqualToData
, which does a
byte-for-byte comparison.
writeToStream
lets you write the contents of a data object
to a stream (a java.io.OutputStream object).
bytesNoCopy()
,
immutableBytes()
,
immutableRange()
,
rangeNoCopy()
,
isEqualToData(NSData otherData)
,
subdataWithRange(NSRange range)
,
writeToStream(java.io.OutputStream outputStream)
,
Serialized FormNested Class Summary |
Nested classes inherited from class com.webobjects.foundation.NSCoding |
NSCoding.Support |
Field Summary | |
static NSData |
EmptyData
An empty data object, which can be shared to save memory. |
Constructor Summary | |
NSData()
Creates an empty data object. |
|
NSData(byte[] bytes)
Creates a data object with all the data in the byte array bytes . |
|
NSData(byte[] bytes,
int offset,
int count)
Creates a data object with the bytes from the byte array bytes
that fall in the range specified by offset and count . |
|
NSData(byte[] bytes,
NSRange range)
Creates a data object with the bytes from the byte array bytes
that fall in the range specified by range . |
|
NSData(byte[] bytes,
NSRange range,
boolean noCopy)
Creates a data object with the bytes from the byte array bytes
that fall in the range specified by range . |
|
NSData(File file)
Deprecated. |
|
NSData(InputStream inputStream,
int chunkSize)
Creates a data object with the data from the stream specified by inputStream . |
|
NSData(NSData otherData)
Creates a data object containing the contents of another data object, otherData . |
|
NSData(String value)
Deprecated. |
|
NSData(String value,
String encoding)
This constructor creates a new NSData object from a String |
|
NSData(URL url)
Creates a new NSData from the contents of url. |
Method Summary | |
byte[] |
bytes()
Returns a byte array containing all of the receiver's contents |
byte[] |
bytes(int offset,
int length)
Returns a byte array containing all of the receiver's contents |
byte[] |
bytes(NSRange range)
Returns a byte array containing all of the receiver's contents |
protected byte[] |
bytesNoCopy()
Due to the internal implementation of NSData, the array returned by this primitive method may contain bytes that are not actually a part of the receiver's data. |
byte[] |
bytesNoCopy(NSMutableRange range)
The receiver's actual data is composed of the returned array's bytes that lie within range . |
Class |
classForCoder()
Conformance to NSCoding. |
Object |
clone()
Since NSData objects are immutable, there's no need to make an actual clone. |
static NSData |
dataWithContentsOfFile(File file)
Deprecated. |
static NSData |
dataWithContentsOfFile(String path)
Deprecated. |
static NSData |
dataWithContentsOfMappedFile(File file)
Deprecated. |
static Object |
decodeObject(NSCoder coder)
Creates an NSData from the data in coder . |
void |
encodeWithCoder(NSCoder coder)
Encodes the receiver using coder . |
boolean |
equals(Object object)
Compares the receiving data object to object . |
int |
hashCode()
Provide an appropriate hash code useful for storing the receiver in a hash-based data structure. |
protected byte[] |
immutableBytes()
Privitive method that returns the receiver's data. |
protected NSRange |
immutableRange()
Privitive method that returns the receiver's range. |
boolean |
isEqualToData(NSData otherData)
Compares the receiving data object to otherData . |
int |
length()
Returns the number of bytes contained by the receiver. |
protected NSRange |
rangeNoCopy()
Primitive method used by mutable subclasses of NSData. |
ByteArrayInputStream |
stream()
Returns a stream containing the receiver's data |
NSData |
subdataWithRange(NSRange range)
Creates a data object containing a copy of the receiver's bytes that fall within the range specified by range . |
String |
toString()
Returns a string representation of the receiver. |
boolean |
writeToFile(String path)
Deprecated. |
void |
writeToStream(OutputStream stream)
Writes the bytes in the receiver contents to the stream . |
boolean |
writeToURL(URL url,
boolean atomically)
Deprecated. |
Methods inherited from class java.lang.Object |
finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final NSData EmptyData
Constructor Detail |
public NSData()
public NSData(byte[] bytes)
bytes
.
bytes
- input byte arraypublic NSData(byte[] bytes, NSRange range)
bytes
that fall in the range specified by range
.
bytes
- input byte arrayrange
- subrange of data within arraypublic NSData(byte[] bytes, int offset, int count)
bytes
that fall in the range specified by offset
and count
.
bytes
- input byte arrayoffset
- offset of start of data within byte arraycount
- length of data starting at offsetpublic NSData(byte[] bytes, NSRange range, boolean noCopy)
bytes
that fall in the range specified by range
. The noCopy
parameter specifies whether or not a copy of bytes
is made.
bytes
- input byte arrayrange
- range of data in byte arraynoCopy
- specifies whether or not a copy of bytes is madepublic NSData(NSData otherData)
otherData
.
otherData
- data object to be copiedpublic NSData(InputStream inputStream, int chunkSize) throws IOException
inputStream
.
The chunkSize
parameter specifies the size, in bytes, of the
block that the input stream returns when it reads. For maximum performance,
you should set the chunk size to the approximate size of the data.
This constructor reads the stream until it detects an end of file
or encounters an exception, but it does not close the stream.
inputStream
- data object with the data from the stream specified
by inputStream
chunkSize
- specifies the size, in bytes, of the block
that the input stream returns
IOException
public NSData(File file) throws IOException
IOException
IllegalArgumentException
NSData(InputStream inputStream, int chunkSize)
public NSData(URL url) throws IOException
url
- input url
IOException
public NSData(String value)
value
- input value as StringNSData(InputStream inputStream, int chunkSize)
public NSData(String value, String encoding)
value
- The textual data to useencoding
- The encoding type used to create the bytes.Method Detail |
public byte[] bytes()
public byte[] bytes(int offset, int length)
offset
- input range specified by offsetlength
- number of bytes following offset
to return
offset
and length
public byte[] bytes(NSRange range)
range
- input range specified by range
range
protected byte[] bytesNoCopy()
rangeNoCopy()
public byte[] bytesNoCopy(NSMutableRange range)
range
.
range
- input mutable range specified by range
range
's offset and length to those of the
receiver's internal NSRange objectpublic Class classForCoder()
classForCoder
in interface NSCoding
NSCoding.classForCoder()
public Object clone()
public static NSData dataWithContentsOfFile(File file) throws IOException
myData = new NSData(new FileInputStream(file), chunkSize);
file
- input java.io.File object
file
IOException
public static NSData dataWithContentsOfFile(String path) throws IOException
myData = new NSData(new FileInputStream(path), chunkSize);
path
- input path of the file
path
IOException
public static NSData dataWithContentsOfMappedFile(File file) throws IOException
myData = new NSData(new FileInputStream(file), chunkSize);
file
- input java.io.File object
file
IOException
public static Object decodeObject(NSCoder coder)
coder
.
coder
-
NSKeyValueCoding.Null
stored in the
NSKeyValueCoding constant NullValue
NSCoding
public void encodeWithCoder(NSCoder coder)
NSCoding
coder
. Object type information along with an
object's data is stored.
encodeWithCoder
in interface NSCoding
coder
- NSCoder
public boolean equals(Object object)
object
.
If object
is an NSData and the contents of object
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.
object
- input data object
public int hashCode()
protected byte[] immutableBytes()
protected NSRange immutableRange()
public boolean isEqualToData(NSData otherData)
otherData
.
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.
otherData
- input data object of type NSData
true
if the contents of otherData
are equal to the contents of the receiver, false
public int length()
protected NSRange rangeNoCopy()
bytesNoCopy
)bytesNoCopy()
,
bytesNoCopy(NSMutableRange dataRange)
public ByteArrayInputStream stream()
java.io.ByteArrayInputStream
containing the
receiver's datapublic NSData subdataWithRange(NSRange range)
range
.
range
- input range
range
RangeException
- if range isn't within the receiver's range of bytespublic 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 }
path
- input path of the file
public void writeToStream(OutputStream stream) throws IOException
stream
.
stream
- the input bytes in the receiver
java.io.IOException
- if the write fails for any reason.
IOException
throws java.io.IOException
public boolean writeToURL(URL url, boolean atomically)
try { FileOutputStream fileOutputStream = new FileOutputStream(url.getFile()); myData.writeToStream(fileOutputStream); fileOutputStream.close(); } catch (IOException exception) { // Do something with the exception }
url
- input urlatomically
- this parameter is ignored
writeToStream(java.io.OutputStream outputStream)
|
Last updated Thu Oct 21 15:04:16 PDT 2004. | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |