Package org.tinfour.io
Class BufferedRandomAccessReader
- java.lang.Object
-
- org.tinfour.io.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 voidclose()Closes the file resources used by the referenced instance.longgetFilePosition()Provides the current position within the random-access file.FilegetFileReference()Gets a File object referring to the currently open file (if any).longgetFileSize()Gets the current size of the file in bytes.StringreadAscii(int maximumLength)Reads a C/C++ style null-terminated string of a specified maximum length from the from data file.intreadAscii(StringBuilder builder, int maximumLength)Reads a C/C++ style null-terminated string of a specified maximum length from the from data file.booleanreadBoolean()Reads one input byte and returnstrueif that byte is nonzero,falseif that byte is zero.bytereadByte()Reads and returns one input byte.doublereadDouble()Read 8 bytes from the file and returns a Java double.doublereadDoubleBigEndian()Read 8 bytes from the file and returns a Java double.floatreadFloat()Reads 4 bytes from the file and returns a Java float.intreadInt()Read 4 bytes and return Java integer.intreadIntBigEndian()Read 4 bytes and return Java integer.longreadLong()Read 8 bytes from the file and returns a java longshortreadShort()Reads two bytes from the file treating them as being in little-endian order and returns a short.intreadUnsignedByte()Reads one input byte and returns an integer value in the range 0 through 255.longreadUnsignedInt()Reads 4 bytes given in little-endian order and and returns a Java long primitive given values in the range 0 through 4294967295.intreadUnsignedShort()Read two bytes and returns a Java int primitive.voidseek(long position)Sets the virtual file-pointer position measured from the beginning of the file.intskipBytes(int n)Makes an attempt to advance the virtual file position bynbytes 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 IOExceptionCloses the file resources used by the referenced instance. No further file access will be possible.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- 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 IOExceptionReads one input byte and returnstrueif that byte is nonzero,falseif 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 IOExceptionReads 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 IOExceptionReads 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 IOExceptionReads 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 IOExceptionRead 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 IOExceptionRead 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 IOExceptionRead 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 IOExceptionRead 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 IOExceptionRead 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 IOExceptionReads 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 IOExceptionRead 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 IOExceptionReads 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 IOExceptionSets 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 IOExceptionMakes an attempt to advance the virtual file position bynbytes 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.
-
-