Package java.io

Class DataOutputStream

All Implemented Interfaces:
Closeable, DataOutput, Flushable, AutoCloseable

public class DataOutputStream
extends FilterOutputStream
implements DataOutput
Wraps an existing OutputStream and writes big-endian typed data to it. Typically, this stream can be read in by DataInputStream. Types that can be written include byte, 16-bit short, 32-bit int, 32-bit float, 64-bit long, 64-bit double, byte strings, and MUTF-8 encoded strings.
See Also:
DataInputStream
  • Field Summary

    Fields
    Modifier and Type Field Description
    protected int written
    The number of bytes written out so far.

    Fields inherited from class java.io.FilterOutputStream

    out
  • Constructor Summary

    Constructors
    Constructor Description
    DataOutputStream​(OutputStream out)
    Constructs a new DataOutputStream on the OutputStream out.
  • Method Summary

    Modifier and Type Method Description
    void flush()
    Flushes this stream to ensure all pending data is sent out to the target stream.
    int size()
    Returns the total number of bytes written to the target stream so far.
    void write​(byte[] buffer, int offset, int count)
    Writes count bytes from the byte array buffer starting at offset to the target stream.
    void write​(int oneByte)
    Writes a byte to the target stream.
    void writeBoolean​(boolean val)
    Writes a boolean to the target stream.
    void writeByte​(int val)
    Writes an 8-bit byte to the target stream.
    void writeBytes​(String str)
    Writes the low order 8-bit bytes from the specified string.
    void writeChar​(int val)
    Writes the specified 16-bit character in big-endian order.
    void writeChars​(String str)
    Writes the 16-bit characters contained in str in big-endian order.
    void writeDouble​(double val)
    Writes the specified 64-bit double in big-endian order.
    void writeFloat​(float val)
    Writes the specified 32-bit float in big-endian order.
    void writeInt​(int val)
    Writes the specified 32-bit int in big-endian order.
    void writeLong​(long val)
    Writes the specified 64-bit long in big-endian order.
    void writeShort​(int val)
    Writes the specified 16-bit short in big-endian order.
    void writeUTF​(String str)
    Writes the specified string encoded in modified UTF-8.

    Methods inherited from class java.io.FilterOutputStream

    close

    Methods inherited from class java.io.OutputStream

    write

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface java.io.DataOutput

    write
  • Field Details

    • written

      protected int written
      The number of bytes written out so far.
  • Constructor Details

    • DataOutputStream

      public DataOutputStream​(OutputStream out)
      Constructs a new DataOutputStream on the OutputStream out. Note that data written by this stream is not in a human readable form but can be reconstructed by using a DataInputStream on the resulting output.
      Parameters:
      out - the target stream for writing.
  • Method Details

    • flush

      public void flush() throws IOException
      Flushes this stream to ensure all pending data is sent out to the target stream. This implementation then also flushes the target stream.
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class FilterOutputStream
      Throws:
      IOException - if an error occurs attempting to flush this stream.
    • size

      public final int size()
      Returns the total number of bytes written to the target stream so far.
      Returns:
      the number of bytes written to the target stream.
    • write

      public void write​(byte[] buffer, int offset, int count) throws IOException
      Writes count bytes from the byte array buffer starting at offset to the target stream.
      Specified by:
      write in interface DataOutput
      Overrides:
      write in class FilterOutputStream
      Parameters:
      buffer - the buffer to write to the target stream.
      offset - the index of the first byte in buffer to write.
      count - the number of bytes from the buffer to write.
      Throws:
      IOException - if an error occurs while writing to the target stream.
      NullPointerException - if buffer is null.
    • write

      public void write​(int oneByte) throws IOException
      Writes a byte to the target stream. Only the least significant byte of the integer oneByte is written.
      Specified by:
      write in interface DataOutput
      Overrides:
      write in class FilterOutputStream
      Parameters:
      oneByte - the byte to write to the target stream.
      Throws:
      IOException - if an error occurs while writing to the target stream.
      See Also:
      DataInputStream.readByte()
    • writeBoolean

      public final void writeBoolean​(boolean val) throws IOException
      Writes a boolean to the target stream.
      Specified by:
      writeBoolean in interface DataOutput
      Parameters:
      val - the boolean value to write to the target stream.
      Throws:
      IOException - if an error occurs while writing to the target stream.
      See Also:
      DataInputStream.readBoolean()
    • writeByte

      public final void writeByte​(int val) throws IOException
      Writes an 8-bit byte to the target stream. Only the least significant byte of the integer val is written.
      Specified by:
      writeByte in interface DataOutput
      Parameters:
      val - the byte value to write to the target stream.
      Throws:
      IOException - if an error occurs while writing to the target stream.
      See Also:
      DataInputStream.readByte(), DataInputStream.readUnsignedByte()
    • writeBytes

      public final void writeBytes​(String str) throws IOException
      Description copied from interface: DataOutput
      Writes the low order 8-bit bytes from the specified string.
      Specified by:
      writeBytes in interface DataOutput
      Parameters:
      str - the string containing the bytes to write.
      Throws:
      IOException - if an I/O error occurs while writing.
    • writeChar

      public final void writeChar​(int val) throws IOException
      Description copied from interface: DataOutput
      Writes the specified 16-bit character in big-endian order. Only the two least significant bytes of the integer oneByte are written.
      Specified by:
      writeChar in interface DataOutput
      Parameters:
      val - the character to write.
      Throws:
      IOException - if an I/O error occurs while writing.
      See Also:
      DataInput.readChar()
    • writeChars

      public final void writeChars​(String str) throws IOException
      Description copied from interface: DataOutput
      Writes the 16-bit characters contained in str in big-endian order.
      Specified by:
      writeChars in interface DataOutput
      Parameters:
      str - the string that contains the characters to write.
      Throws:
      IOException - if an I/O error occurs while writing.
      See Also:
      DataInput.readChar()
    • writeDouble

      public final void writeDouble​(double val) throws IOException
      Description copied from interface: DataOutput
      Writes the specified 64-bit double in big-endian order. The resulting output is the eight bytes returned by Double.doubleToLongBits(double).
      Specified by:
      writeDouble in interface DataOutput
      Parameters:
      val - the double to write.
      Throws:
      IOException - if an I/O error occurs while writing.
      See Also:
      DataInput.readDouble()
    • writeFloat

      public final void writeFloat​(float val) throws IOException
      Description copied from interface: DataOutput
      Writes the specified 32-bit float in big-endian order. The resulting output is the four bytes returned by Float.floatToIntBits(float).
      Specified by:
      writeFloat in interface DataOutput
      Parameters:
      val - the float to write.
      Throws:
      IOException - if an I/O error occurs while writing.
      See Also:
      DataInput.readFloat()
    • writeInt

      public final void writeInt​(int val) throws IOException
      Description copied from interface: DataOutput
      Writes the specified 32-bit int in big-endian order.
      Specified by:
      writeInt in interface DataOutput
      Parameters:
      val - the int to write.
      Throws:
      IOException - if an I/O error occurs while writing.
      See Also:
      DataInput.readInt()
    • writeLong

      public final void writeLong​(long val) throws IOException
      Description copied from interface: DataOutput
      Writes the specified 64-bit long in big-endian order.
      Specified by:
      writeLong in interface DataOutput
      Parameters:
      val - the long to write.
      Throws:
      IOException - if an I/O error occurs while writing.
      See Also:
      DataInput.readLong()
    • writeShort

      public final void writeShort​(int val) throws IOException
      Description copied from interface: DataOutput
      Writes the specified 16-bit short in big-endian order. Only the lower two bytes of val are written.
      Specified by:
      writeShort in interface DataOutput
      Parameters:
      val - the short to write.
      Throws:
      IOException - if an I/O error occurs while writing.
      See Also:
      DataInput.readShort(), DataInput.readUnsignedShort()
    • writeUTF

      public final void writeUTF​(String str) throws IOException
      Description copied from interface: DataOutput
      Writes the specified string encoded in modified UTF-8.
      Specified by:
      writeUTF in interface DataOutput
      Parameters:
      str - the string to write encoded in modified UTF-8.
      Throws:
      IOException - if an I/O error occurs while writing.
      See Also:
      DataInput.readUTF()