Package loci.common
Class ByteArrayHandle
- java.lang.Object
-
- loci.common.AbstractNIOHandle
-
- loci.common.ByteArrayHandle
-
- All Implemented Interfaces:
DataInput,DataOutput,IRandomAccess
public class ByteArrayHandle extends AbstractNIOHandle
A wrapper for a byte array that implements the IRandomAccess interface.- See Also:
IRandomAccess
-
-
Field Summary
Fields Modifier and Type Field Description protected ByteBufferbufferBacking ByteBuffer.protected static intINITIAL_LENGTHInitial length of a new file.-
Fields inherited from class loci.common.AbstractNIOHandle
EOF_ERROR_MSG
-
-
Constructor Summary
Constructors Constructor Description ByteArrayHandle()Creates a random access byte stream to write to a byte array.ByteArrayHandle(byte[] bytes)Creates a random access byte stream to read from, and write to, the bytes specified by the byte[] argument.ByteArrayHandle(int capacity)Creates a random access byte stream to read from, and write to.ByteArrayHandle(ByteBuffer bytes)Creates a random access byte stream to read from, and write to, the supplied ByteBuffer.
-
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.ByteBuffergetByteBuffer()Gets the byte buffer backing this handle.byte[]getBytes()Gets a byte array representing the current state of this ByteArrayHandle.longgetFilePointer()Returns the current offset in this stream.ByteOrdergetOrder()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 buf)Reads up to buffer.capacity() bytes of data from this stream into a ByteBuffer.intread(ByteBuffer buf, 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.voidsetLength(long length)Sets the new length of the handle.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)-
Methods inherited from class loci.common.AbstractNIOHandle
exists, validateLength, validateMode
-
-
-
-
Field Detail
-
INITIAL_LENGTH
protected static final int INITIAL_LENGTH
Initial length of a new file.- See Also:
- Constant Field Values
-
buffer
protected ByteBuffer buffer
Backing ByteBuffer.
-
-
Constructor Detail
-
ByteArrayHandle
public ByteArrayHandle(byte[] bytes)
Creates a random access byte stream to read from, and write to, the bytes specified by the byte[] argument.- Parameters:
bytes- byte array to work with
-
ByteArrayHandle
public ByteArrayHandle(ByteBuffer bytes)
Creates a random access byte stream to read from, and write to, the supplied ByteBuffer.- Parameters:
bytes- ByteBuffer used for reading and writing
-
ByteArrayHandle
public ByteArrayHandle(int capacity)
Creates a random access byte stream to read from, and write to.- Parameters:
capacity- Number of bytes to initially allocate.
-
ByteArrayHandle
public ByteArrayHandle()
Creates a random access byte stream to write to a byte array.
-
-
Method Detail
-
getBytes
public byte[] getBytes()
Gets a byte array representing the current state of this ByteArrayHandle.- Returns:
- a byte array representation of the backing ByteBuffer
-
getByteBuffer
public ByteBuffer getByteBuffer()
Gets the byte buffer backing this handle. NOTE: This is the backing buffer. Any modifications to this buffer including position, length and capacity will affect subsequent calls upon its source handle.- Returns:
- Backing buffer of this handle.
-
setLength
public void setLength(long length) throws IOExceptionDescription copied from class:AbstractNIOHandleSets the new length of the handle.- Specified by:
setLengthin classAbstractNIOHandle- Parameters:
length- New length.- Throws:
IOException- If there is an error changing the handle's length.
-
close
public void close()
Description copied from interface:IRandomAccessCloses this random access stream and releases any system resources associated with the stream.
-
getFilePointer
public long getFilePointer()
Description copied from interface:IRandomAccessReturns 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()
Description copied from interface:IRandomAccessReturns the length of this stream.- Returns:
- the length in bytes of the stream
-
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.- 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.- 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 buf) throws IOException
Description copied from interface:IRandomAccessReads 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:IRandomAccessReads up to len bytes of data from this stream into a ByteBuffer.- Parameters:
buf- 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.- 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()
-
getOrder
public ByteOrder getOrder()
Description copied from interface:IRandomAccessReturns the current order (endianness) of the stream.- Returns:
- See above.
-
setOrder
public void setOrder(ByteOrder order)
Description copied from interface:IRandomAccessSets the byte order (endianness) of the stream.- Parameters:
order- Order to set.
-
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 IOExceptionDescription copied from interface:IRandomAccessAlongvariant 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:IRandomAccessWrites 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:IRandomAccessWrites 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 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
-
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
-
-