Java Reference
In-Depth Information
data type. The controlled-load network element service uses 32-bit IEEE 754 floating-
point numbers, just like Java's float data type. (This is correlation rather than causation.
Both Java and most network protocols were designed by Unix programmers, and con‐
sequently both tend to use the formats common to most Unix systems.) However, this
isn't true for all network protocols, so check the details of any protocol you use. For
instance, the Network Time Protocol (NTP) represents times as 64-bit unsigned fixed-
point numbers with the integer part in the first 32 bits and the fraction part in the last
32 bits. This doesn't match any primitive data type in any common programming lan‐
guage, although it is straightforward to work with—at least as far as is necessary for
NTP.
The DataOutputStream class offers these 11 methods for writing particular Java data
types:
public final void writeBoolean ( boolean b ) throws IOException
public final void writeByte ( int b ) throws IOException
public final void writeShort ( int s ) throws IOException
public final void writeChar ( int c ) throws IOException
public final void writeInt ( int i ) throws IOException
public final void writeLong ( long l ) throws IOException
public final void writeFloat ( float f ) throws IOException
public final void writeDouble ( double d ) throws IOException
public final void writeChars ( String s ) throws IOException
public final void writeBytes ( String s ) throws IOException
public final void writeUTF ( String s ) throws IOException
All data is written in big-endian format. Integers are written in two's complement in the
minimum number of bytes possible. Thus, a byte is written as one byte, a short as two
bytes, an int as four bytes, and a long as eight bytes. Floats and doubles are written in
IEEE 754 form in four and eight bytes, respectively. Booleans are written as a single byte
with the value 0 for false and 1 for true. Chars are written as two unsigned bytes.
The last three methods are a little trickier. The writeChars() method simply iterates
through the String argument, writing each character in turn as a two-byte, big-endian
Unicode character (a UTF-16 code point, to be absolutely precise). The write
Bytes() method iterates through the String argument but writes only the least signif‐
icant byte of each character. Thus, information will be lost for any string with characters
from outside the Latin-1 character set. This method may be useful on some network
protocols that specify the ASCII encoding, but it should be avoided most of the time.
Neither writeChars() nor writeBytes() encodes the length of the string in the output
stream. As a result, you can't really distinguish between raw characters and characters
that make up part of a string. The writeUTF() method does include the length of the
string. It encodes the string itself in a variant of the UTF-8 encoding of Unicode. Because
this variant is subtly incompatible with most non-Java software, it should be used only
for exchanging data with other Java programs that use a DataInputStream to read
Search WWH ::




Custom Search