Package loci.common
Class NIOFileHandle
java.lang.Object
loci.common.AbstractNIOHandle
loci.common.NIOFileHandle
- All Implemented Interfaces:
DataInput,DataOutput,IRandomAccess
A wrapper for buffered NIO logic that implements the IRandomAccess interface.
- Author:
- Chris Allan (callan at blackcat dot ca)
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ByteBufferThe buffer itself.protected intThe buffer size.protected longThe absolute position of the start of the buffer.protected NIOByteBufferProviderProvider class for NIO byte buffers, allocated or memory mapped.protected FileChannelThe file channel backed by the random access file.protected static intDefault NIO buffer size to facilitate buffered I/O.protected static intDefault NIO buffer size to facilitate buffered I/O for read/write streams.protected booleanWhether or not the file is opened read/write.protected FileChannel.MapModeThe default map mode for the file.protected ByteOrderThe buffer's byte ordering.protected longThe absolute position within the file.protected RandomAccessFileThe random access file object backing this FileHandle.Fields inherited from class loci.common.AbstractNIOHandle
EOF_ERROR_MSG -
Constructor Summary
ConstructorsConstructorDescriptionNIOFileHandle(File file, String mode) Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.NIOFileHandle(File file, String mode, int bufferSize) Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.NIOFileHandle(String name, String mode) Creates a random access file stream to read from, and optionally to write to, a file with the specified name. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this random access stream and releases any system resources associated with the stream.intlongReturns the current offset in this stream.getOrder()Returns the current order (endianness) of the stream.longlength()Returns the length of this stream.intread(byte[] b) Reads up to b.length bytes of data from this stream into an array of bytes.intread(byte[] b, int off, int len) Reads up to len bytes of data from this stream into an array of bytes.intread(ByteBuffer buf) Reads up to buffer.capacity() bytes of data from this stream into a ByteBuffer.intread(ByteBuffer buf, int off, int len) Reads up to len bytes of data from this stream into a ByteBuffer.booleanbytereadByte()charreadChar()doublefloatvoidreadFully(byte[] b) voidreadFully(byte[] b, int off, int len) intreadInt()readLine()longreadLong()shortintintreadUTF()voidseek(long pos) Sets the stream pointer offset, measured from the beginning of this stream, at which the next read or write occurs.static voidsetDefaultBufferSize(int size) Set the default buffer size for read-only files.static voidsetDefaultReadWriteBufferSize(int size) Set the default buffer size for read/write files.voidsetLength(long length) Sets the new length of the handle.voidSets the byte order (endianness) of the stream.intskipBytes(int n) longskipBytes(long n) Alongvariant ofIRandomAccess.skipBytes(int).voidwrite(byte[] b) voidwrite(byte[] b, int off, int len) voidwrite(int b) voidwrite(ByteBuffer buf) Writes up to buffer.capacity() bytes of data from the given ByteBuffer to this stream.voidwrite(ByteBuffer buf, int off, int len) Writes up to len bytes of data from the given ByteBuffer to this stream.voidwriteBoolean(boolean v) voidwriteByte(int v) voidwriteBytes(String s) voidwriteChar(int v) voidwriteChars(String s) voidwriteDouble(double v) voidwriteFloat(float v) voidwriteInt(int v) voidwriteLong(long v) voidwriteShort(int v) voidMethods inherited from class loci.common.AbstractNIOHandle
exists, validateLength, validateMode
-
Field Details
-
defaultBufferSize
protected static int defaultBufferSizeDefault NIO buffer size to facilitate buffered I/O. -
defaultRWBufferSize
protected static int defaultRWBufferSizeDefault NIO buffer size to facilitate buffered I/O for read/write streams. -
raf
The random access file object backing this FileHandle. -
channel
The file channel backed by the random access file. -
position
protected long positionThe absolute position within the file. -
bufferStartPosition
protected long bufferStartPositionThe absolute position of the start of the buffer. -
bufferSize
protected int bufferSizeThe buffer size. -
buffer
The buffer itself. -
isReadWrite
protected boolean isReadWriteWhether or not the file is opened read/write. -
mapMode
The default map mode for the file. -
order
The buffer's byte ordering. -
byteBufferProvider
Provider class for NIO byte buffers, allocated or memory mapped.
-
-
Constructor Details
-
NIOFileHandle
Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.- Parameters:
file- aFilerepresenting a file on diskmode- the access mode;r(read only) andrw(read/write) are supportedbufferSize- the size of the buffer used to speed up reading and writing- Throws:
IOException- if there is an error accessing the file
-
NIOFileHandle
Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.- Parameters:
file- aFilerepresenting a file on diskmode- the access mode;r(read only) andrw(read/write) are supported- Throws:
IOException- if there is an error accessing the file
-
NIOFileHandle
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 diskmode- the access mode;r(read only) andrw(read/write) are supported- Throws:
IOException- if there is an error accessing the file
-
-
Method Details
-
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
- Returns:
- the random access file object backing this FileHandle.
-
getFileChannel
- Returns:
- the FileChannel from this FileHandle.
-
getBufferSize
public int getBufferSize()- Returns:
- the current buffer size.
-
setLength
Description copied from class:AbstractNIOHandleSets the new length of the handle.- Specified by:
setLengthin classAbstractNIOHandle- Parameters:
length- New length.- Throws:
IOException- If there is an error changing the handle's length.
-
close
Description copied from interface:IRandomAccessCloses 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:IRandomAccessReturns 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
Description copied from interface:IRandomAccessReturns the length of this stream.- Returns:
- the length in bytes of the stream
- Throws:
IOException- if the length cannot be retrieved
-
getOrder
Description copied from interface:IRandomAccessReturns the current order (endianness) of the stream.- Returns:
- See above.
-
setOrder
Description copied from interface:IRandomAccessSets the byte order (endianness) of the stream.- Parameters:
order- Order to set.
-
read
Description copied from interface:IRandomAccessReads 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
Description copied from interface:IRandomAccessReads up to len bytes of data from this stream into an array of bytes.- Parameters:
b- the array to fill from this streamoff- the offset inbfrom which to start filling; expected to be non-negative and no greater thanb.length - lenlen- the number of bytes to read; expected to be positive and no greater thanb.length - offset- Returns:
- the total number of bytes read into the buffer.
- Throws:
IOException- if reading is not possible
-
read
Description copied from interface:IRandomAccessReads 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
Description copied from interface:IRandomAccessReads up to len bytes of data from this stream into a ByteBuffer.- Parameters:
buf- the ByteBuffer to fill from this streamoff- the offset inbfrom which to start filling; expected to be non-negative and no greater thanbuffer.capacity() - lenlen- the number of bytes to read; expected to be positive and no greater thanbuffer.capacity() - offset- Returns:
- the total number of bytes read into the buffer.
- Throws:
IOException- if reading is not possible
-
seek
Description copied from interface:IRandomAccessSets 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- ifposis invalid or the seek fails- See Also:
-
readBoolean
- Throws:
IOException
-
readByte
- Throws:
IOException
-
readChar
- Throws:
IOException
-
readDouble
- Throws:
IOException
-
readFloat
- Throws:
IOException
-
readFully
- Throws:
IOException
-
readFully
- Throws:
IOException
-
readInt
- Throws:
IOException
-
readLine
- Throws:
IOException
-
readLong
- Throws:
IOException
-
readShort
- Throws:
IOException
-
readUnsignedByte
- Throws:
IOException
-
readUnsignedShort
- Throws:
IOException
-
readUTF
- Throws:
IOException
-
skipBytes
- Throws:
IOException
-
skipBytes
Description copied from interface:IRandomAccessAlongvariant ofIRandomAccess.skipBytes(int).- Parameters:
n- the number of bytes to skip- Returns:
- the number of bytes skipped
- Throws:
IOException- if the operation failed
-
write
- Throws:
IOException
-
write
- Throws:
IOException
-
write
Description copied from interface:IRandomAccessWrites 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
Description copied from interface:IRandomAccessWrites up to len bytes of data from the given ByteBuffer to this stream.- Parameters:
buf- the ByteBuffer containing bytes to write to this streamoff- the offset inbfrom which to start writing; expected to be non-negative and no greater thanbuf.capacity() - lenlen- the number of bytes to write; expected to be positive and no greater thanbuf.capacity() - offset- Throws:
IOException- if writing is not possible
-
write
- Throws:
IOException
-
writeBoolean
- Throws:
IOException
-
writeByte
- Throws:
IOException
-
writeBytes
- Throws:
IOException
-
writeChar
- Throws:
IOException
-
writeChars
- Throws:
IOException
-
writeDouble
- Throws:
IOException
-
writeFloat
- Throws:
IOException
-
writeInt
- Throws:
IOException
-
writeLong
- Throws:
IOException
-
writeShort
- Throws:
IOException
-
writeUTF
- Throws:
IOException
-