Package com.irurueta.geometry.io
Class AbstractFileReaderAndWriter
java.lang.Object
com.irurueta.geometry.io.AbstractFileReaderAndWriter
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
FileReaderAndWriter,MappedFileReaderAndWriter
Abstract class that provides methods to access file data at random positions.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract longReturns the current offset in this file.abstract booleanDetermines whether end of file has been reached (next read() will return -1). or not.abstract intread()Reads one byte at current file position and advances one position.abstract intread(byte[] b) Reads up to b.length bytes of data from this file into an array of bytes.abstract intread(byte[] b, int off, int len) Reads up to len bytes of data from this file into an array of bytes.abstract booleanReads a boolean from this file.abstract bytereadByte()Reads a signed eight-bit value from this file.abstract doubleReads a double from this file.abstract doublereadDouble(EndianType endianType) Reads a double from this file.abstract floatReads a float from this file.abstract floatreadFloat(EndianType endianType) Reads a float from this file.abstract intreadInt()Reads a signed 32-bit integer from this file.abstract intreadInt(EndianType endianType) Reads a signed 32-bit integer from this file.abstract StringreadLine()Reads the next line of text from this file.abstract longreadLong()Reads a signed 64-bit integer from this file.abstract longreadLong(EndianType endianType) Reads a signed 64-bit integer from this file.abstract shortReads a signed 16-bit number from this file.abstract shortreadShort(EndianType endianType) Reads a signed 16-bit number from this file assuming that file is encoded using provided endian type.abstract shortReads an unsigned eight-bit number from this file.abstract longReads an unsigned 32-bit integer from this file.abstract longreadUnsignedInt(EndianType endianType) Reads an unsigned 32-bit integer from this file.abstract intReads an unsigned 16-bit number from this file.abstract intreadUnsignedShort(EndianType endianType) Reads an unsigned 16-bit number from this file.abstract StringSequentially reads characters starting at current file position until one of the characters in provided pattern is found.readWord()Sequentially reads characters starting at current file position until either carriage return, new line, tab or space character is found.abstract voidseek(long pos) Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.abstract longskip(long n) Attempts to skip over n byte of input discarding the skipped bytes.abstract voidwrite(byte[] b) Writes b.length bytes from the specified byte array to this file, starting at the current file pointer.abstract voidwrite(byte[] b, int off, int len) Writes len bytes from the specified byte array starting at offset off to this file.abstract voidwrite(int b) Writes the specified byte to this file.abstract voidwriteASCII(String s) Writes the string to the file as a sequence of bytes.abstract voidwriteBoolean(boolean v) Writes a boolean to the file as a one-byte value.abstract voidwriteByte(byte v) Writes a byte to the file as a one-byte value.abstract voidwriteDouble(double v) Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the file as an eight byte quantity, high byte first.abstract voidwriteDouble(double v, EndianType endianType) Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the file as an eight byte quantity, using provided endian type.abstract voidwriteFloat(float v) Converts the float argument to an int using the floatToIntBits method in class Float, and then write that int value to the file as a four-byte quantity, high byte first.abstract voidwriteFloat(float v, EndianType endianType) Converts the float argument to an int using the floatToIntBits method in class Float, and then write that int value to the file as a four-byte quantity, using provided endian type.abstract voidwriteInt(int v) Writes an int to the file as four bytes, high byte first.abstract voidwriteInt(int v, EndianType endianType) Writes an int to the file as four bytes, using provided endian type.abstract voidwriteLong(long v) Writes a long to the file as eight bytes, high byte first.abstract voidwriteLong(long v, EndianType endianType) Writes a long to the file as eight bytes, using provided endian type.abstract voidwriteShort(short v) Writes a short to the file as two bytes, high byte first.abstract voidwriteShort(short v, EndianType endianType) Writes a short to the file as two bytes using provided endian type.abstract voidwriteUnsignedByte(short v) Writes provided value in the range 0-255 as an unsigned byte.abstract voidwriteUnsignedInt(long v) Writes an unsigned int to the file as four bytes, high byte first.abstract voidwriteUnsignedInt(long v, EndianType endianType) Writes an unsigned int to the file as four bytes, using provided endian type.abstract voidwriteUnsignedShort(int v) Writes an unsigned short to the file as two bytes, high byte first.abstract voidwriteUnsignedShort(int v, EndianType endianType) Writes an unsigned short to the file as two bytes, using provided endian type.
-
Constructor Details
-
AbstractFileReaderAndWriter
public AbstractFileReaderAndWriter()
-
-
Method Details
-
read
Reads one byte at current file position and advances one position.- Returns:
- Next byte of data or -1 if end of file is reached.
- Throws:
IOException- if an I/O error occurs. Not thrown if end-of-file has been reached.
-
read
Reads up to b.length bytes of data from this file into an array of bytes. This method blocks until at least one byte of input is available.- Parameters:
b- The buffer into which the data is read.- Returns:
- The total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.
- Throws:
IOException- If the first byte cannot be read for any reason other than end of file, or if the file has been closed, or if some other I/O error occurs.
-
read
Reads up to len bytes of data from this file into an array of bytes. This method blocks until at least one byte of input is available. This method behaves in exactly the same way as the InputStream.read(byte[], int, int) method of InputStream.- Parameters:
b- the buffer into which the data is read.off- the start offset in array b at which the data is written.len- the maximum number of bytes read.- Returns:
- the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.
- Throws:
IOException- If the first byte cannot be read for any reason other than end of file, or if the random access file has been closed, or if some other I/O error occurs.
-
skip
Attempts to skip over n byte of input discarding the skipped bytes.This method may skip over some number of bytes, possibly zero. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility. The actual number of bytes skipped is returned. If n is negative, no bytes are skipped.
- Parameters:
n- the number of bytes to be skipped.- Returns:
- the actual number of bytes skipped.
- Throws:
IOException- if an I/O error occurs.
-
write
Writes the specified byte to this file. The write starts at the current file pointer.- Parameters:
b- the byte to be written.- Throws:
IOException- if an I/O error occurs.
-
write
Writes b.length bytes from the specified byte array to this file, starting at the current file pointer.- Parameters:
b- the data.- Throws:
IOException- if an I/O error occurs.
-
write
Writes len bytes from the specified byte array starting at offset off to this file.- Parameters:
b- the data.off- the start offset in the data.len- the number of bytes to write.- Throws:
IOException- if an I/O error occurs.
-
getPosition
Returns the current offset in this file.- Returns:
- the offset from the beginning of the file, in bytes, at which the next read or write occurs.
- Throws:
IOException- in an I/O error occurs.
-
isEndOfStream
Determines whether end of file has been reached (next read() will return -1). or not.- Returns:
- True if end of file has been reached, false otherwise.
- Throws:
IOException- if an I/O error occurs.
-
seek
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.
-
readBoolean
Reads a boolean from this file. This method reads a single byte from the file, starting at the current file pointer. A value of 0 represents false. Any other value represents true. This method blocks until the byte is read, the end of the stream is detected, or an exception is thrown.- Returns:
- the boolean value read.
- Throws:
IOException- if an I/O error occurs.
-
readByte
Reads a signed eight-bit value from this file. This method reads a byte from the file, starting from the current file pointer. If the byte read is b, where 0 <= b <= 255, then the result is: (byte)(b) This method blocks until the byte is read, the end of the stream is detected, or an exception is thrown.- Returns:
- the next byte of this file is a signed eight-bit byte.
- Throws:
IOException- if an I/O error occurs.
-
readUnsignedByte
Reads an unsigned eight-bit number from this file. This method reads a byte from this file, starting at the current file pointer, and returns that byte. This method blocks until the byte is read, the end of the stream is detected, or an exception is thrown.- Returns:
- the next byte of this file, interpreted as an unsigned eight-bit number.
- Throws:
IOException- if an I/O error occurs.
-
readShort
Reads a signed 16-bit number from this file. The method reads two byte from this file, starting at the current file 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)(b1 << 8 | b2). 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 file, interpreted as a signed 16-bit number.
- Throws:
IOException- if an I/O error occurs.
-
readShort
Reads a signed 16-bit number from this file assuming that file is encoded using provided endian type. If endian type is big endian type, then natural binary order is preserved, otherwise byte order is reversed. This method blocks until the two bytes of the 16-bit number are read, the end of the stream is detected, or an exception is thrown.- Parameters:
endianType- Endian type. Big endian preserves natural binary order, little endian reverses byte order.- Returns:
- the next two bytes of this file, interpreted as a signed 16-bit number encoded in provided endian type.
- Throws:
IOException- if an I/O error occurs.
-
readUnsignedShort
Reads an unsigned 16-bit number from this file. This method reads two bytes from this file, starting at the current file pointer. If the bytes read, in order, are b1 and b2, where 0 <= b1, b2 <= 255, then the result is equal to: (b1 << 8) | b2 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 file, interpreted as an unsigned 16-bit integer.
- Throws:
IOException- if an I/O error occurs.
-
readUnsignedShort
Reads an unsigned 16-bit number from this file. This method reads two bytes from this file, starting at the current file pointer and using provided endian type. If endian type is big endian, then natural binary order is preserved, otherwise byte order is reversed. This method blocks until the two bytes are read, the end of the stream is detected, or an exception is thrown.- Parameters:
endianType- Endian type. Big endian preserves natural binary order, little endian reverses byte order.- Returns:
- the next two bytes of this file, interpreted as an unsigned 16-bit integer.
- Throws:
IOException- if an I/O error occurs.
-
readInt
Reads a signed 32-bit integer from this file. This method reads 4 bytes from the file, starting at the current file pointer. If the bytes read, in order, are b1, b2, b3, and b4, where 0 <= b1, 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 file, interpreted as an int.
- Throws:
IOException- if an I/O error occurs.
-
readInt
Reads a signed 32-bit integer from this file. This method reads 4 bytes from the file, starting at the current file pointer and using provided endian type. If endian type is big endian, then natural binary order is preserved, otherwise byte order is reversed. This method blocks until the four bytes are read, the end of the stream is detected, or an exception is thrown.- Parameters:
endianType- Endian type. Big endian preserves natural binary order, little endian reverses byte order.- Returns:
- the next four bytes of this file, interpreted as an int.
- Throws:
IOException- if an I/O error occurs.
-
readUnsignedInt
Reads an unsigned 32-bit integer from this file. This method reads 4 bytes from the file, starting at the current file pointer. If the bytes read, in order, are b1, b2, b3, and b4, where 0 <= b1, 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 file, interpreted as a long.
- Throws:
IOException- if an I/O error occurs.
-
readUnsignedInt
Reads an unsigned 32-bit integer from this file. This method reads 4 bytes from the file, starting at the current file pointer and using provided endian type. If endian type is big endian, then natural binary order is preserved, otherwise byte order is reversed. This method blocks until the four bytes are read, the end of the stream is detected, or an exception is thrown.- Parameters:
endianType- Endian type. Big endian preserves natural binary order, little endian reverses byte order.- Returns:
- the next four bytes of this file, interpreted as an int.
- Throws:
IOException- if an I/O error occurs.
-
readLong
Reads a signed 64-bit integer from this file. This method reads eight bytes from the file, starting at the current file pointer. If the bytes read, in order, are b1, b2, b3, b4, b5, b6, b7, and b8, where: 0 <= b1, b2, b3, b4. b5. b6. b7. b8 <= 255, then the result is equal to: ((long)b1 << 56) + ((long)b2 << 48) + ((long)b3 << 40) + ((long)b4 << 32) + ((long)b5 << 24) + ((long)b6 << 16) + ((long)b7 << 8) + b8 This method blocks until the eight bytes are read, the end of the stream is detected, or an exception is thrown.- Returns:
- the next eight bytes of this file, interpreted as a long.
- Throws:
IOException- if an I/O error occurs.
-
readLong
Reads a signed 64-bit integer from this file. This method reads eight bytes from the file, starting at the current file pointer and using provided endian type. If endian type is big endian, then natural binary order is preserved, otherwise byte order is reversed. This method blocks until the eight bytes are read, the end of the stream is detected, or an exception is thrown.- Parameters:
endianType- Endian type. Big endian preserves natural binary order, little endian reverses byte order.- Returns:
- the next eight bytes of this file, interpreted as a long
- Throws:
IOException- if an I/O error occurs.
-
readFloat
Reads a float from this file. This method reads an int value, starting at the current file pointer, as if by the readInt method and then converts that in to a float using the intBitsToFloat method in class Float. 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 file, interpreted as a float.
- Throws:
IOException- if an I/O error occurs.
-
readFloat
Reads a float from this file. This method reads four bytes using provided endian type. If endian type is big endian, then natural binary order is preserved, otherwise byte order is reversed. This method blocks until the four bytes are read, the end of the stream is detected, or an exception is thrown.- Parameters:
endianType- Endian type. Big endian preserves natural binary order, little endian reverses byte order.- Returns:
- the next four bytes of this file, interpreted as a float.
- Throws:
IOException- if an I/O error occurs.
-
readDouble
Reads a double from this file. This method reads a long value, starting at the current file pointer, as if by the readLong method and then converts that long to a double using the longBitsToDouble method in class Double. This method blocks until the eight bytes are read, the end of the stream is detected, or an exception is thrown.- Returns:
- the next eight bytes of this file, interpreted as a double.
- Throws:
IOException- if an I/O error occurs.
-
readDouble
Reads a double from this file. This method reads eight bytes using provided endian type. If endian type is big endian, then natural binary order is preserved, otherwise byte order is reversed. This method blocks until the eight bytes are read, the end of the stream is detected, or an exception is thrown.- Parameters:
endianType- Endian type. Big endian preserves natural binary order, little endian reverses byte order.- Returns:
- the next eight bytes of this file, interpreted as a double.
- Throws:
IOException- if an I/O error occurs.
-
readLine
Reads the next line of text from this file. This method successively reads bytes from the file, starting at the current file pointer, until it reaches a line terminator of the end of the file. Each byte is converted into a character by taking the byte's value for the lower eight bits of the character and setting the high eight bits of the character to zero. This method does not, therefore, support the full Unicode character set. A line of text is terminated by a carriage-return character ('\r'), a newline character('\n'), a carriage-return character immediately followed by a newline character, or the end of the file. Line-terminating characters are discarded and are not included as part of the string returned. This method blocks until a newline character is read, a carriage return and the byte following it are read (to see if it is a newline), the end of the file is reached, or an exception is thrown.- Returns:
- the next line of text from this file, or null if end of file is encountered before even one byte is read.
- Throws:
IOException- if an I/O error occurs.
-
readWord
Sequentially reads characters starting at current file position until either carriage return, new line, tab or space character is found. The result is considered a word and it is returned.- Returns:
- Next work starting at current file position or null if end of stream is reached.
- Throws:
IOException- if an I/O error occurs.
-
readUntilAnyOfTheseCharactersIsFound
Sequentially reads characters starting at current file position until one of the characters in provided pattern is found. All characters read so far will be returned without including any of the pattern characters.- Parameters:
pattern- Stop characters to stop reading when they are found.- Returns:
- String read so far until any of the pattern characters was found.
- Throws:
IOException- if an I/O error occurs.IllegalArgumentException- if no pattern characters are provided.
-
writeBoolean
Writes a boolean to the file as a one-byte value. The value true is written out as the value (byte)1; the value false is written out as the value (byte)0. The write starts at the current position of the file pointer.- Parameters:
v- a boolean value to be written.- Throws:
IOException- if an I/O error occurs.
-
writeByte
Writes a byte to the file as a one-byte value. The write starts at the current position of the file pointer.- Parameters:
v- a byte value to be written.- Throws:
IOException- if an I/O error occurs.
-
writeUnsignedByte
Writes provided value in the range 0-255 as an unsigned byte. The write starts at the current position of the file pointer.- Parameters:
v- a value to be written as an unsigned byte.- Throws:
IOException- if an I/O error occurs.
-
writeShort
Writes a short to the file as two bytes, high byte first. The write starts at the current position of the file pointer.- Parameters:
v- a short to be written.- Throws:
IOException- if an I/O error occurs.
-
writeShort
Writes a short to the file as two bytes using provided endian type. If endian type is big endian, then natural byte order is preserved (and high byte is written first), if little endian order is chosen, then byte order is reversed.- Parameters:
v- a short to be written.endianType- endian type. If it is big endian, natural byte order is preserved, otherwise byte order is reversed.- Throws:
IOException- if an I/O error occurs.
-
writeUnsignedShort
Writes an unsigned short to the file as two bytes, high byte first. Provided integer value is converted to an unsigned short by taking into account only the two lower bytes. The write starts at the current position of the file pointer.- Parameters:
v- an unsigned short to be written (int is converted to unsigned short).- Throws:
IOException- if an I/O error occurs.
-
writeUnsignedShort
Writes an unsigned short to the file as two bytes, using provided endian type. Provided integer value is converted to an unsigned short by taking into account only the two lower bytes. If endian type is big endian, then natural byte order is preserved (and high byte is written first), if little endian order is chosen, then byte order is reversed. The write starts at the current position of the file pointer.- Parameters:
v- an unsigned short to be written (int is converted to unsigned short).endianType- endian type. If it is big endian, natural byte order is preserved, otherwise byte order is reversed.- Throws:
IOException- if an I/O error occurs.
-
writeInt
Writes an int to the file as four bytes, high byte first. The write starts at the current position of the file pointer.- Parameters:
v- an int to be written.- Throws:
IOException- if an I/O error occurs.
-
writeInt
Writes an int to the file as four bytes, using provided endian type. If endian type is big endian, then natural byte order is preserved (and high byte is written first), if little endian order is chosen, then byte order is reversed. The write starts at the current position of the file pointer.- Parameters:
v- an int to be written.endianType- endian type. If it is big endian, natural byte order is preserved, otherwise byte order is reversed.- Throws:
IOException- if an I/O error occurs.
-
writeUnsignedInt
Writes an unsigned int to the file as four bytes, high byte first. Provided integer value is converted to an unsigned int by taking into account only the four lower bytes. The write starts at the current position of the file pointer.- Parameters:
v- an unsigned int to be written (long is converted to unsigned int).- Throws:
IOException- if an I/O error occurs.
-
writeUnsignedInt
Writes an unsigned int to the file as four bytes, using provided endian type. Provided integer value is converted to an unsigned int by taking into account only the four lower bytes. If endian type is big endian, then natural byte order is preserved (and high byte is written first), if little endian order is chosen, then byte order is reversed. The write starts at the current position of the file pointer.- Parameters:
v- an unsigned int to be written (long is converted to unsigned short).endianType- endian type. If it is big endian, natural byte order is preserved, otherwise byte order is reversed.- Throws:
IOException- if an I/O error occurs.
-
writeLong
Writes a long to the file as eight bytes, high byte first. The write starts at the current position of the file pointer.- Parameters:
v- a long to be written.- Throws:
IOException- if an I/O error occurs.
-
writeLong
Writes a long to the file as eight bytes, using provided endian type. If endian type is big endian, then natural byte order is preserved (and high byte is written first), if little endian order is chosen, then byte order is reversed. The write starts at the current position of the file pointer.- Parameters:
v- a long to be written.endianType- endian type. If it is big endian, natural byte order is preserved, otherwise byte order is reversed.- Throws:
IOException- if an I/O error occurs.
-
writeFloat
Converts the float argument to an int using the floatToIntBits method in class Float, and then write that int value to the file as a four-byte quantity, high byte first. The write starts at the current position of the file pointer.- Parameters:
v- a float value to be written.- Throws:
IOException- if an I/O error occurs.
-
writeFloat
Converts the float argument to an int using the floatToIntBits method in class Float, and then write that int value to the file as a four-byte quantity, using provided endian type. If endian type is big endian, then natural byte order is preserved (and high byte is written first), if little endian order is chosen, then byte order is reversed. The write starts at the current position of the file pointer.- Parameters:
v- a float value to be written.endianType- endian type. If it is big endian, natural byte order is preserved, otherwise byte order is reversed.- Throws:
IOException- if an I/O error occurs.
-
writeDouble
Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the file as an eight byte quantity, high byte first. The write starts at the current position of the file pointer.- Parameters:
v- a double value to be written.- Throws:
IOException- if an I/O error occurs.
-
writeDouble
Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the file as an eight byte quantity, using provided endian type. If endian type is big endian, then natural byte order is preserved (and high byte is written first), if little endian order is chosen, then byte order is reversed. The write starts at the current position of the file pointer.- Parameters:
v- a double value to be written.endianType- endian type. If it is big endian, natural byte order is preserved, otherwise byte order is reversed.- Throws:
IOException- if an I/O error occurs.
-
writeASCII
Writes the string to the file as a sequence of bytes. Each character in the string is written out, in sequence, by discarding its high eight bits. The write starts at the current position of the file pointer.- Parameters:
s- a string of bytes to be written.- Throws:
IOException- if an I/O error occurs.
-