Package edu.rit.io

Class DataOutputStream

All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class DataOutputStream extends FilterOutputStream
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
  • Constructor Details

    • DataOutputStream

      public DataOutputStream(OutputStream out)
      Construct a new data output stream.
      Parameters:
      out - Underlying output stream.
  • Method Details

    • writeBoolean

      public void writeBoolean(boolean v) throws IOException
      Write 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:
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • writeInt

      public void writeInt(int v) throws IOException
      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 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:
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • writeUnsignedInt

      public void writeUnsignedInt(int v) throws IOException
      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 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:
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • writeLong

      public void writeLong(long v) throws IOException
      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 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:
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • writeUnsignedLong

      public void writeUnsignedLong(long v) throws IOException
      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 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:
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • writeFloat

      public void writeFloat(float v) throws IOException
      Write 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:
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • writeDouble

      public void writeDouble(double v) throws IOException
      Write 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:
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • writeString

      public void writeString(String v) throws IOException
      Write 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:
      IOException - Thrown if an I/O error occurred.
      IOException - if any.