Package edu.rit.io
Class DataOutputStream
java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
edu.rit.io.DataOutputStream
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
Class DataOutputStream provides an output stream that writes primitive data
types and strings in binary form. It behaves similarly to class
java.io.DataOutputStream, except the methods for writing types byte, short,
char, int, long, and String are implemented differently. These methods write
an integer value using a variable number of bytes, as described below. This
can save space in the file if small integer values are written more
frequently than large integer values. The resulting byte stream can be read
using class DataInputStream.
Note that class DataOutputStream does not implement interface java.io.DataOutput, because the methods do not obey the contract specified in that interface.
- Version:
- 18-Dec-2009
- Author:
- Alan Kaminsky
-
Field Summary
Fields inherited from class java.io.FilterOutputStream
out
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
writeBoolean
(boolean v) Write the given Boolean value to this data output stream.void
writeDouble
(double v) Write the given double value to this data output stream.void
writeFloat
(float v) Write the given float value to this data output stream.void
writeInt
(int v) Write the given integer value to this data output stream.void
writeLong
(long v) Write the given long value to this data output stream.void
Write the given string value to this data output stream.void
writeUnsignedInt
(int v) Write the given unsigned integer value to this data output stream.void
writeUnsignedLong
(long v) Write the given unsigned long value to this data output stream.Methods inherited from class java.io.OutputStream
nullOutputStream
-
Constructor Details
-
DataOutputStream
Construct a new data output stream.- Parameters:
out
- Underlying output stream.
-
-
Method Details
-
writeBoolean
Write the given Boolean value to this data output stream. One byte is written, either 0 (ifv
is false) or 1 (ifv
is true).- Parameters:
v
- Boolean value.- Throws:
IOException
- Thrown if an I/O error occurred.IOException
- if any.
-
writeInt
Write the given integer value to this data output stream. This method can be used to write values of type byte, short, char, or int. From one to five bytes are written, in big-endian order, as follows:-
If −64 ≤
v
≤ 63, then one byte is written, containing 0 (1 bit) followed byv
(7 bits). -
Else if −8192 ≤
v
≤ 8191, then two bytes are written, containing 10 (2 bits) followed byv
(14 bits). -
Else if −1048576 ≤
v
≤ 1048575, then three bytes are written, containing 110 (3 bits) followed byv
(21 bits). -
Else if −134217728 ≤
v
≤ 134217727, then four bytes are written, containing 1110 (4 bits) followed byv
(28 bits). -
Else five bytes are written, containing 1111 (4 bits) followed by
v
(sign-extended to 36 bits).
- Parameters:
v
- Integer value.- Throws:
IOException
- Thrown if an I/O error occurred.IOException
- if any.
-
If −64 ≤
-
writeUnsignedInt
Write the given unsigned integer value to this data output stream. This method can be used to write values of type byte, short, char, or int. From one to five bytes are written, in big-endian order, as follows:-
If 0 ≤
v
≤ 127, then one byte is written, containing 0 (1 bit) followed byv
(7 bits). -
Else if 128 ≤
v
≤ 16383, then two bytes are written, containing 10 (2 bits) followed byv
(14 bits). -
Else if 16384 ≤
v
≤ 2097151, then three bytes are written, containing 110 (3 bits) followed byv
(21 bits). -
Else if 2097152 ≤
v
≤ 268435455, then four bytes are written, containing 1110 (4 bits) followed byv
(28 bits). -
Else five bytes are written, containing 1111 (4 bits) followed by
v
(zero-extended to 36 bits).
- Parameters:
v
- Integer value.- Throws:
IOException
- Thrown if an I/O error occurred.IOException
- if any.
-
If 0 ≤
-
writeLong
Write the given long value to this data output stream. From one to nine bytes are written, in big-endian order, as follows:-
If −64 ≤
v
≤ 63, then one byte is written, containing 0 (1 bit) followed byv
(7 bits). -
Else if −8192 ≤
v
≤ 8191, then two bytes are written, containing 10 (2 bits) followed byv
(14 bits). -
Else if −1048576 ≤
v
≤ 1048575, then three bytes are written, containing 110 (3 bits) followed byv
(21 bits). -
Else if −134217728 ≤
v
≤ 134217727, then four bytes are written, containing 1110 (4 bits) followed byv
(28 bits). -
Else if −17179869184 ≤
v
≤ 17179869183, then five bytes are written, containing 11110 (5 bits) followed byv
(35 bits). -
Else if −2199023255552 ≤
v
≤ 2199023255551, then six bytes are written, containing 111110 (6 bits) followed byv
(42 bits). -
Else if −281474976710656 ≤
v
≤ 281474976710655, then seven bytes are written, containing 1111110 (7 bits) followed byv
(49 bits). -
Else if −36028797018963968 ≤
v
≤ 36028797018963967, then eight bytes are written, containing 11111110 (8 bits) followed byv
(56 bits). -
Else nine bytes are written, containing 11111111 (8 bits) followed by
v
(64 bits).
- Parameters:
v
- Integer value.- Throws:
IOException
- Thrown if an I/O error occurred.IOException
- if any.
-
If −64 ≤
-
writeUnsignedLong
Write the given unsigned long value to this data output stream. From one to nine bytes are written, in big-endian order, as follows:-
If 0 ≤
v
≤ 127, then one byte is written, containing 0 (1 bit) followed byv
(7 bits). -
Else if 128 ≤
v
≤ 16383, then two bytes are written, containing 10 (2 bits) followed byv
(14 bits). -
Else if 16384 ≤
v
≤ 2097151, then three bytes are written, containing 110 (3 bits) followed byv
(21 bits). -
Else if 2097152 ≤
v
≤ 268435455, then four bytes are written, containing 1110 (4 bits) followed byv
(28 bits). -
Else if 268435456 ≤
v
≤ 34359738367, then five bytes are written, containing 11110 (5 bits) followed byv
(35 bits). -
Else if 34359738368 ≤
v
≤ 4398046511103, then six bytes are written, containing 111110 (6 bits) followed byv
(42 bits). -
Else if 4398046511104 ≤
v
≤ 562949953421311, then seven bytes are written, containing 1111110 (7 bits) followed byv
(49 bits). -
Else if 562949953421312 ≤
v
≤ 72057594037927935, then eight bytes are written, containing 11111110 (8 bits) followed byv
(56 bits). -
Else nine bytes are written, containing 11111111 (8 bits) followed by
v
(64 bits).
- Parameters:
v
- Integer value.- Throws:
IOException
- Thrown if an I/O error occurred.IOException
- if any.
-
If 0 ≤
-
writeFloat
Write the given float value to this data output stream. Four bytes are written in big-endian order containingFloat.floatToRawIntBits(v)
.- Parameters:
v
- Float value.- Throws:
IOException
- Thrown if an I/O error occurred.IOException
- if any.
-
writeDouble
Write the given double value to this data output stream. Eight bytes are written in big-endian order containingDouble.doubleToRawLongBits(v)
.- Parameters:
v
- Double value.- Throws:
IOException
- Thrown if an I/O error occurred.IOException
- if any.
-
writeString
Write the given string value to this data output stream. The length of the string is written usingwriteUnsignedInt()
, then each character of the string is written usingwriteUnsignedInt()
.- Parameters:
v
- String value.- Throws:
IOException
- Thrown if an I/O error occurred.IOException
- if any.
-