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
public abstract class StreamHandle extends Object implements IRandomAccess
Abstract IRandomAccess implementation for reading from InputStreams and writing to OutputStreams.- Author:
- Melissa Linkert melissa at glencoesoftware.com
- See Also:
IRandomAccess
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
StreamHandle.Settings
-
Field Summary
Fields Modifier and Type Field Description protected String
file
Name of the open stream.protected long
fp
Current position within the stream.protected long
length
Length of the stream.protected long
mark
Marked position within the stream.protected ByteOrder
order
Byte ordering of this stream.protected DataOutputStream
outStream
OutputStream to be used for writing.protected DataInputStream
stream
InputStream to be used for reading.
-
Constructor Summary
Constructors Constructor Description StreamHandle()
Construct a new StreamHandle.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
close()
Closes this random access stream and releases any system resources associated with the stream.boolean
exists()
Returns whether this refers to a valid objectlong
getFilePointer()
Returns the current offset in this stream.ByteOrder
getOrder()
Returns the current order (endianness) of the stream.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 buffer)
Reads up to buffer.capacity() bytes of data from this stream into a ByteBuffer.int
read(ByteBuffer buffer, 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()
protected abstract void
resetStream()
Close and reopen the stream; the stream pointer and mark should be reset to 0.void
seek(long pos)
Sets the stream pointer offset, measured from the beginning of this stream, at which the next read or write occurs.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)
-
-
-
Field Detail
-
file
protected String file
Name of the open stream.
-
stream
protected DataInputStream stream
InputStream to be used for reading.
-
outStream
protected DataOutputStream outStream
OutputStream to be used for writing.
-
length
protected long length
Length of the stream.
-
fp
protected long fp
Current position within the stream.
-
mark
protected long mark
Marked position within the stream.
-
order
protected ByteOrder order
Byte ordering of this stream.
-
-
Method Detail
-
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.- Specified by:
close
in interfaceIRandomAccess
- Throws:
IOException
- if the underlying stream(s) could not be closed
-
getFilePointer
public long getFilePointer() throws IOException
Description copied from interface:IRandomAccess
Returns the current offset in this stream.- Specified by:
getFilePointer
in 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 IOException
Description copied from interface:IRandomAccess
Returns whether this refers to a valid object- Specified by:
exists
in 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 IOException
Description copied from interface:IRandomAccess
Returns the length of this stream.- Specified by:
length
in interfaceIRandomAccess
- Returns:
- the length in bytes of the stream
- Throws:
IOException
- if the length cannot be retrieved
-
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.- Specified by:
read
in 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 IOException
Description copied from interface:IRandomAccess
Reads up to len bytes of data from this stream into an array of bytes.- Specified by:
read
in interfaceIRandomAccess
- 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 buffer) throws IOException
Description copied from interface:IRandomAccess
Reads up to buffer.capacity() bytes of data from this stream into a ByteBuffer.- Specified by:
read
in 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:IRandomAccess
Reads up to len bytes of data from this stream into a ByteBuffer.- Specified by:
read
in interfaceIRandomAccess
- Parameters:
buffer
- 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.- Specified by:
seek
in 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
- ifpos
is invalid or the seek fails- See Also:
IRandomAccess.getFilePointer()
-
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.- Specified by:
write
in 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:IRandomAccess
Writes up to len bytes of data from the given ByteBuffer to this stream.- Specified by:
write
in interfaceIRandomAccess
- 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
-
getOrder
public ByteOrder getOrder()
Description copied from interface:IRandomAccess
Returns the current order (endianness) of the stream.- Specified by:
getOrder
in interfaceIRandomAccess
- Returns:
- See above.
-
setOrder
public void setOrder(ByteOrder order)
Description copied from interface:IRandomAccess
Sets the byte order (endianness) of the stream.- Specified by:
setOrder
in interfaceIRandomAccess
- Parameters:
order
- Order to set.
-
readBoolean
public boolean readBoolean() throws IOException
- Specified by:
readBoolean
in interfaceDataInput
- Throws:
IOException
-
readByte
public byte readByte() throws IOException
- Specified by:
readByte
in interfaceDataInput
- Throws:
IOException
-
readChar
public char readChar() throws IOException
- Specified by:
readChar
in interfaceDataInput
- Throws:
IOException
-
readDouble
public double readDouble() throws IOException
- Specified by:
readDouble
in interfaceDataInput
- Throws:
IOException
-
readFloat
public float readFloat() throws IOException
- Specified by:
readFloat
in interfaceDataInput
- Throws:
IOException
-
readFully
public void readFully(byte[] b) throws IOException
- Specified by:
readFully
in interfaceDataInput
- Throws:
IOException
-
readFully
public void readFully(byte[] b, int off, int len) throws IOException
- Specified by:
readFully
in interfaceDataInput
- Throws:
IOException
-
readInt
public int readInt() throws IOException
- Specified by:
readInt
in interfaceDataInput
- Throws:
IOException
-
readLine
public String readLine() throws IOException
- Specified by:
readLine
in interfaceDataInput
- Throws:
IOException
-
readLong
public long readLong() throws IOException
- Specified by:
readLong
in interfaceDataInput
- Throws:
IOException
-
readShort
public short readShort() throws IOException
- Specified by:
readShort
in interfaceDataInput
- Throws:
IOException
-
readUnsignedByte
public int readUnsignedByte() throws IOException
- Specified by:
readUnsignedByte
in interfaceDataInput
- Throws:
IOException
-
readUnsignedShort
public int readUnsignedShort() throws IOException
- Specified by:
readUnsignedShort
in interfaceDataInput
- Throws:
IOException
-
readUTF
public String readUTF() throws IOException
- Specified by:
readUTF
in interfaceDataInput
- Throws:
IOException
-
skipBytes
public int skipBytes(int n) throws IOException
- Specified by:
skipBytes
in interfaceDataInput
- Throws:
IOException
-
skipBytes
public long skipBytes(long n) throws IOException
Description copied from interface:IRandomAccess
Along
variant ofDataInput.skipBytes(int)
.- Specified by:
skipBytes
in 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:
write
in interfaceDataOutput
- Throws:
IOException
-
write
public void write(byte[] b, int off, int len) throws IOException
- Specified by:
write
in interfaceDataOutput
- Throws:
IOException
-
write
public void write(int b) throws IOException
- Specified by:
write
in interfaceDataOutput
- Throws:
IOException
-
writeBoolean
public void writeBoolean(boolean v) throws IOException
- Specified by:
writeBoolean
in interfaceDataOutput
- Throws:
IOException
-
writeByte
public void writeByte(int v) throws IOException
- Specified by:
writeByte
in interfaceDataOutput
- Throws:
IOException
-
writeBytes
public void writeBytes(String s) throws IOException
- Specified by:
writeBytes
in interfaceDataOutput
- Throws:
IOException
-
writeChar
public void writeChar(int v) throws IOException
- Specified by:
writeChar
in interfaceDataOutput
- Throws:
IOException
-
writeChars
public void writeChars(String s) throws IOException
- Specified by:
writeChars
in interfaceDataOutput
- Throws:
IOException
-
writeDouble
public void writeDouble(double v) throws IOException
- Specified by:
writeDouble
in interfaceDataOutput
- Throws:
IOException
-
writeFloat
public void writeFloat(float v) throws IOException
- Specified by:
writeFloat
in interfaceDataOutput
- Throws:
IOException
-
writeInt
public void writeInt(int v) throws IOException
- Specified by:
writeInt
in interfaceDataOutput
- Throws:
IOException
-
writeLong
public void writeLong(long v) throws IOException
- Specified by:
writeLong
in interfaceDataOutput
- Throws:
IOException
-
writeShort
public void writeShort(int v) throws IOException
- Specified by:
writeShort
in interfaceDataOutput
- Throws:
IOException
-
writeUTF
public void writeUTF(String str) throws IOException
- Specified by:
writeUTF
in interfaceDataOutput
- Throws:
IOException
-
resetStream
protected abstract void resetStream() throws IOException
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
-
-