Class LittleEndianRandomAccessFile

java.lang.Object
com.twelvemonkeys.io.LittleEndianRandomAccessFile
All Implemented Interfaces:
DataInput, DataOutput

public class LittleEndianRandomAccessFile extends Object implements DataInput, DataOutput
A replacement for RandomAccessFile that is capable of reading and writing data in little endian byte order.

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:
$Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/io/LittleEndianRandomAccessFile.java#1 $
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
     
     
    long
     
    long
     
    int
     
    int
    read(byte[] b)
     
    int
    read(byte[] b, int off, int len)
     
    boolean
    Reads a boolean from the underlying input stream by reading a single byte.
    byte
    Reads a signed byte from the underlying input stream with value between -128 and 127
    char
    Reads a two byte Unicode char from the underlying input stream in little endian order, low byte first.
    final double
     
    final float
     
    void
    readFully(byte[] b)
     
    void
    readFully(byte[] b, int off, int len)
     
    int
    Reads a four byte signed int from the underlying input stream in little endian order, low byte first.
     
    long
    Reads an eight byte signed int from the underlying input stream in little endian order, low byte first.
    short
    Reads a two byte signed short from the underlying input stream in little endian order, low byte first.
    int
    Reads an unsigned byte from the underlying input stream with value between 0 and 255
    int
    Reads a two byte unsigned short from the underlying input stream in little endian order, low byte first.
    Reads a string of no more than 65,535 characters from the underlying input stream using UTF-8 encoding.
    void
    seek(long pos)
    Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.
    void
    setLength(long newLength)
     
    int
    skipBytes(int n)
     
    void
    write(byte[] b)
     
    void
    write(byte[] b, int off, int len)
     
    void
    write(int b)
     
    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.lang.Object

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

  • Constructor Details

  • Method Details

    • close

      public void close() throws IOException
      Throws:
      IOException
    • getChannel

      public FileChannel getChannel()
    • getFD

      public FileDescriptor getFD() throws IOException
      Throws:
      IOException
    • getFilePointer

      public long getFilePointer() throws IOException
      Throws:
      IOException
    • length

      public long length() throws IOException
      Throws:
      IOException
    • read

      public int read() throws IOException
      Throws:
      IOException
    • read

      public int read(byte[] b) throws IOException
      Throws:
      IOException
    • read

      public int read(byte[] b, int off, int len) throws IOException
      Throws:
      IOException
    • readFully

      public void readFully(byte[] b) throws IOException
      Specified by:
      readFully in interface DataInput
      Throws:
      IOException
    • readFully

      public void readFully(byte[] b, int off, int len) throws IOException
      Specified by:
      readFully in interface DataInput
      Throws:
      IOException
    • readLine

      public String readLine() throws IOException
      Specified by:
      readLine in interface DataInput
      Throws:
      IOException
    • readBoolean

      public boolean readBoolean() throws IOException
      Reads a boolean from the underlying input stream by reading a single byte. If the byte is zero, false is returned. If the byte is positive, true is returned.
      Specified by:
      readBoolean in interface DataInput
      Returns:
      the boolean value read.
      Throws:
      EOFException - if the end of the underlying input stream has been reached
      IOException - if the underlying stream throws an IOException.
    • readByte

      public byte readByte() throws IOException
      Reads a signed byte from the underlying input stream with value between -128 and 127
      Specified by:
      readByte in interface DataInput
      Returns:
      the byte value read.
      Throws:
      EOFException - if the end of the underlying input stream has been reached
      IOException - if the underlying stream throws an IOException.
    • readUnsignedByte

      public int readUnsignedByte() throws IOException
      Reads an unsigned byte from the underlying input stream with value between 0 and 255
      Specified by:
      readUnsignedByte in interface DataInput
      Returns:
      the byte value read.
      Throws:
      EOFException - if the end of the underlying input stream has been reached
      IOException - if the underlying stream throws an IOException.
    • readShort

      public short readShort() throws IOException
      Reads a two byte signed short from the underlying input stream in little endian order, low byte first.
      Specified by:
      readShort in interface DataInput
      Returns:
      the short read.
      Throws:
      EOFException - if the end of the underlying input stream has been reached
      IOException - if the underlying stream throws an IOException.
    • readUnsignedShort

      public int readUnsignedShort() throws IOException
      Reads a two byte unsigned short from the underlying input stream in little endian order, low byte first.
      Specified by:
      readUnsignedShort in interface DataInput
      Returns:
      the int value of the unsigned short read.
      Throws:
      EOFException - if the end of the underlying input stream has been reached
      IOException - if the underlying stream throws an IOException.
    • readChar

      public char readChar() throws IOException
      Reads a two byte Unicode char from the underlying input stream in little endian order, low byte first.
      Specified by:
      readChar in interface DataInput
      Returns:
      the int value of the unsigned short read.
      Throws:
      EOFException - if the end of the underlying input stream has been reached
      IOException - if the underlying stream throws an IOException.
    • readInt

      public int readInt() throws IOException
      Reads a four byte signed int from the underlying input stream in little endian order, low byte first.
      Specified by:
      readInt in interface DataInput
      Returns:
      the int read.
      Throws:
      EOFException - if the end of the underlying input stream has been reached
      IOException - if the underlying stream throws an IOException.
    • readLong

      public long readLong() throws IOException
      Reads an eight byte signed int from the underlying input stream in little endian order, low byte first.
      Specified by:
      readLong in interface DataInput
      Returns:
      the int read.
      Throws:
      EOFException - if the end of the underlying input stream has been reached
      IOException - if the underlying stream throws an IOException.
    • readUTF

      public String readUTF() throws IOException
      Reads a string of no more than 65,535 characters from the underlying input stream using UTF-8 encoding. This method first reads 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. Next this many bytes are read and decoded as UTF-8 encoded characters.
      Specified by:
      readUTF in interface DataInput
      Returns:
      the decoded string
      Throws:
      UTFDataFormatException - if the string cannot be decoded
      IOException - if the underlying stream throws an IOException.
    • readDouble

      public final double readDouble() throws IOException
      Specified by:
      readDouble in interface DataInput
      Returns:
      the next eight bytes of this input stream, interpreted as a little endian double.
      Throws:
      EOFException - if end of stream occurs before eight bytes have been read.
      IOException - if an I/O error occurs.
    • readFloat

      public final float readFloat() throws IOException
      Specified by:
      readFloat in interface DataInput
      Returns:
      the next four bytes of this input stream, interpreted as a little endian int.
      Throws:
      EOFException - if end of stream occurs before four bytes have been read.
      IOException - if an I/O error occurs.
    • seek

      public void seek(long pos) throws IOException
      Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs. The offset may be set beyond the end of the file. Setting the offset beyond the end of the file does not change the file length. The file length will change only by writing after the offset has been set beyond the end of the file.
      Parameters:
      pos - the offset position, measured in bytes from the beginning of the file, at which to set the file pointer.
      Throws:
      IOException - if pos is less than 0 or if an I/O error occurs.
    • setLength

      public void setLength(long newLength) throws IOException
      Throws:
      IOException
    • skipBytes

      public int skipBytes(int n) throws IOException
      Specified by:
      skipBytes in interface DataInput
      Throws:
      IOException
    • write

      public void write(byte[] b) throws IOException
      Specified by:
      write in interface DataOutput
      Throws:
      IOException
    • write

      public void write(byte[] b, int off, int len) throws IOException
      Specified by:
      write in interface DataOutput
      Throws:
      IOException
    • write

      public void write(int b) throws IOException
      Specified by:
      write in interface DataOutput
      Throws:
      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() 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.