Class BufferedFile
java.lang.Object
nom.tam.util.BufferedFile
- All Implemented Interfaces:
Closeable, DataInput, DataOutput, AutoCloseable, ArrayDataInput, ArrayDataOutput, FitsIO, RandomAccess
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intmarker position in the dataBuffer.buffer.private final BufferPointerprivate final BufferDecoderprivate final BufferEncoderprivate static final intprivate booleanIs the dataBuffer.buffer being used for input or outputprivate longThe offset of the beginning of the current dataBuffer.bufferprivate static final Loggerprivate final RandomAccessFileThe underlying access to the file systemFields inherited from interface 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, HIGH_INTEGER_MASK, INTEGER_MASK, SHORT_MASK, SHORT_OF_LONG_MASK -
Constructor Summary
ConstructorsConstructorDescriptionBufferedFile(File file) Create a buffered file from a File descriptorBufferedFile(File file, String mode) Create a buffered file from a File descriptorBufferedFile(File file, String mode, int bufferSize) Create a buffered file from a file descriptorBufferedFile(String filename) Create a read-only buffered fileBufferedFile(String filename, String mode) Create a buffered file with the given mode.BufferedFile(String filename, String mode, int bufferSize) Create a buffered file with the given mode and a specified dataBuffer.buffer size. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidcheckBuffer(int needBytes) This should only be used when a small number of bytes is required (substantially smaller than bufferSize.voidclose()protected voidfinalize()voidflush()Flush the output bufferGet the channel associated with this file.getFD()Get the file descriptor associated with this stream.longGet the current offset into the file.longlength()voidmark(int readlimit) See the general contract of themarkmethod ofInputStream.private voidneedBuffer(int need) intread()intread(boolean[] b) Read an array of boolean's.intread(boolean[] b, int start, int length) Read a segment of an array of boolean's.intread(byte[] buf) Read an array of byte's.intread(byte[] buf, int offset, int len) Read a segment of an array of byte's.intread(char[] c) Read an array of char's.intread(char[] c, int start, int length) Read a segment of an array of char's.intread(double[] d) Read an array of double's.intread(double[] d, int start, int length) Read a segment of an array of double's.intread(float[] f) Read an array of float's.intread(float[] f, int start, int length) Read a segment of an array of float's.intread(int[] i) Read an array of int's.intread(int[] i, int start, int length) Read a segment of an array of int's.intread(long[] l) Read a segment of an array of long's.intread(long[] l, int start, int length) Read a segment of an array of long's.intread(short[] s) Read an array of short's.intread(short[] s, int start, int length) Read a segment of an array of short's.intDeprecated.booleanbytereadByte()charreadChar()doublefloatvoidreadFully(byte[] b) voidreadFully(byte[] b, int off, int len) Read a buffer and signal an EOF if the requested elements cannot be read.intreadInt()longreadLArray(Object o) Read an object.readLine()Read a line of input.longreadLong()shortintintreadUTF()voidreset()See the general contract of theresetmethod ofInputStream.voidseek(long offsetFromStart) Move to a specified location in the stream.voidsetLength(long newLength) Set the length of the file.longskip(long offset) Skip the number of bytes.voidskipAllBytes(int toSkip) Skip the number of bytes.voidskipAllBytes(long toSkip) Skip the number of bytes.intskipBytes(int n) voidwrite(boolean[] b) Write an array of boolean's.voidwrite(boolean[] b, int start, int length) Write a segment of an array of boolean's.voidwrite(byte[] buf) voidwrite(byte[] buf, int offset, int length) voidwrite(char[] c) Write an array of char's.voidwrite(char[] c, int start, int length) Write a segment of an array of char's.voidwrite(double[] d) Write an array of double's.voidwrite(double[] d, int start, int length) Write a segment of an array of double's.voidwrite(float[] f) Write an array of float's.voidwrite(float[] f, int start, int length) Write a segment of an array of float's.voidwrite(int buf) voidwrite(int[] i) Write an array of int's.voidwrite(int[] i, int start, int length) Write a segment of an array of int's.voidwrite(long[] l) Write an array of longs.voidwrite(long[] l, int start, int length) Write a segment of an array of longs.voidwrite(short[] s) Write an array of shorts.voidwrite(short[] s, int start, int length) Write a segment of an array of shorts.voidWrite an array of Strings.voidWrite a segment of an array of Strings.voidwriteArray(Object o) This routine provides efficient writing of arrays of any primitive type.voidwriteBoolean(boolean b) voidwriteByte(int b) voidwriteBytes(String s) voidwriteChar(int c) voidwriteChars(String s) voidwriteDouble(double d) voidwriteFloat(float f) voidwriteInt(int i) voidwriteLong(long l) voidwriteShort(int s) void
-
Field Details
-
DEFAULT_BUFFER_SIZE
private static final int DEFAULT_BUFFER_SIZE- See Also:
-
LOG
-
bufferPointer
-
dataDecoder
-
dataEncoder
-
randomAccessFile
The underlying access to the file system -
fileOffset
private long fileOffsetThe offset of the beginning of the current dataBuffer.buffer -
doingInput
private boolean doingInputIs the dataBuffer.buffer being used for input or output -
bufferMarker
private int bufferMarkermarker position in the dataBuffer.buffer.
-
-
Constructor Details
-
BufferedFile
Create a buffered file from a File descriptor- Parameters:
file- the file to open.- Throws:
IOException- if the file could not be opened
-
BufferedFile
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
-
BufferedFile
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
-
BufferedFile
Create a read-only buffered file- Parameters:
filename- the name of the file to open- Throws:
IOException- if the file could not be opened
-
BufferedFile
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
-
BufferedFile
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
-
checkBuffer
This should only be used when a small number of bytes is required (substantially smaller than bufferSize.- Parameters:
needBytes- the number of bytes needed for the next read operation.- Throws:
IOException- if the dataBuffer.buffer could not be filled
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceFitsIO- Throws:
IOException
-
finalize
-
flush
Description copied from interface:ArrayDataOutputFlush the output buffer- Specified by:
flushin interfaceArrayDataOutput- Throws:
IOException- if the flush of the underlying stream failed
-
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 than the offset of the BufferedFile. This is different than 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 long getFilePointer()Get the current offset into the file.- Specified by:
getFilePointerin interfaceRandomAccess- Returns:
- the current position in the stream.
-
length
- Returns:
- the current length of the file.
- Throws:
IOException- if the operation failed
-
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:
-
needBuffer
- Throws:
IOException
-
read
- Returns:
- Read a byte.
- Throws:
IOException- if the underlying read operation fails
-
read
Description copied from interface:ArrayDataInputRead an array of boolean's.- Specified by:
readin interfaceArrayDataInput- Parameters:
b- array of boolean's.- 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 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:ArrayDataInputRead an array of byte's.- Specified by:
readin interfaceArrayDataInput- Parameters:
buf- array of byte's.- Returns:
- number of bytes read.
- Throws:
IOException- if one of the underlying read operations failed- See Also:
-
read
Description copied from interface:ArrayDataInputRead a segment of an array of byte's.- Specified by:
readin interfaceArrayDataInput- Parameters:
buf- array of byte's.offset- start index in the arraylen- number of array elements to read- Returns:
- number of bytes read.
- Throws:
IOException- if one of the underlying read operations failed- See Also:
-
read
Description copied from interface:ArrayDataInputRead an array of char's.- Specified by:
readin interfaceArrayDataInput- Parameters:
c- array of char's.- 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 an array of double's.- Specified by:
readin interfaceArrayDataInput- Parameters:
d- array of double's.- 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.
- Throws:
IOException- if one of the underlying read operations failed
-
read
Description copied from interface:ArrayDataInputRead an array of float's.- Specified by:
readin interfaceArrayDataInput- Parameters:
f- array of float's.- 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 an array of int's.- Specified by:
readin interfaceArrayDataInput- Parameters:
i- array of int's.- 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.- 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 an array of short's.- Specified by:
readin interfaceArrayDataInput- Parameters:
s- array of short's.- 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
-
readArray
Deprecated.Description copied from interface:ArrayDataInputRead a generic (possibly multidimensional) primitive array. An Object[] array is also a legal argument if each element of the array is a legal.The ArrayDataInput classes do not support String input since it is unclear how one would read in an Array of strings.
- Specified by:
readArrayin interfaceArrayDataInput- Parameters:
o- A [multidimensional] primitive (or Object) array.- Returns:
- number of bytes read.
- Throws:
IOException- if the underlying stream failed
-
readBoolean
- Specified by:
readBooleanin interfaceDataInput- Throws:
IOException
-
readByte
- Specified by:
readBytein interfaceDataInput- Throws:
IOException
-
readChar
- Specified by:
readCharin interfaceDataInput- Throws:
IOException
-
readDouble
- Specified by:
readDoublein interfaceDataInput- Throws:
IOException
-
readFloat
- Specified by:
readFloatin interfaceDataInput- Throws:
IOException
-
readFully
- Specified by:
readFullyin interfaceDataInput- Throws:
IOException
-
readFully
Description copied from interface:ArrayDataInputRead a buffer and signal an EOF if the requested elements cannot be read. This differs from read(b,off,len) since that call will not signal and end of file unless no bytes can be read. However both of these routines will attempt to fill their buffers completely.- Specified by:
readFullyin interfaceArrayDataInput- Specified by:
readFullyin interfaceDataInput- Parameters:
b- The input buffer.off- The requested offset into the buffer.len- The number of bytes requested.- Throws:
IOException
-
readInt
- Specified by:
readIntin interfaceDataInput- Throws:
IOException
-
readLArray
Description copied from interface:ArrayDataInputRead an object. An EOF will be signaled if the object cannot be fully read. This version works even if the underlying data is more than 2 Gigabytes.- Specified by:
readLArrayin interfaceArrayDataInput- Parameters:
o- The object to be read. This object should be a primitive (possibly multi-dimensional) array.- Returns:
- number of bytes read.
- Throws:
IOException- if the underlying stream failed
-
readLine
Read a line of input.- Specified by:
readLinein interfaceDataInput- Returns:
- the next line.
- Throws:
IOException
-
readLong
- Specified by:
readLongin interfaceDataInput- Throws:
IOException
-
readShort
- Specified by:
readShortin interfaceDataInput- Throws:
IOException
-
readUnsignedByte
- Specified by:
readUnsignedBytein interfaceDataInput- Throws:
IOException
-
readUnsignedShort
- Specified by:
readUnsignedShortin interfaceDataInput- Throws:
IOException
-
readUTF
- Specified by:
readUTFin interfaceDataInput- Throws:
IOException
-
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:
-
seek
Description copied from interface:RandomAccessMove to a specified location in the stream.- Specified by:
seekin interfaceRandomAccess- Parameters:
offsetFromStart- set the offset messured from the start- Throws:
IOException- if the operation fails
-
setLength
Set the length of the file. This method calls the method of the same name in RandomAccessFile which is only available in JDK1.2 and greater. This method may be deleted for compilation with earlier versions.- Parameters:
newLength- The number of bytes at which the file is set.- Throws:
IOException- if the resizing of the underlying stream fails
-
skip
Description copied from interface:ArrayDataInputSkip the number of bytes. This differs from the skip method in that it will throw an EOF if a forward skip cannot be fully accomplished... (However that isn't supposed to happen with a random access file, so there is probably no operational difference).- Specified by:
skipin interfaceArrayDataInput- Parameters:
offset- the number of bytes to skip- Returns:
- the number of bytes really skipped
- Throws:
IOException- if the underlying stream failed
-
skipAllBytes
Description copied from interface:ArrayDataInputSkip the number of bytes. This differs from the skip method in that it will throw an EOF if a forward skip cannot be fully accomplished... (However that isn't supposed to happen with a random access file, so there is probably no operational difference).- Specified by:
skipAllBytesin interfaceArrayDataInput- Parameters:
toSkip- the number of bytes to skip- Throws:
IOException- if the underlying stream failed
-
skipAllBytes
Description copied from interface:ArrayDataInputSkip the number of bytes. This differs from the skip method in that it will throw an EOF if a forward skip cannot be fully accomplished... (However that isn't supposed to happen with a random access file, so there is probably no operational difference).- Specified by:
skipAllBytesin interfaceArrayDataInput- Parameters:
toSkip- the number of bytes to skip- Throws:
IOException- if the underlying stream failed
-
skipBytes
- Specified by:
skipBytesin interfaceDataInput- Throws:
IOException
-
write
Description copied from interface:ArrayDataOutputWrite an array of boolean's.- Specified by:
writein interfaceArrayDataOutput- Parameters:
b- array of boolean's.- Throws:
IOException- if one of the underlying write operations failed
-
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
- Specified by:
writein interfaceDataOutput- Throws:
IOException
-
write
- Specified by:
writein interfaceDataOutput- Throws:
IOException
-
write
Description copied from interface:ArrayDataOutputWrite an array of char's.- Specified by:
writein interfaceArrayDataOutput- Parameters:
c- array of char's.- 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 an array of double's.- Specified by:
writein interfaceArrayDataOutput- Parameters:
d- array of double's.- 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 an array of float's.- Specified by:
writein interfaceArrayDataOutput- Parameters:
f- array of float's.- 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
- Specified by:
writein interfaceDataOutput- Throws:
IOException
-
write
Description copied from interface:ArrayDataOutputWrite an array of int's.- Specified by:
writein interfaceArrayDataOutput- Parameters:
i- array of int's- 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 an array of longs.- Specified by:
writein interfaceArrayDataOutput- Parameters:
l- array of longs- 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 an array of shorts.- Specified by:
writein interfaceArrayDataOutput- Parameters:
s- the value 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 an array of Strings. Equivalent to calling writeBytes for the selected elements.- Specified by:
writein interfaceArrayDataOutput- Parameters:
s- the array 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
-
writeArray
Description copied from interface:ArrayDataOutputThis routine provides efficient writing of arrays of any primitive type. The String class is also handled but it is an error to invoke this method with an object that is not an array of these types. If the array is multidimensional, then it calls itself recursively to write the entire array. Strings are written using the standard 1 byte format (i.e., as in writeBytes). If the array is an array of objects, then writePrimitiveArray will be called for each element of the array.- Specified by:
writeArrayin interfaceArrayDataOutput- Parameters:
o- The object to be written. It must be an array of a primitive type, Object, or String.- Throws:
IOException- if one of the underlying write operations failed
-
writeBoolean
- Specified by:
writeBooleanin interfaceDataOutput- Throws:
IOException
-
writeByte
- Specified by:
writeBytein interfaceDataOutput- Throws:
IOException
-
writeBytes
- Specified by:
writeBytesin interfaceDataOutput- Throws:
IOException
-
writeChar
- Specified by:
writeCharin interfaceDataOutput- Throws:
IOException
-
writeChars
- Specified by:
writeCharsin interfaceDataOutput- Throws:
IOException
-
writeDouble
- Specified by:
writeDoublein interfaceDataOutput- Throws:
IOException
-
writeFloat
- Specified by:
writeFloatin interfaceDataOutput- Throws:
IOException
-
writeInt
- Specified by:
writeIntin interfaceDataOutput- Throws:
IOException
-
writeLong
- Specified by:
writeLongin interfaceDataOutput- Throws:
IOException
-
writeShort
- Specified by:
writeShortin interfaceDataOutput- Throws:
IOException
-
writeUTF
- Specified by:
writeUTFin interfaceDataOutput- Throws:
IOException
-