Class BufferedRandomAccessReader

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class BufferedRandomAccessReader
    extends Object
    implements Closeable
    Accesses a random access file using a ByteBuffer with little-endian byte order in support of the LAS file format.
    • Constructor Summary

      Constructors 
      Constructor Description
      BufferedRandomAccessReader​(File file)
      Opens the specified file for read-only, random-access.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes the file resources used by the referenced instance.
      long getFilePosition()
      Provides the current position within the random-access file.
      File getFileReference()
      Gets a File object referring to the currently open file (if any).
      long getFileSize()
      Gets the current size of the file in bytes.
      String readAscii​(int maximumLength)
      Reads a C/C++ style null-terminated string of a specified maximum length from the from data file.
      int readAscii​(StringBuilder builder, int maximumLength)
      Reads a C/C++ style null-terminated string of a specified maximum length from the from data file.
      boolean readBoolean()
      Reads one input byte and returns true if that byte is nonzero, false if that byte is zero.
      byte readByte()
      Reads and returns one input byte.
      double readDouble()
      Read 8 bytes from the file and returns a Java double.
      double readDoubleBigEndian()
      Read 8 bytes from the file and returns a Java double.
      float readFloat()
      Reads 4 bytes from the file and returns a Java float.
      int readInt()
      Read 4 bytes and return Java integer.
      int readIntBigEndian()
      Read 4 bytes and return Java integer.
      long readLong()
      Read 8 bytes from the file and returns a java long
      short readShort()
      Reads two bytes from the file treating them as being in little-endian order and returns a short.
      int readUnsignedByte()
      Reads one input byte and returns an integer value in the range 0 through 255.
      long readUnsignedInt()
      Reads 4 bytes given in little-endian order and and returns a Java long primitive given values in the range 0 through 4294967295.
      int readUnsignedShort()
      Read two bytes and returns a Java int primitive.
      void seek​(long position)
      Sets the virtual file-pointer position measured from the beginning of the file.
      int skipBytes​(int n)
      Makes an attempt to advance the virtual file position by n bytes in order to match the functionality of the DataInput interface.
    • Constructor Detail

      • BufferedRandomAccessReader

        public BufferedRandomAccessReader​(File file)
                                   throws IOException
        Opens the specified file for read-only, random-access. A buffer is created to provide efficient input operations across a series of sequential method calls. In accordance with the LAS standard, all data in the file is treated as being in little-endian byte order.
        Parameters:
        file - A valid file object.
        Throws:
        IOException - In the event of an unrecoverable IO condition such as file not found, access denied, etc.
    • Method Detail

      • close

        public void close()
                   throws IOException
        Closes the file resources used by the referenced instance. No further file access will be possible.
        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable
        Throws:
        IOException - In the event of an unrecoverable I/O condition.
      • getFileReference

        public File getFileReference()
        Gets a File object referring to the currently open file (if any).
        Returns:
        A valid object if defined; otherwise, null.
      • getFileSize

        public long getFileSize()
        Gets the current size of the file in bytes.
        Returns:
        A long integer giving file size in bytes.
      • getFilePosition

        public long getFilePosition()
        Provides the current position within the random-access file.
        Returns:
        a long integer value giving offset in bytes from beginning of file.
      • readAscii

        public String readAscii​(int maximumLength)
                         throws IOException
        Reads a C/C++ style null-terminated string of a specified maximum length from the from data file. The source data is treated as specifying a string as one byte per character following the ISO-8859-1 standard. If a zero byte is encountered in the sequence, the string is terminated. Otherwise, it is extended out to the maximum length. Regardless of how many characters are read, the file position is always adjusted forward by the maximum length.
        Parameters:
        maximumLength - Maximum number of bytes to be read from file.
        Returns:
        A valid, potentially empty string.
        Throws:
        IOException - In the event of an unrecoverable I/O condition.
      • readAscii

        public int readAscii​(StringBuilder builder,
                             int maximumLength)
                      throws IOException
        Reads a C/C++ style null-terminated string of a specified maximum length from the from data file. The source data is treated as specifying a string as one byte per character following the ISO-8859-1 standard. If a zero byte is encountered in the sequence, the string is terminated. Otherwise, it is extended out to the maximum length. Regardless of how many characters are read, the file position is always adjusted forward by the maximum length.
        Parameters:
        builder - The StringBuilder to which data is appended; if the builder already contains text, then it will not be clear out before the data is written.
        maximumLength - Maximum number of bytes to be read from file.
        Returns:
        Number of valid characters extracted from the file before a null was encountered.
        Throws:
        IOException - In the event of an unrecoverable I/O condition.
      • readBoolean

        public boolean readBoolean()
                            throws IOException
        Reads one input byte and returns true if that byte is nonzero, false if that byte is zero. This method may be used to read the byte written by the writeBoolean method of interface DataOutput.
        Returns:
        The boolean value read from the file.
        Throws:
        IOException - In the event of an unrecoverable I/O condition.
      • readByte

        public byte readByte()
                      throws IOException
        Reads and returns one input byte. The byte is treated as a signed value in the range -128 through 127, inclusive. This method may be used to read the byte written by the writeByte method of interface DataOutput.
        Returns:
        The 8-bit value read.
        Throws:
        IOException - In the event of an unrecoverable I/O condition.
      • readUnsignedByte

        public int readUnsignedByte()
                             throws IOException
        Reads one input byte and returns an integer value in the range 0 through 255.
        Returns:
        An integer primitive based on the unsigned value of a byte read from the source file.
        Throws:
        IOException - In the event of an unrecoverable I/O condition.
      • readUnsignedInt

        public long readUnsignedInt()
                             throws IOException
        Reads 4 bytes given in little-endian order and and returns a Java long primitive given values in the range 0 through 4294967295.
        Returns:
        a Java long correctly interpreted from the unsigned integer (4-byte) value stored in the data file.
        Throws:
        IOException - In the event of an unrecoverable I/O condition.
      • readUnsignedShort

        public int readUnsignedShort()
                              throws IOException
        Read two bytes and returns a Java int primitive.
        Returns:
        A Java integer primitive in the range 0 to 65535.
        Throws:
        IOException - In the event of an unrecoverable I/O condition.
      • readInt

        public int readInt()
                    throws IOException
        Read 4 bytes and return Java integer.
        Returns:
        A Java integer primitive.
        Throws:
        IOException - In the event of an unrecoverable I/O condition.
      • readIntBigEndian

        public int readIntBigEndian()
                             throws IOException
        Read 4 bytes and return Java integer.
        Returns:
        A Java integer primitive.
        Throws:
        IOException - In the event of an unrecoverable I/O condition.
      • readDoubleBigEndian

        public double readDoubleBigEndian()
                                   throws IOException
        Read 8 bytes from the file and returns a Java double.
        Returns:
        A Java double primitive.
        Throws:
        IOException - In the event of an unrecoverable I/O condition.
      • readDouble

        public double readDouble()
                          throws IOException
        Read 8 bytes from the file and returns a Java double.
        Returns:
        A Java double primitive.
        Throws:
        IOException - In the event of an unrecoverable I/O condition.
      • readFloat

        public float readFloat()
                        throws IOException
        Reads 4 bytes from the file and returns a Java float.
        Returns:
        A Java float primitive.
        Throws:
        IOException - In the event of an unrecoverable I/O condition.
      • readLong

        public long readLong()
                      throws IOException
        Read 8 bytes from the file and returns a java long
        Returns:
        AJava long primitive
        Throws:
        IOException - In the event of an unrecoverable I/O condition.
      • readShort

        public short readShort()
                        throws IOException
        Reads two bytes from the file treating them as being in little-endian order and returns a short.
        Returns:
        A Java short primitive in the range -32768 to 32767.
        Throws:
        IOException - In the event of an unrecoverable I/O condition.
      • seek

        public void seek​(long position)
                  throws IOException
        Sets the virtual file-pointer position measured from the beginning of the file.
        Parameters:
        position - The file position, measured in bytes from the beginning of the file at which to set the virtual file position.
        Throws:
        IOException - In the event of an unrecoverable I/O condition.
      • skipBytes

        public int skipBytes​(int n)
                      throws IOException
        Makes an attempt to advance the virtual file position by n bytes in order to match the functionality of the DataInput interface.
        Parameters:
        n - The number of bytes byte which to advance the file position
        Returns:
        the number of bytes skipped.
        Throws:
        IOException - In the event of an unrecoverable I/O condition.