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
ConstructorDescriptionCreate 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 TypeMethodDescriptionvoid
close()
void
flush()
final FileChannel
Get 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 FileDescriptor
getFD()
Get the file descriptor associated with this stream.final long
Gets the current read/write position in the file.final boolean
hasAvailable
(int need) Checks if there are the required number of bytes available to read from this file.boolean
Checks whether we are currently at the start of this output file or stream.final long
length()
Returns the current length of the file.void
mark
(int readlimit) See the general contract of themark
method ofInputStream
.boolean
See the general contract of themarkSupported
method ofInputStream
.final int
read()
Reads a byte.int
read
(boolean[] b, int start, int length) Read a segment of an array of boolean's.final int
read
(byte[] b) Read as many bytes into a byte array as possible.final int
read
(byte[] b, int from, int len) Reads up to the specified number of bytes into a buffer.int
read
(char[] c, int start, int length) Read a segment of an array of char's.int
read
(double[] d, int start, int length) Read a segment of an array of double's.int
read
(float[] f, int start, int length) Read a segment of an array of float's.int
read
(int[] i, int start, int length) Read a segment of an array of int's.int
read
(long[] l, int start, int length) Read a segment of an array of long's.int
read
(short[] s, int start, int length) Read a segment of an array of short's.int
Reads into an array of booleans, possibly including legalnull
values.final int
Deprecated.boolean
final byte
readByte()
char
readChar()
final double
final float
final void
readFully
(byte[] b) Reads bytes to completely fill the supplied buffer.void
readFully
(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 int
readInt()
readLine()
final long
readLong()
final short
final int
final int
final String
readUTF()
Same asRandomAccessFile.readUTF()
.void
reset()
See the general contract of thereset
method ofInputStream
.final void
seek
(long newPos) Sets a new position in the file for subsequent reading or writing.void
setLength
(long newLength) Sets the length of the file.final long
skip
(long n) Moves the file pointer by a number of bytes from its current position.void
skipAllBytes
(long toSkip) Skips a number of bytes from the input.int
skipBytes
(int toSkip) void
write
(boolean[] b, int start, int length) Write a segment of an array of boolean's.final void
write
(byte[] b) Writes the contents of a byte array into the file.final void
write
(byte[] b, int from, int len) Writes up to the specified number of bytes from a buffer to the stream.void
write
(char[] c, int start, int length) Write a segment of an array of char's.void
write
(double[] d, int start, int length) Write a segment of an array of double's.void
write
(float[] f, int start, int length) Write a segment of an array of float's.final void
write
(int b) Writes a byte.void
write
(int[] i, int start, int length) Write a segment of an array of int's.void
write
(long[] l, int start, int length) Write a segment of an array of longs.void
write
(short[] s, int start, int length) Write a segment of an array of shorts.void
Write a segment of an array of booleans, possibly including legalnull
values.void
Write a segment of an array of Strings.void
writeBoolean
(boolean v) final void
writeByte
(int v) final void
writeBytes
(String s) void
writeChar
(int v) final void
writeChars
(String s) final void
writeDouble
(double v) final void
writeFloat
(float v) final void
writeInt
(int v) final void
writeLong
(long v) final void
writeShort
(int v) final void
Methods inherited from class nom.tam.util.ArrayDataFile
readArrayFully, readImage, readLArray, writeArray
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface nom.tam.util.ArrayDataInput
read, read, read, read, read, read, read, read, read, readArrayFully, readFully, readImage, readLArray, skip, skipAllBytes
Methods inherited from interface nom.tam.util.ArrayDataOutput
flush, write, write, write, write, write, write, write, write, write, writeArray
Methods inherited from interface java.io.DataOutput
write, write, write, writeUTF
Methods inherited from interface nom.tam.util.InputReader
read, read
Methods inherited from interface nom.tam.util.OutputWriter
write, write
Methods inherited from interface nom.tam.util.RandomAccess
getFilePointer, position, position, seek
Methods 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:ArrayDataFile
Returns 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:
getEncoder
in classArrayDataFile
- Returns:
- the conversion from Java arrays to their binary representation in file
- See Also:
-
ArrayDataFile.setEncoder(OutputEncoder)
ArrayDataFile.getDecoder()
-
getDecoder
Description copied from class:ArrayDataFile
Returns 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:
getDecoder
in classArrayDataFile
- Returns:
- the conversion from the binary representation of arrays in the file to Java arrays
- See Also:
-
ArrayDataFile.setDecoder(InputDecoder)
ArrayDataFile.getEncoder()
-
isAtStart
public boolean isAtStart()Description copied from interface:FitsOutput
Checks whether we are currently at the start of this output file or stream.- Specified by:
isAtStart
in interfaceFitsOutput
- Returns:
true
if we are currently at the start of stream (where a primary HDU is to be written), orfalse
if we are further along, where HDUs should be written as extensions.- See Also:
-
readUnsignedByte
- Specified by:
readUnsignedByte
in interfaceDataInput
- Throws:
IOException
-
readByte
- Specified by:
readByte
in interfaceDataInput
- Throws:
IOException
-
readBoolean
- Specified by:
readBoolean
in interfaceDataInput
- Throws:
IOException
-
readChar
- Specified by:
readChar
in interfaceDataInput
- Throws:
IOException
-
readUnsignedShort
- Specified by:
readUnsignedShort
in interfaceDataInput
- Throws:
IOException
-
readShort
- Specified by:
readShort
in interfaceDataInput
- Throws:
IOException
-
readInt
- Specified by:
readInt
in interfaceDataInput
- Throws:
IOException
-
readLong
- Specified by:
readLong
in interfaceDataInput
- Throws:
IOException
-
readFloat
- Specified by:
readFloat
in interfaceDataInput
- Throws:
IOException
-
readDouble
- Specified by:
readDouble
in interfaceDataInput
- Throws:
IOException
-
readLine
- Specified by:
readLine
in interfaceDataInput
- Throws:
IOException
-
read
Description copied from interface:ArrayDataInput
Read a segment of an array of boolean's.- Specified by:
read
in 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:
EOFException
- if already at the end of file.IOException
- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInput
Reads into an array of booleans, possibly including legalnull
values. 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 fornull
values.- Specified by:
read
in 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:
EOFException
- if already at the end of file.IOException
- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInput
Read a segment of an array of char's.- Specified by:
read
in 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:
EOFException
- if already at the end of file.IOException
- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInput
Read a segment of an array of short's.- Specified by:
read
in 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:
EOFException
- if already at the end of file.IOException
- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInput
Read a segment of an array of int's.- Specified by:
read
in 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:
EOFException
- if already at the end of file.IOException
- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInput
Read a segment of an array of long's.- Specified by:
read
in 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:
EOFException
- if already at the end of file.IOException
- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInput
Read a segment of an array of float's.- Specified by:
read
in 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:
EOFException
- if already at the end of file.IOException
- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInput
Read a segment of an array of double's.- Specified by:
read
in 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:
EOFException
- if already at the end of file.IOException
- if one of the underlying read operations failed
-
readArray
Deprecated.- Specified by:
readArray
in 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:
EOFException
- if already at the end of file.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:ArrayDataInput
See the general contract of themarkSupported
method ofInputStream
.- Specified by:
markSupported
in interfaceArrayDataInput
- Returns:
- true if this stream instance supports the mark and reset methods; false otherwise.
-
mark
Description copied from interface:ArrayDataInput
See the general contract of themark
method ofInputStream
.- Specified by:
mark
in 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:ArrayDataInput
See the general contract of thereset
method ofInputStream
.If
markpos
is-1
(no mark has been set or the mark has been invalidated), anIOException
is thrown. Otherwise,pos
is set equal tomarkpos
.- Specified by:
reset
in 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:
skipBytes
in interfaceDataInput
- Throws:
IOException
-
skipAllBytes
Description copied from interface:ArrayDataInput
Skips 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:
skipAllBytes
in 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:
writeByte
in interfaceDataOutput
- Throws:
IOException
-
writeBoolean
- Specified by:
writeBoolean
in interfaceDataOutput
- Throws:
IOException
-
writeChar
- Specified by:
writeChar
in interfaceDataOutput
- Throws:
IOException
-
writeShort
- Specified by:
writeShort
in interfaceDataOutput
- Throws:
IOException
-
writeInt
- Specified by:
writeInt
in interfaceDataOutput
- Throws:
IOException
-
writeLong
- Specified by:
writeLong
in interfaceDataOutput
- Throws:
IOException
-
writeFloat
- Specified by:
writeFloat
in interfaceDataOutput
- Throws:
IOException
-
writeDouble
- Specified by:
writeDouble
in interfaceDataOutput
- Throws:
IOException
-
writeBytes
- Specified by:
writeBytes
in interfaceDataOutput
- Throws:
IOException
-
writeChars
- Specified by:
writeChars
in interfaceDataOutput
- Throws:
IOException
-
write
Description copied from interface:ArrayDataOutput
Write a segment of an array of boolean's.- Specified by:
write
in 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:ArrayDataOutput
Write a segment of an array of booleans, possibly including legalnull
values. 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:
write
in 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:ArrayDataOutput
Write a segment of an array of char's.- Specified by:
write
in 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:ArrayDataOutput
Write a segment of an array of shorts.- Specified by:
write
in 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:ArrayDataOutput
Write a segment of an array of int's.- Specified by:
write
in 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:ArrayDataOutput
Write a segment of an array of longs.- Specified by:
write
in 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:ArrayDataOutput
Write a segment of an array of float's.- Specified by:
write
in 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:ArrayDataOutput
Write a segment of an array of double's.- Specified by:
write
in 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:ArrayDataOutput
Write a segment of an array of Strings. Equivalent to calling writeBytes for the selected elements.- Specified by:
write
in 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:
true
if 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:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
flush
- Specified by:
flush
in interfaceFlushable
- Throws:
IOException
-
write
Description copied from interface:OutputWriter
Writes a byte. See the general contract ofDataOutputStream.write(int)
.- Specified by:
write
in 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:InputReader
Reads a byte. See the general contract ofFilterInputStream.read()
.- Specified by:
read
in 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:OutputWriter
Writes up to the specified number of bytes from a buffer to the stream. See the general contract ofDataOutputStream.write(byte[], int, int)
.- Specified by:
write
in 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:InputReader
Reads up to the specified number of bytes into a buffer. See the general contract ofDataInputStream.read(byte[], int, int)
.- Specified by:
read
in 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, anEOFException
will 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, anEOFException
will 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...
-