java.io
Class DataOutputStream
java.lang.Object
|
+--java.io.OutputStream
|
+--java.io.FilterOutputStream
|
+--java.io.DataOutputStream
All Implemented Interfaces:
DataOutput
This class provides a mechanism for writing primitive Java datatypes
to an OutputStream in a portable way. Data written to
a stream using this class can be read back in using the
DataInputStream class on any platform.
Author:- Aaron M. Renn (arenn@urbanophile.com)
See Also:
written
protected int written This is the total number of bytes that have been written to the
stream by this object instance.
DataOutputStream
public DataOutputStream(java.io.OutputStream out) This method initializes an instance of DataOutputStream to
write its data to the specified underlying OutputStream
Parameters:
flush
public void flush() This method flushes any unwritten bytes to the underlying stream.
Throws:
size
public final int size() This method returns the total number of bytes that have been written to
the underlying output stream so far. This is the value of the
written instance variable
Returns:
- The number of bytes written to the stream.
write
public void write(byte[] buf, int offset, int len) This method writes len bytes from the specified byte array
buf starting at position offset into the
buffer to the underlying output stream.
Parameters:
Throws:
write
public void write(int b) This method writes the specified byte (passed as an int)
to the underlying output stream.
Parameters:
Throws:
writeBoolean
public final void writeBoolean(boolean b) This method writes a Java boolean to the underlying output
stream. For a value of true, 1 is written to the stream.
For a value of false, 0 is written.
Parameters:
Throws:
writeByte
public final void writeByte(int b) This method writes a Java byte value to the underlying
output stream.
Parameters:
Throws:
writeBytes
public final void writeBytes(java.lang.String s) This method writes all the bytes in a String out to the
stream. One byte is written for each character in the String.
The high eight bits of each character are discarded.
Parameters:
Throws:
writeChar
public final void writeChar(int c) This method writes a single char value to the stream,
high byte first.
Parameters:
Throws:
writeChars
public final void writeChars(java.lang.String s) This method writes all the characters in a String to the
stream. There will be two bytes for each character value. The high
byte of the character will be written first.
Parameters:
Throws:
writeDouble
public final void writeDouble(double d) This method writes a Java double value to the stream. This
value is written by first calling the method Double.doubleToLongBits
to retrieve an long representing the floating point number,
then writing this long value to the stream exactly the same
as the writeLong() method does.
Parameters:
Throws:
See Also:
writeFloat
public final void writeFloat(float f) This method writes a Java float value to the stream. This
value is written by first calling the method Float.floatToIntBits
to retrieve an int representing the floating point number,
then writing this int value to the stream exactly the same
as the writeInt() method does.
Parameters:
Throws:
See Also:
writeInt
public final void writeInt(int i) This method writes a Java int to the stream, high bytes
first. This method requires four bytes to encode the value.
Parameters:
Throws:
writeLong
public final void writeLong(long l) This method writes a Java long to the stream, high bytes
first. This method requires eight bytes to encode the value.
Parameters:
Throws:
writeShort
public final void writeShort(int s) This method writes a Java short to the stream, high byte
first. This method requires two bytes to encode the value.
Parameters:
Throws:
writeUTF
public final synchronized void writeUTF(java.lang.String s) This method writes a Java String to the stream in a modified
UTF-8 format. First, two bytes are written to the stream indicating the
number of bytes to follow. Note that this is the number of bytes in the
encoded String not the String length. Next
come the encoded characters. Each character in the String
is encoded as either one, two or three bytes. For characters in the
range of \u0001 to <\u007F>, one byte is used. The character
value goes into bits 0-7 and bit eight is 0. For characters in the range
of \u0080 to \u007FF, two bytes are used. Bits
6-10 of the character value are encoded bits 0-4 of the first byte, with
the high bytes having a value of "110". Bits 0-5 of the character value
are stored in bits 0-5 of the second byte, with the high bits set to
"10". This type of encoding is also done for the null character
\u0000. This eliminates any C style NUL character values
in the output. All remaining characters are stored as three bytes.
Bits 12-15 of the character value are stored in bits 0-3 of the first
byte. The high bits of the first bytes are set to "1110". Bits 6-11
of the character value are stored in bits 0-5 of the second byte. The
high bits of the second byte are set to "10". And bits 0-5 of the
character value are stored in bits 0-5 of byte three, with the high bits
of that byte set to "10".
Parameters:
Throws:
OutputStreamin a portable way. Data written to a stream using this class can be read back in using theDataInputStreamclass on any platform.