Package loci.common
Class NIOFileHandle
- java.lang.Object
-
- loci.common.AbstractNIOHandle
-
- loci.common.NIOFileHandle
-
- All Implemented Interfaces:
DataInput
,DataOutput
,IRandomAccess
public class NIOFileHandle extends AbstractNIOHandle
A wrapper for buffered NIO logic that implements the IRandomAccess interface.- Author:
- Chris Allan (callan at blackcat dot ca)
- See Also:
IRandomAccess
,RandomAccessFile
-
-
Field Summary
Fields Modifier and Type Field Description protected ByteBuffer
buffer
The buffer itself.protected int
bufferSize
The buffer size.protected long
bufferStartPosition
The absolute position of the start of the buffer.protected NIOByteBufferProvider
byteBufferProvider
Provider class for NIO byte buffers, allocated or memory mapped.protected FileChannel
channel
The file channel backed by the random access file.protected static int
defaultBufferSize
Default NIO buffer size to facilitate buffered I/O.protected static int
defaultRWBufferSize
Default NIO buffer size to facilitate buffered I/O for read/write streams.protected boolean
isReadWrite
Whether or not the file is opened read/write.protected FileChannel.MapMode
mapMode
The default map mode for the file.protected ByteOrder
order
The buffer's byte ordering.protected long
position
The absolute position within the file.protected RandomAccessFile
raf
The random access file object backing this FileHandle.-
Fields inherited from class loci.common.AbstractNIOHandle
EOF_ERROR_MSG
-
-
Constructor Summary
Constructors Constructor Description NIOFileHandle(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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes this random access stream and releases any system resources associated with the stream.int
getBufferSize()
FileChannel
getFileChannel()
long
getFilePointer()
Returns the current offset in this stream.ByteOrder
getOrder()
Returns the current order (endianness) of the stream.RandomAccessFile
getRandomAccessFile()
long
length()
Returns the length of this stream.int
read(byte[] b)
Reads up to b.length bytes of data from this stream into an array of bytes.int
read(byte[] b, int off, int len)
Reads up to len bytes of data from this stream into an array of bytes.int
read(ByteBuffer buf)
Reads up to buffer.capacity() bytes of data from this stream into a ByteBuffer.int
read(ByteBuffer buf, int off, int len)
Reads up to len bytes of data from this stream into a ByteBuffer.boolean
readBoolean()
byte
readByte()
char
readChar()
double
readDouble()
float
readFloat()
void
readFully(byte[] b)
void
readFully(byte[] b, int off, int len)
int
readInt()
String
readLine()
long
readLong()
short
readShort()
int
readUnsignedByte()
int
readUnsignedShort()
String
readUTF()
void
seek(long pos)
Sets the stream pointer offset, measured from the beginning of this stream, at which the next read or write occurs.static void
setDefaultBufferSize(int size)
Set the default buffer size for read-only files.static void
setDefaultReadWriteBufferSize(int size)
Set the default buffer size for read/write files.void
setLength(long length)
Sets the new length of the handle.void
setOrder(ByteOrder order)
Sets the byte order (endianness) of the stream.int
skipBytes(int n)
long
skipBytes(long n)
Along
variant ofDataInput.skipBytes(int)
.void
write(byte[] b)
void
write(byte[] b, int off, int len)
void
write(int b)
void
write(ByteBuffer buf)
Writes up to buffer.capacity() bytes of data from the given ByteBuffer to this stream.void
write(ByteBuffer buf, int off, int len)
Writes up to len bytes of data from the given ByteBuffer to this stream.void
writeBoolean(boolean v)
void
writeByte(int v)
void
writeBytes(String s)
void
writeChar(int v)
void
writeChars(String s)
void
writeDouble(double v)
void
writeFloat(float v)
void
writeInt(int v)
void
writeLong(long v)
void
writeShort(int v)
void
writeUTF(String str)
-
Methods inherited from class loci.common.AbstractNIOHandle
exists, validateLength, validateMode
-
-
-
-
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.
-
mapMode
protected FileChannel.MapMode mapMode
The default map mode for the file.
-
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
- aFile
representing 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
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
- aFile
representing 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
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 diskmode
- the access mode;r
(read only) andrw
(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.
-
setLength
public void setLength(long length) throws IOException
Description copied from class:AbstractNIOHandle
Sets the new length of the handle.- Specified by:
setLength
in classAbstractNIOHandle
- Parameters:
length
- New length.- Throws:
IOException
- If there is an error changing the handle's length.
-
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 streamoff
- the offset inb
from which to start filling; expected to be non-negative and no greater thanb.length - len
len
- 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
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 streamoff
- the offset inb
from which to start filling; expected to be non-negative and no greater thanbuffer.capacity() - len
len
- 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
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
- ifpos
is invalid or the seek fails- See Also:
IRandomAccess.getFilePointer()
-
readBoolean
public boolean readBoolean() throws IOException
- Throws:
IOException
-
readByte
public byte readByte() throws IOException
- Throws:
IOException
-
readChar
public char readChar() throws IOException
- Throws:
IOException
-
readDouble
public double readDouble() throws IOException
- Throws:
IOException
-
readFloat
public float readFloat() throws IOException
- Throws:
IOException
-
readFully
public void readFully(byte[] b) throws IOException
- Throws:
IOException
-
readFully
public void readFully(byte[] b, int off, int len) throws IOException
- Throws:
IOException
-
readInt
public int readInt() throws IOException
- Throws:
IOException
-
readLine
public String readLine() throws IOException
- Throws:
IOException
-
readLong
public long readLong() throws IOException
- Throws:
IOException
-
readShort
public short readShort() throws IOException
- Throws:
IOException
-
readUnsignedByte
public int readUnsignedByte() throws IOException
- Throws:
IOException
-
readUnsignedShort
public int readUnsignedShort() throws IOException
- Throws:
IOException
-
readUTF
public String readUTF() throws IOException
- Throws:
IOException
-
skipBytes
public int skipBytes(int n) throws IOException
- Throws:
IOException
-
skipBytes
public long skipBytes(long n) throws IOException
Description copied from interface:IRandomAccess
Along
variant ofDataInput.skipBytes(int)
.- Parameters:
n
- the number of bytes to skip- Returns:
- the number of bytes skipped
- Throws:
IOException
- if the operation failed
-
write
public void write(byte[] b) throws IOException
- Throws:
IOException
-
write
public void write(byte[] b, int off, int len) throws IOException
- Throws:
IOException
-
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 streamoff
- the offset inb
from which to start writing; expected to be non-negative and no greater thanbuf.capacity() - len
len
- the number of bytes to write; expected to be positive and no greater thanbuf.capacity() - offset
- Throws:
IOException
- if writing is not possible
-
write
public void write(int b) throws IOException
- Throws:
IOException
-
writeBoolean
public void writeBoolean(boolean v) throws IOException
- Throws:
IOException
-
writeByte
public void writeByte(int v) throws IOException
- Throws:
IOException
-
writeBytes
public void writeBytes(String s) throws IOException
- Throws:
IOException
-
writeChar
public void writeChar(int v) throws IOException
- Throws:
IOException
-
writeChars
public void writeChars(String s) throws IOException
- Throws:
IOException
-
writeDouble
public void writeDouble(double v) throws IOException
- Throws:
IOException
-
writeFloat
public void writeFloat(float v) throws IOException
- Throws:
IOException
-
writeInt
public void writeInt(int v) throws IOException
- Throws:
IOException
-
writeLong
public void writeLong(long v) throws IOException
- Throws:
IOException
-
writeShort
public void writeShort(int v) throws IOException
- Throws:
IOException
-
writeUTF
public void writeUTF(String str) throws IOException
- Throws:
IOException
-
-