edu.rit.io
Class DataOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.FilterOutputStream
-
- edu.rit.io.DataOutputStream
-
- All Implemented Interfaces:
- java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable
public class DataOutputStream extends java.io.FilterOutputStreamClass 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.
-
-
Constructor Summary
Constructors Constructor and Description DataOutputStream(java.io.OutputStream out)Construct a new data output stream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidwriteBoolean(boolean v)Write the given Boolean value to this data output stream.voidwriteDouble(double v)Write the given double value to this data output stream.voidwriteFloat(float v)Write the given float value to this data output stream.voidwriteInt(int v)Write the given integer value to this data output stream.voidwriteLong(long v)Write the given long value to this data output stream.voidwriteString(java.lang.String v)Write the given string value to this data output stream.voidwriteUnsignedInt(int v)Write the given unsigned integer value to this data output stream.voidwriteUnsignedLong(long v)Write the given unsigned long value to this data output stream.
-
-
-
Constructor Detail
-
DataOutputStream
public DataOutputStream(java.io.OutputStream out)
Construct a new data output stream.- Parameters:
out- Underlying output stream.
-
-
Method Detail
-
writeBoolean
public void writeBoolean(boolean v) throws java.io.IOExceptionWrite the given Boolean value to this data output stream. One byte is written, either 0 (if v is false) or 1 (if v is true).- Parameters:
v- Boolean value.- Throws:
java.io.IOException- Thrown if an I/O error occurred.
-
writeInt
public void writeInt(int v) throws java.io.IOExceptionWrite 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 by v (7 bits).
- Else if −8192 ≤ v ≤ 8191, then two bytes are written, containing 10 (2 bits) followed by v (14 bits).
- Else if −1048576 ≤ v ≤ 1048575, then three bytes are written, containing 110 (3 bits) followed by v (21 bits).
- Else if −134217728 ≤ v ≤ 134217727, then four bytes are written, containing 1110 (4 bits) followed by v (28 bits).
- Else five bytes are written, containing 1111 (4 bits) followed by v (sign-extended to 36 bits).
- Parameters:
v- Integer value.- Throws:
java.io.IOException- Thrown if an I/O error occurred.
-
writeUnsignedInt
public void writeUnsignedInt(int v) throws java.io.IOExceptionWrite 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 by v (7 bits).
- Else if 128 ≤ v ≤ 16383, then two bytes are written, containing 10 (2 bits) followed by v (14 bits).
- Else if 16384 ≤ v ≤ 2097151, then three bytes are written, containing 110 (3 bits) followed by v (21 bits).
- Else if 2097152 ≤ v ≤ 268435455, then four bytes are written, containing 1110 (4 bits) followed by v (28 bits).
- Else five bytes are written, containing 1111 (4 bits) followed by v (zero-extended to 36 bits).
- Parameters:
v- Integer value.- Throws:
java.io.IOException- Thrown if an I/O error occurred.
-
writeLong
public void writeLong(long v) throws java.io.IOExceptionWrite 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 by v (7 bits).
- Else if −8192 ≤ v ≤ 8191, then two bytes are written, containing 10 (2 bits) followed by v (14 bits).
- Else if −1048576 ≤ v ≤ 1048575, then three bytes are written, containing 110 (3 bits) followed by v (21 bits).
- Else if −134217728 ≤ v ≤ 134217727, then four bytes are written, containing 1110 (4 bits) followed by v (28 bits).
- Else if −17179869184 ≤ v ≤ 17179869183, then five bytes are written, containing 11110 (5 bits) followed by v (35 bits).
- Else if −2199023255552 ≤ v ≤ 2199023255551, then six bytes are written, containing 111110 (6 bits) followed by v (42 bits).
- Else if −281474976710656 ≤ v ≤ 281474976710655, then seven bytes are written, containing 1111110 (7 bits) followed by v (49 bits).
- Else if −36028797018963968 ≤ v ≤ 36028797018963967, then eight bytes are written, containing 11111110 (8 bits) followed by v (56 bits).
- Else nine bytes are written, containing 11111111 (8 bits) followed by v (64 bits).
- Parameters:
v- Integer value.- Throws:
java.io.IOException- Thrown if an I/O error occurred.
-
writeUnsignedLong
public void writeUnsignedLong(long v) throws java.io.IOExceptionWrite 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 by v (7 bits).
- Else if 128 ≤ v ≤ 16383, then two bytes are written, containing 10 (2 bits) followed by v (14 bits).
- Else if 16384 ≤ v ≤ 2097151, then three bytes are written, containing 110 (3 bits) followed by v (21 bits).
- Else if 2097152 ≤ v ≤ 268435455, then four bytes are written, containing 1110 (4 bits) followed by v (28 bits).
- Else if 268435456 ≤ v ≤ 34359738367, then five bytes are written, containing 11110 (5 bits) followed by v (35 bits).
- Else if 34359738368 ≤ v ≤ 4398046511103, then six bytes are written, containing 111110 (6 bits) followed by v (42 bits).
- Else if 4398046511104 ≤ v ≤ 562949953421311, then seven bytes are written, containing 1111110 (7 bits) followed by v (49 bits).
- Else if 562949953421312 ≤ v ≤ 72057594037927935, then eight bytes are written, containing 11111110 (8 bits) followed by v (56 bits).
- Else nine bytes are written, containing 11111111 (8 bits) followed by v (64 bits).
- Parameters:
v- Integer value.- Throws:
java.io.IOException- Thrown if an I/O error occurred.
-
writeFloat
public void writeFloat(float v) throws java.io.IOExceptionWrite the given float value to this data output stream. Four bytes are written in big-endian order containing Float.floatToRawIntBits(v).- Parameters:
v- Float value.- Throws:
java.io.IOException- Thrown if an I/O error occurred.
-
writeDouble
public void writeDouble(double v) throws java.io.IOExceptionWrite the given double value to this data output stream. Eight bytes are written in big-endian order containing Double.doubleToRawLongBits(v).- Parameters:
v- Double value.- Throws:
java.io.IOException- Thrown if an I/O error occurred.
-
writeString
public void writeString(java.lang.String v) throws java.io.IOExceptionWrite the given string value to this data output stream. The length of the string is written using writeUnsignedInt(), then each character of the string is written using writeUnsignedInt().- Parameters:
v- String value.- Throws:
java.io.IOException- Thrown if an I/O error occurred.
-
-
DMelt 3.0 © DataMelt by jWork.ORG