Package loci.common

Class NIOFileHandle

    • Field Detail

      • defaultBufferSize

        protected static int defaultBufferSize
        Default NIO buffer size to facilitate buffered I/O.
      • defaultRWBufferSize

        protected static int defaultRWBufferSize
        Default NIO buffer size to facilitate buffered I/O for read/write streams.
      • raf

        protected RandomAccessFile raf
        The random access file object backing this FileHandle.
      • channel

        protected FileChannel channel
        The file channel backed by the random access file.
      • position

        protected long position
        The absolute position within the file.
      • bufferStartPosition

        protected long bufferStartPosition
        The absolute position of the start of the buffer.
      • bufferSize

        protected int bufferSize
        The buffer size.
      • buffer

        protected ByteBuffer buffer
        The buffer itself.
      • isReadWrite

        protected boolean isReadWrite
        Whether or not the file is opened read/write.
      • order

        protected ByteOrder order
        The buffer's byte ordering.
      • byteBufferProvider

        protected NIOByteBufferProvider byteBufferProvider
        Provider class for NIO byte buffers, allocated or memory mapped.
    • Constructor Detail

      • NIOFileHandle

        public NIOFileHandle​(File file,
                             String mode,
                             int bufferSize)
                      throws IOException
        Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.
        Parameters:
        file - a File representing a file on disk
        mode - the access mode; r (read only) and rw (read/write) are supported
        bufferSize - the size of the buffer used to speed up reading and writing
        Throws:
        IOException - if there is an error accessing the file
      • NIOFileHandle

        public NIOFileHandle​(File file,
                             String mode)
                      throws IOException
        Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.
        Parameters:
        file - a File representing a file on disk
        mode - the access mode; r (read only) and rw (read/write) are supported
        Throws:
        IOException - if there is an error accessing the file
      • NIOFileHandle

        public NIOFileHandle​(String name,
                             String mode)
                      throws IOException
        Creates a random access file stream to read from, and optionally to write to, a file with the specified name.
        Parameters:
        name - the path to a file on disk
        mode - the access mode; r (read only) and rw (read/write) are supported
        Throws:
        IOException - if there is an error accessing the file
    • Method Detail

      • setDefaultBufferSize

        public static void setDefaultBufferSize​(int size)
        Set the default buffer size for read-only files. Subsequent uses of the NIOFileHandle(String, String) and NIOFileHandle(File, String) constructors will use this buffer size.
        Parameters:
        size - the new default buffer size
      • setDefaultReadWriteBufferSize

        public static void setDefaultReadWriteBufferSize​(int size)
        Set the default buffer size for read/write files. Subsequent uses of the NIOFileHandle(String, String) and NIOFileHandle(File, String) constructors will use this buffer size.
        Parameters:
        size - the new default buffer size
      • getRandomAccessFile

        public RandomAccessFile getRandomAccessFile()
        Returns:
        the random access file object backing this FileHandle.
      • getFileChannel

        public FileChannel getFileChannel()
        Returns:
        the FileChannel from this FileHandle.
      • getBufferSize

        public int getBufferSize()
        Returns:
        the current buffer size.
      • close

        public void close()
                   throws IOException
        Description copied from interface: IRandomAccess
        Closes this random access stream and releases any system resources associated with the stream.
        Throws:
        IOException - if the underlying stream(s) could not be closed
      • getFilePointer

        public long getFilePointer()
        Description copied from interface: IRandomAccess
        Returns the current offset in this stream.
        Returns:
        the current byte offset within the file; expected to be non-negative and less than the value of #length()
      • length

        public long length()
                    throws IOException
        Description copied from interface: IRandomAccess
        Returns the length of this stream.
        Returns:
        the length in bytes of the stream
        Throws:
        IOException - if the length cannot be retrieved
      • getOrder

        public ByteOrder getOrder()
        Description copied from interface: IRandomAccess
        Returns the current order (endianness) of the stream.
        Returns:
        See above.
      • setOrder

        public void setOrder​(ByteOrder order)
        Description copied from interface: IRandomAccess
        Sets the byte order (endianness) of the stream.
        Parameters:
        order - Order to set.
      • read

        public int read​(byte[] b)
                 throws IOException
        Description copied from interface: IRandomAccess
        Reads up to b.length bytes of data from this stream into an array of bytes.
        Parameters:
        b - the array to fill from this stream
        Returns:
        the total number of bytes read into the buffer.
        Throws:
        IOException - if reading is not possible
      • read

        public int read​(byte[] b,
                        int off,
                        int len)
                 throws IOException
        Description copied from interface: IRandomAccess
        Reads up to len bytes of data from this stream into an array of bytes.
        Parameters:
        b - the array to fill from this stream
        off - the offset in b from which to start filling; expected to be non-negative and no greater than b.length - len
        len - the number of bytes to read; expected to be positive and no greater than b.length - offset
        Returns:
        the total number of bytes read into the buffer.
        Throws:
        IOException - if reading is not possible
      • read

        public int read​(ByteBuffer buf)
                 throws IOException
        Description copied from interface: IRandomAccess
        Reads up to buffer.capacity() bytes of data from this stream into a ByteBuffer.
        Parameters:
        buf - the ByteBuffer to fill from this stream
        Returns:
        the total number of bytes read into the buffer.
        Throws:
        IOException - if reading is not possible
      • read

        public int read​(ByteBuffer buf,
                        int off,
                        int len)
                 throws IOException
        Description copied from interface: IRandomAccess
        Reads up to len bytes of data from this stream into a ByteBuffer.
        Parameters:
        buf - the ByteBuffer to fill from this stream
        off - the offset in b from which to start filling; expected to be non-negative and no greater than buffer.capacity() - len
        len - the number of bytes to read; expected to be positive and no greater than buffer.capacity() - offset
        Returns:
        the total number of bytes read into the buffer.
        Throws:
        IOException - if reading is not possible
      • seek

        public void seek​(long pos)
                  throws IOException
        Description copied from interface: IRandomAccess
        Sets the stream pointer offset, measured from the beginning of this stream, at which the next read or write occurs.
        Parameters:
        pos - new byte offset (pointer) in the current stream. Unless otherwise noted, may be larger or smaller than the current pointer, but must be non-negative and less than the value of #length()
        Throws:
        IOException - if pos is invalid or the seek fails
        See Also:
        IRandomAccess.getFilePointer()
      • write

        public void write​(ByteBuffer buf)
                   throws IOException
        Description copied from interface: IRandomAccess
        Writes up to buffer.capacity() bytes of data from the given ByteBuffer to this stream.
        Parameters:
        buf - the ByteBuffer containing bytes to write to this stream
        Throws:
        IOException - if writing is not possible
      • write

        public void write​(ByteBuffer buf,
                          int off,
                          int len)
                   throws IOException
        Description copied from interface: IRandomAccess
        Writes up to len bytes of data from the given ByteBuffer to this stream.
        Parameters:
        buf - the ByteBuffer containing bytes to write to this stream
        off - the offset in b from which to start writing; expected to be non-negative and no greater than buf.capacity() - len
        len - the number of bytes to write; expected to be positive and no greater than buf.capacity() - offset
        Throws:
        IOException - if writing is not possible