Package nom.tam.util

Class BufferedFile

java.lang.Object
nom.tam.util.BufferedFile
All Implemented Interfaces:
Closeable, DataInput, DataOutput, AutoCloseable, ArrayDataInput, ArrayDataOutput, FitsIO, RandomAccess

public class BufferedFile extends Object implements ArrayDataOutput, RandomAccess
  • Field Details

    • DEFAULT_BUFFER_SIZE

      private static final int DEFAULT_BUFFER_SIZE
      See Also:
    • LOG

      private static final Logger LOG
    • bufferPointer

      private final BufferPointer bufferPointer
    • dataDecoder

      private final BufferDecoder dataDecoder
    • dataEncoder

      private final BufferEncoder dataEncoder
    • randomAccessFile

      private final RandomAccessFile randomAccessFile
      The underlying access to the file system
    • fileOffset

      private long fileOffset
      The offset of the beginning of the current dataBuffer.buffer
    • doingInput

      private boolean doingInput
      Is the dataBuffer.buffer being used for input or output
    • bufferMarker

      private int bufferMarker
      marker position in the dataBuffer.buffer.
  • Constructor Details

    • BufferedFile

      public BufferedFile(File file) throws IOException
      Create a buffered file from a File descriptor
      Parameters:
      file - the file to open.
      Throws:
      IOException - if the file could not be opened
    • BufferedFile

      public BufferedFile(File file, String mode) throws IOException
      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

      public BufferedFile(File file, String mode, int bufferSize) throws IOException
      Create a buffered file from a file descriptor
      Parameters:
      file - the file to open.
      mode - the mode to open the file in
      bufferSize - the dataBuffer.buffer size to use
      Throws:
      IOException - if the file could not be opened
    • BufferedFile

      public BufferedFile(String filename) throws IOException
      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

      public BufferedFile(String filename, String mode) throws IOException
      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

      public BufferedFile(String filename, String mode, int bufferSize) throws IOException
      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

      private void checkBuffer(int needBytes) throws IOException
      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

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface FitsIO
      Throws:
      IOException
    • finalize

      protected void finalize()
      Overrides:
      finalize in class Object
    • flush

      public void flush() throws IOException
      Description copied from interface: ArrayDataOutput
      Flush the output buffer
      Specified by:
      flush in interface ArrayDataOutput
      Throws:
      IOException - if the flush of the underlying stream failed
    • getChannel

      public FileChannel 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

      public FileDescriptor getFD() throws IOException
      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:
      getFilePointer in interface RandomAccess
      Returns:
      the current position in the stream.
    • length

      public long length() throws IOException
      Returns:
      the current length of the file.
      Throws:
      IOException - if the operation failed
    • mark

      public void mark(int readlimit) throws IOException
      Description copied from interface: ArrayDataInput
      See the general contract of the mark method of InputStream.
      Specified by:
      mark in interface ArrayDataInput
      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

      private void needBuffer(int need) throws IOException
      Throws:
      IOException
    • read

      public int read() throws IOException
      Returns:
      Read a byte.
      Throws:
      IOException - if the underlying read operation fails
    • read

      public int read(boolean[] b) throws IOException
      Description copied from interface: ArrayDataInput
      Read an array of boolean's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      b - array of boolean's.
      Returns:
      number of bytes read.
      Throws:
      IOException - if one of the underlying read operations failed
    • read

      public int read(boolean[] b, int start, int length) throws IOException
      Description copied from interface: ArrayDataInput
      Read a segment of an array of boolean's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      b - array of boolean's.
      start - start index in the array
      length - number of array elements to read
      Returns:
      number of bytes read.
      Throws:
      IOException - if one of the underlying read operations failed
    • read

      public int read(byte[] buf) throws IOException
      Description copied from interface: ArrayDataInput
      Read an array of byte's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      buf - array of byte's.
      Returns:
      number of bytes read.
      Throws:
      IOException - if one of the underlying read operations failed
      See Also:
    • read

      public int read(byte[] buf, int offset, int len) throws IOException
      Description copied from interface: ArrayDataInput
      Read a segment of an array of byte's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      buf - array of byte's.
      offset - start index in the array
      len - number of array elements to read
      Returns:
      number of bytes read.
      Throws:
      IOException - if one of the underlying read operations failed
      See Also:
    • read

      public int read(char[] c) throws IOException
      Description copied from interface: ArrayDataInput
      Read an array of char's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      c - array of char's.
      Returns:
      number of bytes read.
      Throws:
      IOException - if one of the underlying read operations failed
    • read

      public int read(char[] c, int start, int length) throws IOException
      Description copied from interface: ArrayDataInput
      Read a segment of an array of char's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      c - array of char's.
      start - start index in the array
      length - number of array elements to read
      Returns:
      number of bytes read.
      Throws:
      IOException - if one of the underlying read operations failed
    • read

      public int read(double[] d) throws IOException
      Description copied from interface: ArrayDataInput
      Read an array of double's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      d - array of double's.
      Returns:
      number of bytes read.
      Throws:
      IOException - if one of the underlying read operations failed
    • read

      public int read(double[] d, int start, int length) throws IOException
      Description copied from interface: ArrayDataInput
      Read a segment of an array of double's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      d - array of double's.
      start - start index in the array
      length - number of array elements to read
      Returns:
      number of bytes read.
      Throws:
      IOException - if one of the underlying read operations failed
    • read

      public int read(float[] f) throws IOException
      Description copied from interface: ArrayDataInput
      Read an array of float's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      f - array of float's.
      Returns:
      number of bytes read.
      Throws:
      IOException - if one of the underlying read operations failed
    • read

      public int read(float[] f, int start, int length) throws IOException
      Description copied from interface: ArrayDataInput
      Read a segment of an array of float's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      f - array of float's.
      start - start index in the array
      length - number of array elements to read
      Returns:
      number of bytes read.
      Throws:
      IOException - if one of the underlying read operations failed
    • read

      public int read(int[] i) throws IOException
      Description copied from interface: ArrayDataInput
      Read an array of int's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      i - array of int's.
      Returns:
      number of bytes read.
      Throws:
      IOException - if one of the underlying read operations failed
    • read

      public int read(int[] i, int start, int length) throws IOException
      Description copied from interface: ArrayDataInput
      Read a segment of an array of int's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      i - array of int's.
      start - start index in the array
      length - number of array elements to read
      Returns:
      number of bytes read.
      Throws:
      IOException - if one of the underlying read operations failed
    • read

      public int read(long[] l) throws IOException
      Description copied from interface: ArrayDataInput
      Read a segment of an array of long's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      l - array of long's.
      Returns:
      number of bytes read.
      Throws:
      IOException - if one of the underlying read operations failed
    • read

      public int read(long[] l, int start, int length) throws IOException
      Description copied from interface: ArrayDataInput
      Read a segment of an array of long's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      l - array of long's.
      start - start index in the array
      length - number of array elements to read
      Returns:
      number of bytes read.
      Throws:
      IOException - if one of the underlying read operations failed
    • read

      public int read(short[] s) throws IOException
      Description copied from interface: ArrayDataInput
      Read an array of short's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      s - array of short's.
      Returns:
      number of bytes read.
      Throws:
      IOException - if one of the underlying read operations failed
    • read

      public int read(short[] s, int start, int length) throws IOException
      Description copied from interface: ArrayDataInput
      Read a segment of an array of short's.
      Specified by:
      read in interface ArrayDataInput
      Parameters:
      s - array of short's.
      start - start index in the array
      length - number of array elements to read
      Returns:
      number of bytes read.
      Throws:
      IOException - if one of the underlying read operations failed
    • readArray

      @Deprecated public int readArray(Object o) throws IOException
      Deprecated.
      Description copied from interface: ArrayDataInput
      Read 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:
      readArray in interface ArrayDataInput
      Parameters:
      o - A [multidimensional] primitive (or Object) array.
      Returns:
      number of bytes read.
      Throws:
      IOException - if the underlying stream failed
    • readBoolean

      public boolean readBoolean() throws IOException
      Specified by:
      readBoolean in interface DataInput
      Throws:
      IOException
    • readByte

      public byte readByte() throws IOException
      Specified by:
      readByte in interface DataInput
      Throws:
      IOException
    • readChar

      public char readChar() throws IOException
      Specified by:
      readChar in interface DataInput
      Throws:
      IOException
    • readDouble

      public double readDouble() throws IOException
      Specified by:
      readDouble in interface DataInput
      Throws:
      IOException
    • readFloat

      public float readFloat() throws IOException
      Specified by:
      readFloat in interface DataInput
      Throws:
      IOException
    • readFully

      public void readFully(byte[] b) throws IOException
      Specified by:
      readFully in interface DataInput
      Throws:
      IOException
    • readFully

      public void readFully(byte[] b, int off, int len) throws IOException
      Description copied from interface: ArrayDataInput
      Read 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:
      readFully in interface ArrayDataInput
      Specified by:
      readFully in interface DataInput
      Parameters:
      b - The input buffer.
      off - The requested offset into the buffer.
      len - The number of bytes requested.
      Throws:
      IOException
    • readInt

      public int readInt() throws IOException
      Specified by:
      readInt in interface DataInput
      Throws:
      IOException
    • readLArray

      public long readLArray(Object o) throws IOException
      Description copied from interface: ArrayDataInput
      Read 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:
      readLArray in interface ArrayDataInput
      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

      public String readLine() throws IOException
      Read a line of input.
      Specified by:
      readLine in interface DataInput
      Returns:
      the next line.
      Throws:
      IOException
    • readLong

      public long readLong() throws IOException
      Specified by:
      readLong in interface DataInput
      Throws:
      IOException
    • readShort

      public short readShort() throws IOException
      Specified by:
      readShort in interface DataInput
      Throws:
      IOException
    • readUnsignedByte

      public int readUnsignedByte() throws IOException
      Specified by:
      readUnsignedByte in interface DataInput
      Throws:
      IOException
    • readUnsignedShort

      public int readUnsignedShort() throws IOException
      Specified by:
      readUnsignedShort in interface DataInput
      Throws:
      IOException
    • readUTF

      public String readUTF() throws IOException
      Specified by:
      readUTF in interface DataInput
      Throws:
      IOException
    • reset

      public void reset() throws IOException
      Description copied from interface: ArrayDataInput
      See the general contract of the reset method of InputStream.

      If markpos is -1 (no mark has been set or the mark has been invalidated), an IOException is thrown. Otherwise, pos is set equal to markpos.

      Specified by:
      reset in interface ArrayDataInput
      Throws:
      IOException - if this stream has not been marked or, if the mark has been invalidated, or the stream has been closed by invoking its FitsIO.close() method, or an I/O error occurs.
      See Also:
    • seek

      public void seek(long offsetFromStart) throws IOException
      Description copied from interface: RandomAccess
      Move to a specified location in the stream.
      Specified by:
      seek in interface RandomAccess
      Parameters:
      offsetFromStart - set the offset messured from the start
      Throws:
      IOException - if the operation fails
    • setLength

      public void setLength(long newLength) throws IOException
      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

      public long skip(long offset) throws IOException
      Description copied from interface: ArrayDataInput
      Skip 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:
      skip in interface ArrayDataInput
      Parameters:
      offset - the number of bytes to skip
      Returns:
      the number of bytes really skipped
      Throws:
      IOException - if the underlying stream failed
    • skipAllBytes

      public void skipAllBytes(int toSkip) throws IOException
      Description copied from interface: ArrayDataInput
      Skip 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:
      skipAllBytes in interface ArrayDataInput
      Parameters:
      toSkip - the number of bytes to skip
      Throws:
      IOException - if the underlying stream failed
    • skipAllBytes

      public void skipAllBytes(long toSkip) throws IOException
      Description copied from interface: ArrayDataInput
      Skip 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:
      skipAllBytes in interface ArrayDataInput
      Parameters:
      toSkip - the number of bytes to skip
      Throws:
      IOException - if the underlying stream failed
    • skipBytes

      public int skipBytes(int n) throws IOException
      Specified by:
      skipBytes in interface DataInput
      Throws:
      IOException
    • write

      public void write(boolean[] b) throws IOException
      Description copied from interface: ArrayDataOutput
      Write an array of boolean's.
      Specified by:
      write in interface ArrayDataOutput
      Parameters:
      b - array of boolean's.
      Throws:
      IOException - if one of the underlying write operations failed
    • write

      public void write(boolean[] b, int start, int length) throws IOException
      Description copied from interface: ArrayDataOutput
      Write a segment of an array of boolean's.
      Specified by:
      write in interface ArrayDataOutput
      Parameters:
      b - array of boolean's.
      start - start index in the array
      length - number of array elements to write
      Throws:
      IOException - if one of the underlying write operations failed
    • write

      public void write(byte[] buf) throws IOException
      Specified by:
      write in interface DataOutput
      Throws:
      IOException
    • write

      public void write(byte[] buf, int offset, int length) throws IOException
      Specified by:
      write in interface DataOutput
      Throws:
      IOException
    • write

      public void write(char[] c) throws IOException
      Description copied from interface: ArrayDataOutput
      Write an array of char's.
      Specified by:
      write in interface ArrayDataOutput
      Parameters:
      c - array of char's.
      Throws:
      IOException - if one of the underlying write operations failed
    • write

      public void write(char[] c, int start, int length) throws IOException
      Description copied from interface: ArrayDataOutput
      Write a segment of an array of char's.
      Specified by:
      write in interface ArrayDataOutput
      Parameters:
      c - array of char's.
      start - start index in the array
      length - number of array elements to write
      Throws:
      IOException - if one of the underlying write operations failed
    • write

      public void write(double[] d) throws IOException
      Description copied from interface: ArrayDataOutput
      Write an array of double's.
      Specified by:
      write in interface ArrayDataOutput
      Parameters:
      d - array of double's.
      Throws:
      IOException - if one of the underlying write operations failed
    • write

      public void write(double[] d, int start, int length) throws IOException
      Description copied from interface: ArrayDataOutput
      Write a segment of an array of double's.
      Specified by:
      write in interface ArrayDataOutput
      Parameters:
      d - array of double's.
      start - start index in the array
      length - number of array elements to write
      Throws:
      IOException - if one of the underlying write operations failed
    • write

      public void write(float[] f) throws IOException
      Description copied from interface: ArrayDataOutput
      Write an array of float's.
      Specified by:
      write in interface ArrayDataOutput
      Parameters:
      f - array of float's.
      Throws:
      IOException - if one of the underlying write operations failed
    • write

      public void write(float[] f, int start, int length) throws IOException
      Description copied from interface: ArrayDataOutput
      Write a segment of an array of float's.
      Specified by:
      write in interface ArrayDataOutput
      Parameters:
      f - array of float's.
      start - start index in the array
      length - number of array elements to write
      Throws:
      IOException - if one of the underlying write operations failed
    • write

      public void write(int buf) throws IOException
      Specified by:
      write in interface DataOutput
      Throws:
      IOException
    • write

      public void write(int[] i) throws IOException
      Description copied from interface: ArrayDataOutput
      Write an array of int's.
      Specified by:
      write in interface ArrayDataOutput
      Parameters:
      i - array of int's
      Throws:
      IOException - if one of the underlying write operations failed
    • write

      public void write(int[] i, int start, int length) throws IOException
      Description copied from interface: ArrayDataOutput
      Write a segment of an array of int's.
      Specified by:
      write in interface ArrayDataOutput
      Parameters:
      i - array of int's
      start - start index in the array
      length - number of array elements to write
      Throws:
      IOException - if one of the underlying write operations failed
    • write

      public void write(long[] l) throws IOException
      Description copied from interface: ArrayDataOutput
      Write an array of longs.
      Specified by:
      write in interface ArrayDataOutput
      Parameters:
      l - array of longs
      Throws:
      IOException - if one of the underlying write operations failed
    • write

      public void write(long[] l, int start, int length) throws IOException
      Description copied from interface: ArrayDataOutput
      Write a segment of an array of longs.
      Specified by:
      write in interface ArrayDataOutput
      Parameters:
      l - array of longs
      start - start index in the array
      length - number of array elements to write
      Throws:
      IOException - if one of the underlying write operations failed
    • write

      public void write(short[] s) throws IOException
      Description copied from interface: ArrayDataOutput
      Write an array of shorts.
      Specified by:
      write in interface ArrayDataOutput
      Parameters:
      s - the value to write
      Throws:
      IOException - if one of the underlying write operations failed
    • write

      public void write(short[] s, int start, int length) throws IOException
      Description copied from interface: ArrayDataOutput
      Write a segment of an array of shorts.
      Specified by:
      write in interface ArrayDataOutput
      Parameters:
      s - the value to write
      start - start index in the array
      length - number of array elements to write
      Throws:
      IOException - if one of the underlying write operations failed
    • write

      public void write(String[] s) throws IOException
      Description copied from interface: ArrayDataOutput
      Write an array of Strings. Equivalent to calling writeBytes for the selected elements.
      Specified by:
      write in interface ArrayDataOutput
      Parameters:
      s - the array to write
      Throws:
      IOException - if one of the underlying write operations failed
    • write

      public void write(String[] s, int start, int length) throws IOException
      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 interface ArrayDataOutput
      Parameters:
      s - the array to write
      start - start index in the array
      length - number of array elements to write
      Throws:
      IOException - if one of the underlying write operations failed
    • writeArray

      public void writeArray(Object o) throws IOException
      Description copied from interface: ArrayDataOutput
      This 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:
      writeArray in interface ArrayDataOutput
      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

      public void writeBoolean(boolean b) throws IOException
      Specified by:
      writeBoolean in interface DataOutput
      Throws:
      IOException
    • writeByte

      public void writeByte(int b) throws IOException
      Specified by:
      writeByte in interface DataOutput
      Throws:
      IOException
    • writeBytes

      public void writeBytes(String s) throws IOException
      Specified by:
      writeBytes in interface DataOutput
      Throws:
      IOException
    • writeChar

      public void writeChar(int c) throws IOException
      Specified by:
      writeChar in interface DataOutput
      Throws:
      IOException
    • writeChars

      public void writeChars(String s) throws IOException
      Specified by:
      writeChars in interface DataOutput
      Throws:
      IOException
    • writeDouble

      public void writeDouble(double d) throws IOException
      Specified by:
      writeDouble in interface DataOutput
      Throws:
      IOException
    • writeFloat

      public void writeFloat(float f) throws IOException
      Specified by:
      writeFloat in interface DataOutput
      Throws:
      IOException
    • writeInt

      public void writeInt(int i) throws IOException
      Specified by:
      writeInt in interface DataOutput
      Throws:
      IOException
    • writeLong

      public void writeLong(long l) throws IOException
      Specified by:
      writeLong in interface DataOutput
      Throws:
      IOException
    • writeShort

      public void writeShort(int s) throws IOException
      Specified by:
      writeShort in interface DataOutput
      Throws:
      IOException
    • writeUTF

      public void writeUTF(String s) throws IOException
      Specified by:
      writeUTF in interface DataOutput
      Throws:
      IOException