Package loci.common
Class StreamHandle
java.lang.Object
loci.common.StreamHandle
- All Implemented Interfaces:
DataInput,DataOutput,IRandomAccess
- Direct Known Subclasses:
BZip2Handle,GZipHandle,S3Handle,URLHandle,ZipHandle
Abstract IRandomAccess implementation for reading from InputStreams and
writing to OutputStreams.
- Author:
- Melissa Linkert melissa at glencoesoftware.com
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected StringName of the open stream.protected longCurrent position within the stream.protected longLength of the stream.protected longMarked position within the stream.protected ByteOrderByte ordering of this stream.protected DataOutputStreamOutputStream to be used for writing.protected DataInputStreamInputStream to be used for reading. -
Constructor Summary
Constructors -
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()protected abstract voidClose and reopen the stream; the stream pointer and mark should be reset to 0.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
-
file
Name of the open stream. -
stream
InputStream to be used for reading. -
outStream
OutputStream to be used for writing. -
length
protected long lengthLength of the stream. -
fp
protected long fpCurrent position within the stream. -
mark
protected long markMarked position within the stream. -
order
Byte ordering of this stream.
-
-
Constructor Details
-
StreamHandle
public StreamHandle()Construct a new StreamHandle. The file pointer will be set to 0, and the byte ordering will be big-endian.
-
-
Method Details
-
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
-
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.
-
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
-
resetStream
Close and reopen the stream; the stream pointer and mark should be reset to 0. This method is called if we need to seek backwards within the stream.- Throws:
IOException- if the stream cannot be reset
-