Class BinaryOutputStream

All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
Direct Known Subclasses:
DicomOutputStream

public class BinaryOutputStream extends FilterOutputStream

A class that extends FilterOutputStream by adding the concept of little and big endian binary value encoding, and supplies functions for writing various sized integer and floating point words.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected long
     

    Fields inherited from class java.io.FilterOutputStream

    out
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Get the byte offset where the next byte will be written.
    boolean
    Is the stream byte order big endian ?
    boolean
    Is the stream byte order little endian ?
    protected void
    localInit(boolean big)
     
    static void
    main(String[] arg)
    For testing.
    void
    Set the stream byte order to big endian.
    void
    setEndian(boolean big)
    Set the stream byte order to that specified.
    void
    Set the stream byte order to little endian.
    void
    write(byte[] b)
    Writes b.length bytes to this output stream.
    void
    write(byte[] b, int off, int len)
    Writes len bytes from the specified byte array starting at offset off to this output stream.
    void
    write(int b)
    Writes the specified byte to this output stream.
    final void
    writeDouble(double value)
    Write one floating point 64 bit value.
    final void
    writeDouble(double[] f, int len)
    Write an array of floating point 64 bit values.
    final void
    writeFloat(float value)
    Write one floating point 32 bit value.
    final void
    writeFloat(float[] f, int len)
    Write an array of floating point 32 bit values.
    final void
    Write one signed integer 16 bit value.
    final void
    writeSigned32(long v)
    Write one signed integer 32 bit value.
    final void
    writeSigned64(long v)
    Write one signed integer 64 bit value.
    final void
    Write one unsigned integer 16 bit value.
    final void
    writeUnsigned16(short[] w, int len)
    Write an array of unsigned integer 16 bit values.
    final void
    writeUnsigned32(int[] w, int len)
    Write an array of unsigned integer 32 bit values.
    final void
    Write one unsigned integer 32 bit value.
    final void
    Write one unsigned integer 64 bit value.
    final void
    writeUnsigned64(long[] w, int len)
    Write an array of unsigned integer 64 bit values.
    final void
    Write one unsigned integer 8 bit value.

    Methods inherited from class java.io.FilterOutputStream

    close, flush

    Methods inherited from class java.io.OutputStream

    nullOutputStream

    Methods inherited from class java.lang.Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • byteOffset

      protected long byteOffset
  • Constructor Details

    • BinaryOutputStream

      public BinaryOutputStream(OutputStream o, boolean big)
      Parameters:
      o - the output stream
      big - true if big endian, false if little endian
  • Method Details

    • getByteOffset

      public long getByteOffset()

      Get the byte offset where the next byte will be written.

      Will be zero at the start of the file.

      Returns:
      the byte offset
    • localInit

      protected void localInit(boolean big)
      Parameters:
      big - true if big endian, false if little endian
    • write

      public void write(byte[] b, int off, int len) throws IOException
      Writes len bytes from the specified byte array starting at offset off to this output stream.

      The write method of BinaryOutputStream calls the corresponding multi-byte write method of one argument of the. OutputStream supplied in the constructor, and overrides the default use of multiple calls to the single-byte write method that is the behavior of FilterOutputStream.

      The exceptions thrown are whatever is thrown by the OutputStream write method.

      Overrides:
      write in class FilterOutputStream
      Parameters:
      b - the data.
      off - the start offset in the data.
      len - the number of bytes to write.
      Throws:
      IOException - if an I/O error occurs.
    • write

      public void write(int b) throws IOException
      Writes the specified byte to this output stream.
      Overrides:
      write in class FilterOutputStream
      Parameters:
      b - the byte.
      Throws:
      IOException - if an I/O error occurs.
    • write

      public void write(byte[] b) throws IOException
      Writes b.length bytes to this output stream.
      Overrides:
      write in class FilterOutputStream
      Parameters:
      b - the data to be written.
      Throws:
      IOException - if an I/O error occurs.
    • isBigEndian

      public boolean isBigEndian()

      Is the stream byte order big endian ?

      Returns:
      true if big endian, false if little endian
    • isLittleEndian

      public boolean isLittleEndian()

      Is the stream byte order little endian ?

      Returns:
      true if little endian, false if big endian
    • setBigEndian

      public void setBigEndian()

      Set the stream byte order to big endian.

    • setLittleEndian

      public void setLittleEndian()

      Set the stream byte order to little endian.

    • setEndian

      public void setEndian(boolean big)

      Set the stream byte order to that specified.

      Parameters:
      big - true if to set to big endian, false if little endian
    • writeUnsigned8

      public final void writeUnsigned8(int v) throws IOException

      Write one unsigned integer 8 bit value.

      Parameters:
      v - an int containing an unsigned value
      Throws:
      IOException - if an I/O error occurs
    • writeUnsigned16

      public final void writeUnsigned16(int v) throws IOException

      Write one unsigned integer 16 bit value.

      Parameters:
      v - an int containing an unsigned value
      Throws:
      IOException - if an I/O error occurs
    • writeSigned16

      public final void writeSigned16(int v) throws IOException

      Write one signed integer 16 bit value.

      Parameters:
      v - an int containing an signed value
      Throws:
      IOException - if an I/O error occurs
    • writeUnsigned32

      public final void writeUnsigned32(long v) throws IOException

      Write one unsigned integer 32 bit value.

      Parameters:
      v - a long containing an unsigned value
      Throws:
      IOException - if an I/O error occurs
    • writeSigned32

      public final void writeSigned32(long v) throws IOException

      Write one signed integer 32 bit value.

      Parameters:
      v - a long containing an signed value
      Throws:
      IOException - if an I/O error occurs
    • writeUnsigned64

      public final void writeUnsigned64(long v) throws IOException

      Write one unsigned integer 64 bit value.

      Parameters:
      v - a long containing an unsigned value
      Throws:
      IOException - if an I/O error occurs
    • writeSigned64

      public final void writeSigned64(long v) throws IOException

      Write one signed integer 64 bit value.

      Parameters:
      v - a long containing a signed value
      Throws:
      IOException - if an I/O error occurs
    • writeFloat

      public final void writeFloat(float value) throws IOException

      Write one floating point 32 bit value.

      Parameters:
      value - a float value
      Throws:
      IOException - if an I/O error occurs
    • writeDouble

      public final void writeDouble(double value) throws IOException

      Write one floating point 64 bit value.

      Parameters:
      value - a double value
      Throws:
      IOException - if an I/O error occurs
    • writeUnsigned16

      public final void writeUnsigned16(short[] w, int len) throws IOException

      Write an array of unsigned integer 16 bit values.

      Parameters:
      w - an array of integers to write
      len - the number of 16 bit values to write
      Throws:
      IOException - if an I/O error occurs
    • writeUnsigned32

      public final void writeUnsigned32(int[] w, int len) throws IOException

      Write an array of unsigned integer 32 bit values.

      Parameters:
      w - an array of integers to write
      len - the number of 32 bit values to write
      Throws:
      IOException - if an I/O error occurs
    • writeUnsigned64

      public final void writeUnsigned64(long[] w, int len) throws IOException

      Write an array of unsigned integer 64 bit values.

      Parameters:
      w - an array of integers to write
      len - the number of 64 bit values to write
      Throws:
      IOException - if an I/O error occurs
    • writeFloat

      public final void writeFloat(float[] f, int len) throws IOException

      Write an array of floating point 32 bit values.

      Parameters:
      f - an array of floats to write
      len - the number of values to write
      Throws:
      IOException - if an I/O error occurs
    • writeDouble

      public final void writeDouble(double[] f, int len) throws IOException

      Write an array of floating point 64 bit values.

      Parameters:
      f - an array of floats to write
      len - the number of values to write
      Throws:
      IOException - if an I/O error occurs
    • main

      public static void main(String[] arg)

      For testing.

      Parameters:
      arg - file to write to