Class RandomAccessFileOrArray

  • All Implemented Interfaces:
    DataInput

    public class RandomAccessFileOrArray
    extends Object
    implements DataInput
    Intended to be layered on top of a low level RandomAccessSource object. Provides functionality useful during parsing:
    • tracks current position in the file
    • allows single byte pushback
    • allows reading of multi-byte data structures (int, long, String) for both Big and Little Endian representations
    • allows creation of independent 'views' of the underlying data source
    Author:
    Paulo Soares, Kevin Day
    • Method Detail

      • createView

        public RandomAccessFileOrArray createView()
        Creates an independent view of this object (with it's own file pointer and pushback queue). Closing the new object will not close this object. Closing this object will have adverse effect on the view.
        Returns:
        the new view
      • pushBack

        public void pushBack​(byte b)
        Pushes a byte back. The next get() will return this byte instead of the value from the underlying data source
        Parameters:
        b - the byte to push
      • read

        public int read()
                 throws IOException
        Reads a single byte
        Returns:
        the byte, or -1 if EOF is reached
        Throws:
        IOException
      • readShortLE

        public final short readShortLE()
                                throws IOException
        Reads a signed 16-bit number from this stream in little-endian order. The method reads two bytes from this stream, starting at the current stream pointer. If the two bytes read, in order, are b1 and b2, where each of the two values is between 0 and 255, inclusive, then the result is equal to:
             (short)((b2 << 8) | b1)
         

        This method blocks until the two bytes are read, the end of the stream is detected, or an exception is thrown.

        Returns:
        the next two bytes of this stream, interpreted as a signed 16-bit number.
        Throws:
        EOFException - if this stream reaches the end before reading two bytes.
        IOException - if an I/O error occurs.
      • readUnsignedShortLE

        public final int readUnsignedShortLE()
                                      throws IOException
        Reads an unsigned 16-bit number from this stream in little-endian order. This method reads two bytes from the stream, starting at the current stream pointer. If the bytes read, in order, are b1 and b2, where 0 <= b1, b2 <= 255, then the result is equal to:
             (b2 << 8) | b1
         

        This method blocks until the two bytes are read, the end of the stream is detected, or an exception is thrown.

        Returns:
        the next two bytes of this stream, interpreted as an unsigned 16-bit integer.
        Throws:
        EOFException - if this stream reaches the end before reading two bytes.
        IOException - if an I/O error occurs.
      • readCharLE

        public final char readCharLE()
                              throws IOException
        Reads a Unicode character from this stream in little-endian order. This method reads two bytes from the stream, starting at the current stream pointer. If the bytes read, in order, are b1 and b2, where 0 <= b1, b2 <= 255, then the result is equal to:
             (char)((b2 << 8) | b1)
         

        This method blocks until the two bytes are read, the end of the stream is detected, or an exception is thrown.

        Returns:
        the next two bytes of this stream as a Unicode character.
        Throws:
        EOFException - if this stream reaches the end before reading two bytes.
        IOException - if an I/O error occurs.
      • readIntLE

        public final int readIntLE()
                            throws IOException
        Reads a signed 32-bit integer from this stream in little-endian order. This method reads 4 bytes from the stream, starting at the current stream pointer. If the bytes read, in order, are b1, b2, b3, and b4, where 0 <= b1, b2, b3, b4 <= 255, then the result is equal to:
             (b4 << 24) | (b3 << 16) + (b2 << 8) + b1
         

        This method blocks until the four bytes are read, the end of the stream is detected, or an exception is thrown.

        Returns:
        the next four bytes of this stream, interpreted as an int.
        Throws:
        EOFException - if this stream reaches the end before reading four bytes.
        IOException - if an I/O error occurs.
      • readUnsignedInt

        public final long readUnsignedInt()
                                   throws IOException
        Reads an unsigned 32-bit integer from this stream. This method reads 4 bytes from the stream, starting at the current stream pointer. If the bytes read, in order, are b1, b2, b3, and b4, where 0 <= b1, b2, b3, b4 <= 255, then the result is equal to:
             (b1 << 24) | (b2 << 16) + (b3 << 8) + b4
         

        This method blocks until the four bytes are read, the end of the stream is detected, or an exception is thrown.

        Returns:
        the next four bytes of this stream, interpreted as a long.
        Throws:
        EOFException - if this stream reaches the end before reading four bytes.
        IOException - if an I/O error occurs.
      • readString

        public String readString​(int length,
                                 String encoding)
                          throws IOException
        Reads a String from the font file as bytes using the given encoding.
        Parameters:
        length - the length of bytes to read
        encoding - the given encoding
        Returns:
        the String read
        Throws:
        IOException - the font file could not be read