Java Reference
In-Depth Information
The
Datagram
class is an abstract class that encapsulates the following concepts
common to all datagram protocols:
• Datagrams have a
buffer
in which the datagram stores the data in a single
datagram packet.
• Datagrams have an
offset
, which is the current position for reading or writing data
in the buffer.
• A datagram's buffer has a
length
, indicating the size of the datagram buffer.
• Datagrams have a representation of the source or destination address of the
datagram.
Datagrams implement the
DataInput
and
DataOutput
interfaces, as you saw in
Figure 12-1, letting you read and write various primitive data types, including
boolean
,
byte
,
character
,
double
,
float
,
integer
,
long integer
, and UTF
String
s. The interface to
DataInput
and
DataOutput
is reminiscent of that provided by
DataInputStream
and
DataOutputStream
; the classes provide methods
read
X
and
write
X
for the various sup-
ported types
X
, including the following:
•
readBoolean
, which lets you read an input byte and returns
true
if the byte is
nonzero, and
writeBoolean
, which lets you write a
boolean
as a single byte
•
readByte
and
writeByte
, which let you read and write single bytes, respectively
•
readChar
and
writeChar
, which let you read and write single characters, respectively
•
readFloat
and
writeFloat
, which let you read and write floating-point numbers,
respectively
•
readShort
and
writeShort
, which let you read and write short integers, respectively
•
readInt
and
writeInt
, which let you read and write integers, respectively
•
readLong
and
writeLong
, which let you read and write long integers, respectively
•
readUnsignedByte
and
writeUnsignedByte
, which let you read and write a single
unsigned byte, respectively
•
readUTF
and
writeUTF
, which let you read and write UTF-8 encoded strings,
respectively
•
readFully
and
write
, which let you read and write arrays of bytes, respectively