Package loci.common
Class FileHandle
java.lang.Object
loci.common.FileHandle
- All Implemented Interfaces:
DataInput,DataOutput,IRandomAccess
A wrapper for RandomAccessFile that implements the IRandomAccess interface.
- Author:
- Curtis Rueden ctrueden at wisc.edu
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected RandomAccessFileThe random access file object backing this FileHandle. -
Constructor Summary
ConstructorsConstructorDescriptionFileHandle(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
Modifier and TypeMethodDescriptionvoidclose()Closes this random access stream and releases any system resources associated with the stream.booleanexists()Returns whether this refers to a valid objectlongReturns 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 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.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.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) void
-
Field Details
-
raf
The random access file object backing this FileHandle.
-
-
Constructor Details
-
FileHandle
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
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 Details
-
getRandomAccessFile
- Returns:
- the
RandomAccessFileobject backing this FileHandle.
-
close
Description 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
Description 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
Description 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
Description 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
Description 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
Description 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
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
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
Description 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:
-
write
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
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
- Specified by:
readBooleanin interfaceDataInput- Throws:
IOException
-
readByte
- Specified by:
readBytein interfaceDataInput- Throws:
IOException
-
readChar
- Specified by:
readCharin interfaceDataInput- Throws:
IOException
-
readDouble
- Specified by:
readDoublein interfaceDataInput- Throws:
IOException
-
readFloat
- Specified by:
readFloatin interfaceDataInput- Throws:
IOException
-
readFully
- Specified by:
readFullyin interfaceDataInput- Throws:
IOException
-
readFully
- Specified by:
readFullyin interfaceDataInput- Throws:
IOException
-
readInt
- Specified by:
readIntin interfaceDataInput- Throws:
IOException
-
readLine
- Specified by:
readLinein interfaceDataInput- Throws:
IOException
-
readLong
- Specified by:
readLongin interfaceDataInput- Throws:
IOException
-
readShort
- Specified by:
readShortin interfaceDataInput- Throws:
IOException
-
readUnsignedByte
- Specified by:
readUnsignedBytein interfaceDataInput- Throws:
IOException
-
readUnsignedShort
- Specified by:
readUnsignedShortin interfaceDataInput- Throws:
IOException
-
readUTF
- Specified by:
readUTFin interfaceDataInput- Throws:
IOException
-
skipBytes
- Specified by:
skipBytesin interfaceDataInput- Throws:
IOException
-
skipBytes
Description copied from interface:IRandomAccessAlongvariant ofIRandomAccess.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
- Specified by:
writein interfaceDataOutput- Throws:
IOException
-
write
- Specified by:
writein interfaceDataOutput- Throws:
IOException
-
write
- Specified by:
writein interfaceDataOutput- Throws:
IOException
-
writeBoolean
- Specified by:
writeBooleanin interfaceDataOutput- Throws:
IOException
-
writeByte
- Specified by:
writeBytein interfaceDataOutput- Throws:
IOException
-
writeBytes
- Specified by:
writeBytesin interfaceDataOutput- Throws:
IOException
-
writeChar
- Specified by:
writeCharin interfaceDataOutput- Throws:
IOException
-
writeChars
- Specified by:
writeCharsin interfaceDataOutput- Throws:
IOException
-
writeDouble
- Specified by:
writeDoublein interfaceDataOutput- Throws:
IOException
-
writeFloat
- Specified by:
writeFloatin interfaceDataOutput- Throws:
IOException
-
writeInt
- Specified by:
writeIntin interfaceDataOutput- Throws:
IOException
-
writeLong
- Specified by:
writeLongin interfaceDataOutput- Throws:
IOException
-
writeShort
- Specified by:
writeShortin interfaceDataOutput- Throws:
IOException
-
writeUTF
- Specified by:
writeUTFin interfaceDataOutput- Throws:
IOException
-
getOrder
Description copied from interface:IRandomAccessReturns the current order (endianness) of the stream.- Specified by:
getOrderin interfaceIRandomAccess- Returns:
- See above.
-
setOrder
Description copied from interface:IRandomAccessSets the byte order (endianness) of the stream.- Specified by:
setOrderin interfaceIRandomAccess- Parameters:
order- Order to set.
-