Class LittleEndianDataOutputStream

java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
com.twelvemonkeys.io.LittleEndianDataOutputStream
All Implemented Interfaces:
Closeable, DataOutput, Flushable, AutoCloseable

public class LittleEndianDataOutputStream extends FilterOutputStream implements DataOutput
A little endian output stream writes primitive Java numbers and characters to an output stream in a little endian format.

The standard java.io.DataOutputStream class which this class imitates uses big endian integers.

Warning: The DataInput and DataOutput interfaces specifies big endian byte order in their documentation. This means that this class is, strictly speaking, not a proper implementation. However, I don't see a reason for the these interfaces to specify the byte order of their underlying representations.

Version:
1.0.1, 19 May 1999
Author:
Elliotte Rusty Harold
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected int
    The number of bytes written so far to the little endian output stream.

    Fields inherited from class java.io.FilterOutputStream

    out
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new little endian output stream and chains it to the output stream specified by the pStream argument.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns the number of bytes written to this little endian output stream.
    void
    write(byte[] pBytes, int pOffset, int pLength)
    Writes pLength bytes from the specified byte array starting at pOffset to the underlying output stream.
    void
    write(int pByte)
    Writes the specified byte value to the underlying output stream.
    void
    writeBoolean(boolean pBoolean)
    Writes a boolean to the underlying output stream as a single byte.
    void
    writeByte(int pByte)
    Writes out a byte to the underlying output stream
    void
    writeBytes(String pString)
    Writes a string to the underlying output stream as a sequence of bytes.
    void
    writeChar(int pChar)
    Writes a two byte char to the underlying output stream in little endian order, low byte first.
    void
    writeChars(String pString)
    Writes a string to the underlying output stream as a sequence of characters.
    final void
    writeDouble(double d)
    Writes an 8 byte Java double to the underlying output stream in little endian order.
    final void
    writeFloat(float f)
    Writes a 4 byte Java float to the underlying output stream in little endian order.
    void
    writeInt(int pInt)
    Writes a four-byte int to the underlying output stream in little endian order, low byte first, high byte last
    void
    writeLong(long pLong)
    Writes an eight-byte long to the underlying output stream in little endian order, low byte first, high byte last
    void
    writeShort(int pShort)
    Writes a two byte short to the underlying output stream in little endian order, low byte first.
    void
    writeUTF(String pString)
    Writes a string of no more than 65,535 characters to the underlying output stream using UTF-8 encoding.

    Methods inherited from class java.io.FilterOutputStream

    close, flush, write

    Methods inherited from class java.io.OutputStream

    nullOutputStream

    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

    • bytesWritten

      protected int bytesWritten
      The number of bytes written so far to the little endian output stream.
  • Constructor Details

    • LittleEndianDataOutputStream

      public LittleEndianDataOutputStream(OutputStream pStream)
      Creates a new little endian output stream and chains it to the output stream specified by the pStream argument.
      Parameters:
      pStream - the underlying output stream.
      See Also:
  • Method Details

    • write

      public void write(int pByte) throws IOException
      Writes the specified byte value to the underlying output stream.
      Specified by:
      write in interface DataOutput
      Overrides:
      write in class FilterOutputStream
      Parameters:
      pByte - the byte value to be written.
      Throws:
      IOException - if the underlying stream throws an IOException.
    • write

      public void write(byte[] pBytes, int pOffset, int pLength) throws IOException
      Writes pLength bytes from the specified byte array starting at pOffset to the underlying output stream.
      Specified by:
      write in interface DataOutput
      Overrides:
      write in class FilterOutputStream
      Parameters:
      pBytes - the data.
      pOffset - the start offset in the data.
      pLength - the number of bytes to write.
      Throws:
      IOException - if the underlying stream throws an IOException.
    • writeBoolean

      public void writeBoolean(boolean pBoolean) throws IOException
      Writes a boolean to the underlying output stream as a single byte. If the argument is true, the byte value 1 is written. If the argument is false, the byte value 0 in written.
      Specified by:
      writeBoolean in interface DataOutput
      Parameters:
      pBoolean - the boolean value to be written.
      Throws:
      IOException - if the underlying stream throws an IOException.
    • writeByte

      public void writeByte(int pByte) throws IOException
      Writes out a byte to the underlying output stream
      Specified by:
      writeByte in interface DataOutput
      Parameters:
      pByte - the byte value to be written.
      Throws:
      IOException - if the underlying stream throws an IOException.
    • writeShort

      public void writeShort(int pShort) throws IOException
      Writes a two byte short to the underlying output stream in little endian order, low byte first.
      Specified by:
      writeShort in interface DataOutput
      Parameters:
      pShort - the short to be written.
      Throws:
      IOException - if the underlying stream throws an IOException.
    • writeChar

      public void writeChar(int pChar) throws IOException
      Writes a two byte char to the underlying output stream in little endian order, low byte first.
      Specified by:
      writeChar in interface DataOutput
      Parameters:
      pChar - the char value to be written.
      Throws:
      IOException - if the underlying stream throws an IOException.
    • writeInt

      public void writeInt(int pInt) throws IOException
      Writes a four-byte int to the underlying output stream in little endian order, low byte first, high byte last
      Specified by:
      writeInt in interface DataOutput
      Parameters:
      pInt - the int to be written.
      Throws:
      IOException - if the underlying stream throws an IOException.
    • writeLong

      public void writeLong(long pLong) throws IOException
      Writes an eight-byte long to the underlying output stream in little endian order, low byte first, high byte last
      Specified by:
      writeLong in interface DataOutput
      Parameters:
      pLong - the long to be written.
      Throws:
      IOException - if the underlying stream throws an IOException.
    • writeFloat

      public final void writeFloat(float f) throws IOException
      Writes a 4 byte Java float to the underlying output stream in little endian order.
      Specified by:
      writeFloat in interface DataOutput
      Parameters:
      f - the float value to be written.
      Throws:
      IOException - if an I/O error occurs.
    • writeDouble

      public final void writeDouble(double d) throws IOException
      Writes an 8 byte Java double to the underlying output stream in little endian order.
      Specified by:
      writeDouble in interface DataOutput
      Parameters:
      d - the double value to be written.
      Throws:
      IOException - if an I/O error occurs.
    • writeBytes

      public void writeBytes(String pString) throws IOException
      Writes a string to the underlying output stream as a sequence of bytes. Each character is written to the data output stream as if by the writeByte(int) method.
      Specified by:
      writeBytes in interface DataOutput
      Parameters:
      pString - the String value to be written.
      Throws:
      IOException - if the underlying stream throws an IOException.
      See Also:
    • writeChars

      public void writeChars(String pString) throws IOException
      Writes a string to the underlying output stream as a sequence of characters. Each character is written to the data output stream as if by the writeChar method.
      Specified by:
      writeChars in interface DataOutput
      Parameters:
      pString - a String value to be written.
      Throws:
      IOException - if the underlying stream throws an IOException.
      See Also:
    • writeUTF

      public void writeUTF(String pString) throws IOException
      Writes a string of no more than 65,535 characters to the underlying output stream using UTF-8 encoding. This method first writes a two byte short in big endian order as required by the UTF-8 specification. This gives the number of bytes in the UTF-8 encoded version of the string, not the number of characters in the string. Next each character of the string is written using the UTF-8 encoding for the character.
      Specified by:
      writeUTF in interface DataOutput
      Parameters:
      pString - the string to be written.
      Throws:
      UTFDataFormatException - if the string is longer than 65,535 characters.
      IOException - if the underlying stream throws an IOException.
    • size

      public int size()
      Returns the number of bytes written to this little endian output stream. (This class is not thread-safe with respect to this method. It is possible that this number is temporarily less than the actual number of bytes written.)
      Returns:
      the value of the written field.
      See Also: