Class RandomAccessFileOrArray

  • All Implemented Interfaces:
    java.io.DataInput

    public class RandomAccessFileOrArray
    extends java.lang.Object
    implements java.io.DataInput
    Class that is used to unify reading from random access files and arrays.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private byte back
      the pushed back byte, if any
      private IRandomAccessSource byteSource
      The source that backs this object
      private long byteSourcePosition
      The physical location in the underlying byte source.
      private boolean isBack
      Whether there is a pushed back byte
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes the underlying source.
      IRandomAccessSource createSourceView()
      Creates the view of the byte source of this object.
      RandomAccessFileOrArray createView()
      Creates an independent view of this object (with it's own file pointer and push back queue).
      private void ensureByteSourceIsThreadSafe()  
      long getPosition()
      Gets the current position of the source considering the pushed byte to the source.
      long length()
      Gets the total amount of bytes in the source.
      int peek()
      Gets the next byte without moving current position.
      int peek​(byte[] buffer)
      Gets the next buffer.length bytes without moving current position.
      void pushBack​(byte b)
      Pushes a byte back.
      int read()
      Reads a single byte
      int read​(byte[] b)
      Reads the bytes to the buffer.
      int read​(byte[] b, int off, int len)
      Reads the specified amount of bytes to the buffer applying the offset.
      boolean readBoolean()
      byte readByte()
      char readChar()
      char readCharLE()
      Reads a Unicode character from this stream in little-endian order.
      double readDouble()
      double readDoubleLE()  
      float readFloat()
      float readFloatLE()  
      void readFully​(byte[] b)
      void readFully​(byte[] b, int off, int len)
      int readInt()
      int readIntLE()
      Reads a signed 32-bit integer from this stream in little-endian order.
      java.lang.String readLine()
      long readLong()
      long readLongLE()  
      short readShort()
      short readShortLE()
      Reads a signed 16-bit number from this stream in little-endian order.
      java.lang.String readString​(int length, java.lang.String encoding)
      Reads a String from the font file as bytes using the given encoding.
      int readUnsignedByte()
      long readUnsignedInt()
      Reads an unsigned 32-bit integer from this stream.
      long readUnsignedIntLE()  
      int readUnsignedShort()
      int readUnsignedShortLE()
      Reads an unsigned 16-bit number from this stream in little-endian order.
      java.lang.String readUTF()
      void seek​(long pos)
      Sets the current position in the source to the specified index.
      long skip​(long n)
      Make an attempt to skip the specified amount of bytes in source.
      int skipBytes​(int n)
      • Methods inherited from class java.lang.Object

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

      • byteSourcePosition

        private long byteSourcePosition
        The physical location in the underlying byte source.
      • back

        private byte back
        the pushed back byte, if any
      • isBack

        private boolean isBack
        Whether there is a pushed back byte
    • Constructor Detail

      • RandomAccessFileOrArray

        public RandomAccessFileOrArray​(IRandomAccessSource byteSource)
        Creates a RandomAccessFileOrArray that wraps the specified byte source. The byte source will be closed when this RandomAccessFileOrArray is closed.
        Parameters:
        byteSource - the byte source to wrap
    • Method Detail

      • createView

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

        public IRandomAccessSource createSourceView()
        Creates the view of the byte source of this object. Closing the view won't affect this object. Closing source will have adverse effect on the view.
        Returns:
        the byte source 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 java.io.IOException
        Reads a single byte
        Returns:
        the byte, or -1 if EOF is reached
        Throws:
        java.io.IOException - in case of any reading error.
      • peek

        public int peek()
                 throws java.io.IOException
        Gets the next byte without moving current position.
        Returns:
        the next byte, or -1 if EOF is reached
        Throws:
        java.io.IOException - in case of any reading error.
      • peek

        public int peek​(byte[] buffer)
                 throws java.io.IOException
        Gets the next buffer.length bytes without moving current position.
        Parameters:
        buffer - buffer to store read bytes
        Returns:
        the number of read bytes. If it is less than buffer.length it means EOF has been reached.
        Throws:
        java.io.IOException - in case of any reading error.
      • read

        public int read​(byte[] b,
                        int off,
                        int len)
                 throws java.io.IOException
        Reads the specified amount of bytes to the buffer applying the offset.
        Parameters:
        b - destination buffer
        off - offset at which to start storing characters
        len - maximum number of characters to read
        Returns:
        the number of bytes actually read or -1 in case of EOF
        Throws:
        java.io.IOException - in case of any I/O error
      • read

        public int read​(byte[] b)
                 throws java.io.IOException
        Reads the bytes to the buffer. This method will try to read as many bytes as the buffer can hold.
        Parameters:
        b - the destination buffer
        Returns:
        the number of bytes actually read
        Throws:
        java.io.IOException - in case of any I/O error
      • readFully

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

        public void readFully​(byte[] b,
                              int off,
                              int len)
                       throws java.io.IOException
        Specified by:
        readFully in interface java.io.DataInput
        Throws:
        java.io.IOException
      • skip

        public long skip​(long n)
        Make an attempt to skip the specified amount of bytes in source. However it may skip less amount of bytes. Possibly zero.
        Parameters:
        n - the number of bytes to skip
        Returns:
        the actual number of bytes skipped
      • skipBytes

        public int skipBytes​(int n)
        Specified by:
        skipBytes in interface java.io.DataInput
      • close

        public void close()
                   throws java.io.IOException
        Closes the underlying source.
        Throws:
        java.io.IOException - in case of any I/O error.
      • length

        public long length()
        Gets the total amount of bytes in the source.
        Returns:
        source's size.
      • seek

        public void seek​(long pos)
        Sets the current position in the source to the specified index.
        Parameters:
        pos - the position to set
      • getPosition

        public long getPosition()
        Gets the current position of the source considering the pushed byte to the source.
        Returns:
        the index of last read byte in the source in or the index of last read byte in source - 1 in case byte was pushed.
      • readBoolean

        public boolean readBoolean()
                            throws java.io.IOException
        Specified by:
        readBoolean in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readByte

        public byte readByte()
                      throws java.io.IOException
        Specified by:
        readByte in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readUnsignedByte

        public int readUnsignedByte()
                             throws java.io.IOException
        Specified by:
        readUnsignedByte in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readShort

        public short readShort()
                        throws java.io.IOException
        Specified by:
        readShort in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readShortLE

        public final short readShortLE()
                                throws java.io.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:
        java.io.EOFException - if this stream reaches the end before reading two bytes.
        java.io.IOException - if an I/O error occurs.
      • readUnsignedShort

        public int readUnsignedShort()
                              throws java.io.IOException
        Specified by:
        readUnsignedShort in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readUnsignedShortLE

        public final int readUnsignedShortLE()
                                      throws java.io.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:
        java.io.EOFException - if this stream reaches the end before reading two bytes.
        java.io.IOException - if an I/O error occurs.
      • readChar

        public char readChar()
                      throws java.io.IOException
        Specified by:
        readChar in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readCharLE

        public final char readCharLE()
                              throws java.io.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:
        java.io.EOFException - if this stream reaches the end before reading two bytes.
        java.io.IOException - if an I/O error occurs.
      • readInt

        public int readInt()
                    throws java.io.IOException
        Specified by:
        readInt in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readIntLE

        public final int readIntLE()
                            throws java.io.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:
        java.io.EOFException - if this stream reaches the end before reading four bytes.
        java.io.IOException - if an I/O error occurs.
      • readUnsignedInt

        public final long readUnsignedInt()
                                   throws java.io.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:
        java.io.EOFException - if this stream reaches the end before reading four bytes.
        java.io.IOException - if an I/O error occurs.
      • readUnsignedIntLE

        public final long readUnsignedIntLE()
                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • readLong

        public long readLong()
                      throws java.io.IOException
        Specified by:
        readLong in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readLongLE

        public final long readLongLE()
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • readFloat

        public float readFloat()
                        throws java.io.IOException
        Specified by:
        readFloat in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readFloatLE

        public final float readFloatLE()
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • readDouble

        public double readDouble()
                          throws java.io.IOException
        Specified by:
        readDouble in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readDoubleLE

        public final double readDoubleLE()
                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • readLine

        public java.lang.String readLine()
                                  throws java.io.IOException
        Specified by:
        readLine in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readUTF

        public java.lang.String readUTF()
                                 throws java.io.IOException
        Specified by:
        readUTF in interface java.io.DataInput
        Throws:
        java.io.IOException
      • readString

        public java.lang.String readString​(int length,
                                           java.lang.String encoding)
                                    throws java.io.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:
        java.io.IOException - the font file could not be read
      • ensureByteSourceIsThreadSafe

        private void ensureByteSourceIsThreadSafe()