Package loci.common
Class FileHandle
- java.lang.Object
-
- loci.common.FileHandle
-
- All Implemented Interfaces:
DataInput,DataOutput,IRandomAccess
public class FileHandle extends Object implements IRandomAccess
A wrapper for RandomAccessFile that implements the IRandomAccess interface.- Author:
- Curtis Rueden ctrueden at wisc.edu
- See Also:
IRandomAccess,RandomAccessFile
-
-
Field Summary
Fields Modifier and Type Field Description protected RandomAccessFilerafThe random access file object backing this FileHandle.
-
Constructor Summary
Constructors Constructor Description FileHandle(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.FileHandle(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 Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes this random access stream and releases any system resources associated with the stream.booleanexists()Returns whether this refers to a valid objectlonggetFilePointer()Returns the current offset in this stream.ByteOrdergetOrder()Returns the current order (endianness) of the stream.RandomAccessFilegetRandomAccessFile()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 buffer)Reads up to buffer.capacity() bytes of data from this stream into a ByteBuffer.intread(ByteBuffer buffer, int off, int len)Reads up to len bytes of data from this stream into a ByteBuffer.booleanreadBoolean()bytereadByte()charreadChar()doublereadDouble()floatreadFloat()voidreadFully(byte[] b)voidreadFully(byte[] b, int off, int len)intreadInt()StringreadLine()longreadLong()shortreadShort()intreadUnsignedByte()intreadUnsignedShort()StringreadUTF()voidseek(long pos)Sets the stream pointer offset, measured from the beginning of this stream, at which the next read or write occurs.voidsetOrder(ByteOrder order)Sets the byte order (endianness) of the stream.intskipBytes(int n)longskipBytes(long n)Alongvariant ofDataInput.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)voidwriteUTF(String str)
-
-
-
Field Detail
-
raf
protected RandomAccessFile raf
The random access file object backing this FileHandle.
-
-
Constructor Detail
-
FileHandle
public FileHandle(File file, String mode) throws FileNotFoundException
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- a valid access mode as defined inRandomAccessFile- Throws:
FileNotFoundException- if the file does not exist
-
FileHandle
public FileHandle(String name, String mode) throws FileNotFoundException
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- a valid access mode as defined inRandomAccessFile- Throws:
FileNotFoundException- if the file does not exist
-
-
Method Detail
-
getRandomAccessFile
public RandomAccessFile getRandomAccessFile()
- Returns:
- the
RandomAccessFileobject backing this FileHandle.
-
close
public void close() throws IOExceptionDescription copied from interface:IRandomAccessCloses this random access stream and releases any system resources associated with the stream.- Specified by:
closein interfaceIRandomAccess- Throws:
IOException- if the underlying stream(s) could not be closed
-
getFilePointer
public long getFilePointer() throws IOExceptionDescription copied from interface:IRandomAccessReturns the current offset in this stream.- Specified by:
getFilePointerin interfaceIRandomAccess- Returns:
- the current byte offset within the file; expected to be non-negative and less than the value of #length()
- Throws:
IOException- if the offset cannot be retrieved
-
exists
public boolean exists() throws IOExceptionDescription copied from interface:IRandomAccessReturns whether this refers to a valid object- Specified by:
existsin interfaceIRandomAccess- Returns:
- true if this refers to a valid object
- Throws:
IOException- if unable to determine whether the object is valid
-
length
public long length() throws IOExceptionDescription copied from interface:IRandomAccessReturns the length of this stream.- Specified by:
lengthin interfaceIRandomAccess- Returns:
- the length in bytes of the stream
- Throws:
IOException- if the length cannot be retrieved
-
read
public int read(byte[] b) throws IOExceptionDescription copied from interface:IRandomAccessReads up to b.length bytes of data from this stream into an array of bytes.- Specified by:
readin interfaceIRandomAccess- 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 IOExceptionDescription copied from interface:IRandomAccessReads up to len bytes of data from this stream into an array of bytes.- Specified by:
readin interfaceIRandomAccess- 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
public int read(ByteBuffer buffer) throws IOException
Description copied from interface:IRandomAccessReads up to buffer.capacity() bytes of data from this stream into a ByteBuffer.- Specified by:
readin interfaceIRandomAccess- Parameters:
buffer- 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 buffer, int off, int len) throws IOException
Description copied from interface:IRandomAccessReads up to len bytes of data from this stream into a ByteBuffer.- Specified by:
readin interfaceIRandomAccess- Parameters:
buffer- 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
public void seek(long pos) throws IOExceptionDescription copied from interface:IRandomAccessSets the stream pointer offset, measured from the beginning of this stream, at which the next read or write occurs.- Specified by:
seekin interfaceIRandomAccess- 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:
IRandomAccess.getFilePointer()
-
write
public void write(ByteBuffer buf) throws IOException
Description copied from interface:IRandomAccessWrites up to buffer.capacity() bytes of data from the given ByteBuffer to this stream.- Specified by:
writein interfaceIRandomAccess- 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:IRandomAccessWrites up to len bytes of data from the given ByteBuffer to this stream.- Specified by:
writein interfaceIRandomAccess- 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
-
readBoolean
public boolean readBoolean() throws IOException- Specified by:
readBooleanin interfaceDataInput- Throws:
IOException
-
readByte
public byte readByte() throws IOException- Specified by:
readBytein interfaceDataInput- Throws:
IOException
-
readChar
public char readChar() throws IOException- Specified by:
readCharin interfaceDataInput- Throws:
IOException
-
readDouble
public double readDouble() throws IOException- Specified by:
readDoublein interfaceDataInput- Throws:
IOException
-
readFloat
public float readFloat() throws IOException- Specified by:
readFloatin interfaceDataInput- Throws:
IOException
-
readFully
public void readFully(byte[] b) throws IOException- Specified by:
readFullyin interfaceDataInput- Throws:
IOException
-
readFully
public void readFully(byte[] b, int off, int len) throws IOException- Specified by:
readFullyin interfaceDataInput- Throws:
IOException
-
readInt
public int readInt() throws IOException- Specified by:
readIntin interfaceDataInput- Throws:
IOException
-
readLine
public String readLine() throws IOException
- Specified by:
readLinein interfaceDataInput- Throws:
IOException
-
readLong
public long readLong() throws IOException- Specified by:
readLongin interfaceDataInput- Throws:
IOException
-
readShort
public short readShort() throws IOException- Specified by:
readShortin interfaceDataInput- Throws:
IOException
-
readUnsignedByte
public int readUnsignedByte() throws IOException- Specified by:
readUnsignedBytein interfaceDataInput- Throws:
IOException
-
readUnsignedShort
public int readUnsignedShort() throws IOException- Specified by:
readUnsignedShortin interfaceDataInput- Throws:
IOException
-
readUTF
public String readUTF() throws IOException
- Specified by:
readUTFin interfaceDataInput- Throws:
IOException
-
skipBytes
public int skipBytes(int n) throws IOException- Specified by:
skipBytesin interfaceDataInput- Throws:
IOException
-
skipBytes
public long skipBytes(long n) throws IOExceptionDescription copied from interface:IRandomAccessAlongvariant ofDataInput.skipBytes(int).- Specified by:
skipBytesin interfaceIRandomAccess- 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- Specified by:
writein interfaceDataOutput- Throws:
IOException
-
write
public void write(byte[] b, int off, int len) throws IOException- Specified by:
writein interfaceDataOutput- Throws:
IOException
-
write
public void write(int b) throws IOException- Specified by:
writein interfaceDataOutput- Throws:
IOException
-
writeBoolean
public void writeBoolean(boolean v) throws IOException- Specified by:
writeBooleanin interfaceDataOutput- Throws:
IOException
-
writeByte
public void writeByte(int v) throws IOException- Specified by:
writeBytein interfaceDataOutput- Throws:
IOException
-
writeBytes
public void writeBytes(String s) throws IOException
- Specified by:
writeBytesin interfaceDataOutput- Throws:
IOException
-
writeChar
public void writeChar(int v) throws IOException- Specified by:
writeCharin interfaceDataOutput- Throws:
IOException
-
writeChars
public void writeChars(String s) throws IOException
- Specified by:
writeCharsin interfaceDataOutput- Throws:
IOException
-
writeDouble
public void writeDouble(double v) throws IOException- Specified by:
writeDoublein interfaceDataOutput- Throws:
IOException
-
writeFloat
public void writeFloat(float v) throws IOException- Specified by:
writeFloatin interfaceDataOutput- Throws:
IOException
-
writeInt
public void writeInt(int v) throws IOException- Specified by:
writeIntin interfaceDataOutput- Throws:
IOException
-
writeLong
public void writeLong(long v) throws IOException- Specified by:
writeLongin interfaceDataOutput- Throws:
IOException
-
writeShort
public void writeShort(int v) throws IOException- Specified by:
writeShortin interfaceDataOutput- Throws:
IOException
-
writeUTF
public void writeUTF(String str) throws IOException
- Specified by:
writeUTFin interfaceDataOutput- Throws:
IOException
-
getOrder
public ByteOrder getOrder()
Description copied from interface:IRandomAccessReturns the current order (endianness) of the stream.- Specified by:
getOrderin interfaceIRandomAccess- Returns:
- See above.
-
setOrder
public void setOrder(ByteOrder order)
Description copied from interface:IRandomAccessSets the byte order (endianness) of the stream.- Specified by:
setOrderin interfaceIRandomAccess- Parameters:
order- Order to set.
-
-