|
WebObjects 5.2 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | 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 FormInner 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)
Deprecated. |
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 returnsIOException
- public NSData(File file) throws IOException
IOException
- IllegalArgumentException
- NSData(InputStream inputStream, int chunkSize)
public NSData(URL url) throws IOException
URLConnection connection = url.openConnection(); connection.connect(); NSData myData = new NSData(connection.getInputStream(),chunkSize);
url
- input urlIOException
- 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 returnoffset
and length
public byte[] bytes(NSRange range)
range
- input range specified by rangerange
protected byte[] bytesNoCopy()
rangeNoCopy()
public byte[] bytesNoCopy(NSMutableRange range)
range
.
range
- input mutable range specified by rangerange
's offset and length to those of the
receiver's internal NSRange objectpublic Class classForCoder()
classForCoder
in interface NSCoding
NSCoding.classForCoder()
public Object clone()
clone
in class Object
public static NSData dataWithContentsOfFile(File file) throws IOException
myData = new NSData(new FileInputStream(file), chunkSize);
file
- input java.io.File objectfile
IOException
- public static NSData dataWithContentsOfFile(String path) throws IOException
myData = new NSData(new FileInputStream(path), chunkSize);
path
- input path of the filepath
IOException
- public static NSData dataWithContentsOfMappedFile(File file) throws IOException
myData = new NSData(new FileInputStream(file), chunkSize);
file
- input java.io.File objectfile
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
- 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.equals
in class Object
object
- input data objectpublic int hashCode()
hashCode
in class Object
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 NSDatatrue
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 rangerange
RangeException
- if range isn't within the receiver's range of bytespublic String toString()
toString
in class Object
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 filepublic void writeToStream(OutputStream stream) throws IOException
stream
.stream
- the input bytes in the receiverjava.io.IOException
- if the write fails for any reason.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 ignoredwriteToStream(java.io.OutputStream outputStream)
|
Last updated Fri Feb 21 13:15:00 PST 2003. | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |