Class FitsFile
- All Implemented Interfaces:
Closeable,DataInput,DataOutput,Flushable,AutoCloseable,ArrayDataInput,ArrayDataOutput,FitsIO,FitsOutput,InputReader,OutputWriter,RandomAccess,ReadWriteAccess
- Direct Known Subclasses:
BufferedFile
Note that although this class supports most of the contract of RandomAccessFile it does not (and can not) extend that class since many of the methods of RandomAccessFile are final. In practice this method works much like the StreamFilter classes. All methods are implemented in this class but some are simply delegated to an underlying RandomAccessFile member.
Testing and timing routines are available in the nom.tam.util.test.BufferedFileTester class.
Prior versions of this class under the old BufferedFile name:
- Version 1.1 -- October 12, 2000: Fixed handling of EOF in array reads so that a partial array will be returned when an EOF is detected. Excess bytes that cannot be used to construct array elements will be discarded (e.g., if there are 2 bytes left and the user is reading an int array).
- Version 1.2 -- December 8, 2002: Added getChannel method.
- Version 1.3 -- March 2, 2007: Added File based constructors.
- Version 1.4 -- July 20, 2009: Added support for >2G Object reads. This is still a bit problematic in that we do not support primitive arrays larger than 2 GB/atomsize. However except in the case of bytes this is not currently a major issue.
Version 2.0 -- Oct 30, 2021: New hierarchy for more digestible code. Improved buffering, and renamed from
BufferedFile to the more appropriate name of FitsFile. Performance is 2-4 times better than
before.
- Since:
- 1.16
- See Also:
-
Field Summary
Fields inherited from interface nom.tam.util.FitsIO
BITS_OF_1_BYTE, BITS_OF_2_BYTES, BITS_OF_3_BYTES, BITS_OF_4_BYTES, BITS_OF_5_BYTES, BITS_OF_6_BYTES, BITS_OF_7_BYTES, BYTE_1_OF_LONG_MASK, BYTE_2_OF_LONG_MASK, BYTE_3_OF_LONG_MASK, BYTE_4_OF_LONG_MASK, BYTE_MASK, BYTES_IN_BOOLEAN, BYTES_IN_BYTE, BYTES_IN_CHAR, BYTES_IN_DOUBLE, BYTES_IN_FLOAT, BYTES_IN_INTEGER, BYTES_IN_LONG, BYTES_IN_SHORT, DEFAULT_BUFFER_SIZE, HIGH_INTEGER_MASK, INTEGER_MASK, SHORT_MASK, SHORT_OF_LONG_MASK -
Constructor Summary
ConstructorsConstructorDescriptionCreate a buffered file from a File descriptorCreate a buffered file from a File descriptorCreate a buffered file from a file descriptorCreate a read-only buffered fileCreate a buffered file with the given mode.Create a buffered file with the given mode and a specified dataBuffer.buffer size.FitsFile(RandomAccessFileIO src, int bufferSize) Create a buffered file from a random access data object. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voidflush()final FileChannelGet the channel associated with this file.Returns the conversion from the binary representation of arrays in file to Java arrays.Returns the conversion from Java arrays to their binary representation in file.final FileDescriptorgetFD()Get the file descriptor associated with this stream.final longGets the current read/write position in the file.final booleanhasAvailable(int need) Checks if there are the required number of bytes available to read from this file.booleanChecks whether we are currently at the start of this output file or stream.final longlength()Returns the current length of the file.voidmark(int readlimit) See the general contract of themarkmethod ofInputStream.booleanSee the general contract of themarkSupportedmethod ofInputStream.final intread()Reads a byte.intread(boolean[] b, int start, int length) Read a segment of an array of boolean's.final intread(byte[] b) Read as many bytes into a byte array as possible.final intread(byte[] b, int from, int len) Reads up to the specified number of bytes into a buffer.intread(char[] c, int start, int length) Read a segment of an array of char's.intread(double[] d, int start, int length) Read a segment of an array of double's.intread(float[] f, int start, int length) Read a segment of an array of float's.intread(int[] i, int start, int length) Read a segment of an array of int's.intread(long[] l, int start, int length) Read a segment of an array of long's.intread(short[] s, int start, int length) Read a segment of an array of short's.intReads into an array of booleans, possibly including legalnullvalues.final intDeprecated.booleanfinal bytereadByte()charreadChar()final doublefinal floatfinal voidreadFully(byte[] b) Reads bytes to completely fill the supplied buffer.voidreadFully(byte[] b, int off, int len) Reads bytes to fill the supplied buffer with the requested number of bytes from the given starting buffer index.final intreadInt()readLine()final longreadLong()final shortfinal intfinal intfinal StringreadUTF()Same asRandomAccessFile.readUTF().voidreset()See the general contract of theresetmethod ofInputStream.final voidseek(long newPos) Sets a new position in the file for subsequent reading or writing.voidsetLength(long newLength) Sets the length of the file.final longskip(long n) Moves the file pointer by a number of bytes from its current position.voidskipAllBytes(long toSkip) Skips a number of bytes from the input.intskipBytes(int toSkip) voidwrite(boolean[] b, int start, int length) Write a segment of an array of boolean's.final voidwrite(byte[] b) Writes the contents of a byte array into the file.final voidwrite(byte[] b, int from, int len) Writes up to the specified number of bytes from a buffer to the stream.voidwrite(char[] c, int start, int length) Write a segment of an array of char's.voidwrite(double[] d, int start, int length) Write a segment of an array of double's.voidwrite(float[] f, int start, int length) Write a segment of an array of float's.final voidwrite(int b) Writes a byte.voidwrite(int[] i, int start, int length) Write a segment of an array of int's.voidwrite(long[] l, int start, int length) Write a segment of an array of longs.voidwrite(short[] s, int start, int length) Write a segment of an array of shorts.voidWrite a segment of an array of booleans, possibly including legalnullvalues.voidWrite a segment of an array of Strings.voidwriteBoolean(boolean v) final voidwriteByte(int v) final voidwriteBytes(String s) voidwriteChar(int v) final voidwriteChars(String s) final voidwriteDouble(double v) final voidwriteFloat(float v) final voidwriteInt(int v) final voidwriteLong(long v) final voidwriteShort(int v) final voidMethods inherited from class nom.tam.util.ArrayDataFile
readArrayFully, readImage, readLArray, writeArrayMethods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface nom.tam.util.ArrayDataInput
read, read, read, read, read, read, read, read, read, readArrayFully, readFully, readImage, readLArray, skip, skipAllBytesMethods inherited from interface nom.tam.util.ArrayDataOutput
flush, write, write, write, write, write, write, write, write, write, writeArrayMethods inherited from interface java.io.DataOutput
write, write, write, writeUTFMethods inherited from interface nom.tam.util.InputReader
read, readMethods inherited from interface nom.tam.util.OutputWriter
write, writeMethods inherited from interface nom.tam.util.RandomAccess
getFilePointer, position, position, seekMethods inherited from interface nom.tam.util.ReadWriteAccess
length
-
Constructor Details
-
FitsFile
Create a buffered file from a File descriptor- Parameters:
file- the file to open.- Throws:
IOException- if the file could not be opened
-
FitsFile
Create a buffered file from a File descriptor- Parameters:
file- the file to open.mode- the mode to open the file in- Throws:
IOException- if the file could not be opened
-
FitsFile
Create a buffered file from a file descriptor- Parameters:
file- the file to open.mode- the mode to open the file inbufferSize- the dataBuffer.buffer size to use- Throws:
IOException- if the file could not be opened
-
FitsFile
Create a buffered file from a random access data object.- Parameters:
src- random access databufferSize- the dataBuffer's buffer size to use- Throws:
IOException- if data could not be read
-
FitsFile
Create a read-only buffered file- Parameters:
filename- the name of the file to open- Throws:
IOException- if the file could not be opened
-
FitsFile
Create a buffered file with the given mode.- Parameters:
filename- The file to be accessed.mode- A string composed of "r" and "w" for read and write access.- Throws:
IOException- if the file could not be opened
-
FitsFile
Create a buffered file with the given mode and a specified dataBuffer.buffer size.- Parameters:
filename- The file to be accessed.mode- A string composed of "r" and "w" indicating read or write access.bufferSize- The dataBuffer.buffer size to be used. This should be substantially larger than 100 bytes and defaults to 32768 bytes in the other constructors.- Throws:
IOException- if the file could not be opened
-
-
Method Details
-
getEncoder
Description copied from class:ArrayDataFileReturns the conversion from Java arrays to their binary representation in file. Subclass implementations can use this to access the required conversion when writing data to file.- Overrides:
getEncoderin classArrayDataFile- Returns:
- the conversion from Java arrays to their binary representation in file
- See Also:
-
getDecoder
Description copied from class:ArrayDataFileReturns the conversion from the binary representation of arrays in file to Java arrays. Subclass implementeations can use this to access the required conversion when writing data to file.- Overrides:
getDecoderin classArrayDataFile- Returns:
- the conversion from the binary representation of arrays in the file to Java arrays
- See Also:
-
isAtStart
public boolean isAtStart()Description copied from interface:FitsOutputChecks whether we are currently at the start of this output file or stream.- Specified by:
isAtStartin interfaceFitsOutput- Returns:
trueif we are currently at the start of stream (where a primary HDU is to be written), orfalseif we are further along, where HDUs should be written as extensions.- See Also:
-
readUnsignedByte
- Specified by:
readUnsignedBytein interfaceDataInput- Throws:
IOException
-
readByte
- Specified by:
readBytein interfaceDataInput- Throws:
IOException
-
readBoolean
- Specified by:
readBooleanin interfaceDataInput- Throws:
IOException
-
readChar
- Specified by:
readCharin interfaceDataInput- Throws:
IOException
-
readUnsignedShort
- Specified by:
readUnsignedShortin interfaceDataInput- Throws:
IOException
-
readShort
- Specified by:
readShortin interfaceDataInput- Throws:
IOException
-
readInt
- Specified by:
readIntin interfaceDataInput- Throws:
IOException
-
readLong
- Specified by:
readLongin interfaceDataInput- Throws:
IOException
-
readFloat
- Specified by:
readFloatin interfaceDataInput- Throws:
IOException
-
readDouble
- Specified by:
readDoublein interfaceDataInput- Throws:
IOException
-
readLine
- Specified by:
readLinein interfaceDataInput- Throws:
IOException
-
read
Description copied from interface:ArrayDataInputRead a segment of an array of boolean's.- Specified by:
readin interfaceArrayDataInput- Parameters:
b- array of boolean's.start- start index in the arraylength- number of array elements to read- Returns:
- number of bytes read.
- Throws:
IOException- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInputReads into an array of booleans, possibly including legalnullvalues. The method has a default implementation, callsDataInput.readBoolean()element by element. Classes that implement this interface might want to replace that with a more efficient block read implementation and/or to add the desired translation fornullvalues.- Specified by:
readin interfaceArrayDataInput- Parameters:
buf- array of boolean's.offset- start index in the arraysize- number of array elements to read- Returns:
- number of bytes read.
- Throws:
IOException- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInputRead a segment of an array of char's.- Specified by:
readin interfaceArrayDataInput- Parameters:
c- array of char's.start- start index in the arraylength- number of array elements to read- Returns:
- number of bytes read.
- Throws:
IOException- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInputRead a segment of an array of short's.- Specified by:
readin interfaceArrayDataInput- Parameters:
s- array of short's.start- start index in the arraylength- number of array elements to read- Returns:
- number of bytes read.
- Throws:
IOException- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInputRead a segment of an array of int's.- Specified by:
readin interfaceArrayDataInput- Parameters:
i- array of int's.start- start index in the arraylength- number of array elements to read- Returns:
- number of bytes read.
- Throws:
IOException- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInputRead a segment of an array of long's.- Specified by:
readin interfaceArrayDataInput- Parameters:
l- array of long's.start- start index in the arraylength- number of array elements to read- Returns:
- number of bytes read.
- Throws:
IOException- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInputRead a segment of an array of float's.- Specified by:
readin interfaceArrayDataInput- Parameters:
f- array of float's.start- start index in the arraylength- number of array elements to read- Returns:
- number of bytes read.
- Throws:
IOException- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInputRead a segment of an array of double's.- Specified by:
readin interfaceArrayDataInput- Parameters:
d- array of double's.start- start index in the arraylength- number of array elements to read- Returns:
- number of bytes read, or -1 if at the end of file/stream
- Throws:
IOException- if one of the underlying read operations failed
-
readArray
Deprecated.- Specified by:
readArrayin interfaceArrayDataInput- Parameters:
o- a Java array object, including heterogeneous arrays of arrays. Ifnull, nothing will be read from the output.- Returns:
- the number of bytes read from the input.
- Throws:
IOException- if there was an IO error, other than the end-of-file, while reading from the input
-
markSupported
public boolean markSupported()Description copied from interface:ArrayDataInputSee the general contract of themarkSupportedmethod ofInputStream.- Specified by:
markSupportedin interfaceArrayDataInput- Returns:
- true if this stream instance supports the mark and reset methods; false otherwise.
-
mark
Description copied from interface:ArrayDataInputSee the general contract of themarkmethod ofInputStream.- Specified by:
markin interfaceArrayDataInput- Parameters:
readlimit- the maximum limit of bytes that can be read before the mark position becomes invalid.- Throws:
IOException- if the operation failed- See Also:
-
reset
Description copied from interface:ArrayDataInputSee the general contract of theresetmethod ofInputStream.If
markposis-1(no mark has been set or the mark has been invalidated), anIOExceptionis thrown. Otherwise,posis set equal tomarkpos.- Specified by:
resetin interfaceArrayDataInput- Throws:
IOException- if this stream has not been marked or, if the mark has been invalidated, or the stream has been closed by invoking itsFitsIO.close()method, or an I/O error occurs.- See Also:
-
skipBytes
- Specified by:
skipBytesin interfaceDataInput- Throws:
IOException
-
skipAllBytes
Description copied from interface:ArrayDataInputSkips a number of bytes from the input. This differs from theArrayDataInput.skip(long)method in that it will throw an EOF if the skip cannot be fully accomplished as requested...- Specified by:
skipAllBytesin interfaceArrayDataInput- Parameters:
toSkip- the number of bytes to skip forward. Subclass implementations may support negative valued arguments for a backward skip also.- Throws:
EOFException- if the end (or beginning) of the stream was reached before skipping the required number of bytes. This does not happen typically with forward skips on random access files, where positioning beyond the EOF is generally allowed for writing.IOException- if there was an underlying IO failure.- See Also:
-
writeByte
- Specified by:
writeBytein interfaceDataOutput- Throws:
IOException
-
writeBoolean
- Specified by:
writeBooleanin interfaceDataOutput- Throws:
IOException
-
writeChar
- Specified by:
writeCharin interfaceDataOutput- Throws:
IOException
-
writeShort
- Specified by:
writeShortin interfaceDataOutput- Throws:
IOException
-
writeInt
- Specified by:
writeIntin interfaceDataOutput- Throws:
IOException
-
writeLong
- Specified by:
writeLongin interfaceDataOutput- Throws:
IOException
-
writeFloat
- Specified by:
writeFloatin interfaceDataOutput- Throws:
IOException
-
writeDouble
- Specified by:
writeDoublein interfaceDataOutput- Throws:
IOException
-
writeBytes
- Specified by:
writeBytesin interfaceDataOutput- Throws:
IOException
-
writeChars
- Specified by:
writeCharsin interfaceDataOutput- Throws:
IOException
-
write
Description copied from interface:ArrayDataOutputWrite a segment of an array of boolean's.- Specified by:
writein interfaceArrayDataOutput- Parameters:
b- array of boolean's.start- start index in the arraylength- number of array elements to write- Throws:
IOException- if one of the underlying write operations failed
-
write
Description copied from interface:ArrayDataOutputWrite a segment of an array of booleans, possibly including legalnullvalues. The method has a default implementation, which callsDataOutput.writeBoolean(boolean)element by element. Classes that implement this interface might want to replace that with a more efficient block read implementation/- Specified by:
writein interfaceArrayDataOutput- Parameters:
buf- array of booleans.offset- start index in the arraysize- number of array elements to write- Throws:
IOException- if one of the underlying write operations failed
-
write
Description copied from interface:ArrayDataOutputWrite a segment of an array of char's.- Specified by:
writein interfaceArrayDataOutput- Parameters:
c- array of char's.start- start index in the arraylength- number of array elements to write- Throws:
IOException- if one of the underlying write operations failed
-
write
Description copied from interface:ArrayDataOutputWrite a segment of an array of shorts.- Specified by:
writein interfaceArrayDataOutput- Parameters:
s- the value to writestart- start index in the arraylength- number of array elements to write- Throws:
IOException- if one of the underlying write operations failed
-
write
Description copied from interface:ArrayDataOutputWrite a segment of an array of int's.- Specified by:
writein interfaceArrayDataOutput- Parameters:
i- array of int'sstart- start index in the arraylength- number of array elements to write- Throws:
IOException- if one of the underlying write operations failed
-
write
Description copied from interface:ArrayDataOutputWrite a segment of an array of longs.- Specified by:
writein interfaceArrayDataOutput- Parameters:
l- array of longsstart- start index in the arraylength- number of array elements to write- Throws:
IOException- if one of the underlying write operations failed
-
write
Description copied from interface:ArrayDataOutputWrite a segment of an array of float's.- Specified by:
writein interfaceArrayDataOutput- Parameters:
f- array of float's.start- start index in the arraylength- number of array elements to write- Throws:
IOException- if one of the underlying write operations failed
-
write
Description copied from interface:ArrayDataOutputWrite a segment of an array of double's.- Specified by:
writein interfaceArrayDataOutput- Parameters:
d- array of double's.start- start index in the arraylength- number of array elements to write- Throws:
IOException- if one of the underlying write operations failed
-
write
Description copied from interface:ArrayDataOutputWrite a segment of an array of Strings. Equivalent to calling writeBytes for the selected elements.- Specified by:
writein interfaceArrayDataOutput- Parameters:
s- the array to writestart- start index in the arraylength- number of array elements to write- Throws:
IOException- if one of the underlying write operations failed
-
seek
Sets a new position in the file for subsequent reading or writing.- Parameters:
newPos- the new byte offset from the beginning of the file. It may be beyond the current end of the file, for example for writing more data after some 'gap'.- Throws:
IOException- if the position is negative or cannot be set.
-
getChannel
Get the channel associated with this file. Note that this returns the channel of the associated RandomAccessFile. Note that since the BufferedFile buffers the I/O's to the underlying file, the offset of the channel may be different from the offset of the BufferedFile. This is different for a RandomAccessFile where the offsets are guaranteed to be the same.- Returns:
- the file channel
-
getFD
Get the file descriptor associated with this stream. Note that this returns the file descriptor of the associated RandomAccessFile.- Returns:
- the file descriptor
- Throws:
IOException- if the descriptor could not be accessed.
-
getFilePointer
public final long getFilePointer()Gets the current read/write position in the file.- Returns:
- the current byte offset from the beginning of the file.
-
hasAvailable
Checks if there are the required number of bytes available to read from this file.- Parameters:
need- the number of bytes we need.- Returns:
trueif the needed number of bytes can be read from the file or elsefalse.- Throws:
IOException- if there was an IO error accessing the file.
-
length
Returns the current length of the file.- Returns:
- the current length of the file.
- Throws:
IOException- if the operation failed
-
setLength
Sets the length of the file. This method calls the method of the same name inRandomAccessFileIO.- Parameters:
newLength- The number of bytes at which the file is set.- Throws:
IOException- if the resizing of the underlying stream fails
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
flush
- Specified by:
flushin interfaceFlushable- Throws:
IOException
-
write
Description copied from interface:OutputWriterWrites a byte. See the general contract ofDataOutputStream.write(int).- Specified by:
writein interfaceOutputWriter- Parameters:
b- the (unsigned) byte value to write.- Throws:
IOException- if there was an underlying IO error- See Also:
-
read
Description copied from interface:InputReaderReads a byte. See the general contract ofFilterInputStream.read().- Specified by:
readin interfaceInputReader- Returns:
- the (unsigned) byte value or -1 if there is nothing left to read.
- Throws:
IOException- if there was an underlying IO error- See Also:
-
write
Description copied from interface:OutputWriterWrites up to the specified number of bytes from a buffer to the stream. See the general contract ofDataOutputStream.write(byte[], int, int).- Specified by:
writein interfaceOutputWriter- Parameters:
b- the bufferfrom- the starting buffer indexlen- the number of bytes to write.- Throws:
IOException- if there was an underlying IO error- See Also:
-
read
Description copied from interface:InputReaderReads up to the specified number of bytes into a buffer. See the general contract ofDataInputStream.read(byte[], int, int).- Specified by:
readin interfaceInputReader- Parameters:
b- the bufferfrom- the starting buffer indexlen- the number of bytes to read.- Returns:
- the number of bytes actually read, or -1 if there is nothing left to read.
- Throws:
IOException- if there was an underlying IO error- See Also:
-
readFully
Reads bytes to completely fill the supplied buffer. If not enough bytes are avaialable in the file to fully fill the buffer, anEOFExceptionwill be thrown.- Parameters:
b- the buffer- Throws:
EOFException- if already at the end of file.IOException- if there was an IO error before the buffer could be fully populated.
-
readFully
Reads bytes to fill the supplied buffer with the requested number of bytes from the given starting buffer index. If not enough bytes are avaialable in the file to deliver the reqauested number of bytes the buffer, anEOFExceptionwill be thrown.- Parameters:
b- the bufferoff- the buffer index at which to start reading datalen- the total number of bytes to read.- Throws:
EOFException- if already at the end of file.IOException- if there was an IO error before the requested number of bytes could all be read.
-
readUTF
Same asRandomAccessFile.readUTF().- Returns:
- a string
- Throws:
IOException- if there was an IO error while reading from the file.
-
writeUTF
- Parameters:
s- a string- Throws:
IOException- if there was an IO error while writing to the file.
-
skip
Moves the file pointer by a number of bytes from its current position.- Parameters:
n- the number of byter to move. Negative values are allowed and result in moving the pointer backward.- Returns:
- the actual number of bytes that the pointer moved, which may be fewer than requested if the file boundary was reached.
- Throws:
IOException- if there was an IO error.
-
read
Read as many bytes into a byte array as possible. The number of bytes read may be fewer than the size of the array, for example because the end-of-file is reached during the read.- Parameters:
b- the byte buffer to fill with data from the file.- Returns:
- the number of bytes actually read.
- Throws:
IOException- if there was an IO error while reading, other than the end-of-file.
-
write
Writes the contents of a byte array into the file.- Parameters:
b- the byte buffer to write into the file.- Throws:
IOException- if there was an IO error while writing to the file...
-