Package loci.common
Class ByteArrayHandle
java.lang.Object
loci.common.AbstractNIOHandle
loci.common.ByteArrayHandle
- All Implemented Interfaces:
java.io.DataInput
,java.io.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 java.nio.ByteBuffer
buffer
Backing ByteBuffer.protected static int
INITIAL_LENGTH
Initial length of a new file. -
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(java.nio.ByteBuffer bytes)
Creates a random access byte stream to read from, and write to, the supplied ByteBuffer. -
Method Summary
Modifier and Type Method Description void
close()
Closes this random access stream and releases any system resources associated with the stream.java.nio.ByteBuffer
getByteBuffer()
Gets the byte buffer backing this handle.byte[]
getBytes()
Gets a byte array representing the current state of this ByteArrayHandle.long
getFilePointer()
Returns the current offset in this stream.java.nio.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(java.nio.ByteBuffer buf)
Reads up to buffer.capacity() bytes of data from this stream into a ByteBuffer.int
read(java.nio.ByteBuffer buf, 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()
java.lang.String
readLine()
long
readLong()
short
readShort()
int
readUnsignedByte()
int
readUnsignedShort()
java.lang.String
readUTF()
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
setLength(long length)
Sets the new length of the handle.void
setOrder(java.nio.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(java.nio.ByteBuffer buf)
Writes up to buffer.capacity() bytes of data from the given ByteBuffer to this stream.void
write(java.nio.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(java.lang.String s)
void
writeChar(int v)
void
writeChars(java.lang.String s)
void
writeDouble(double v)
void
writeFloat(float v)
void
writeInt(int v)
void
writeLong(long v)
void
writeShort(int v)
void
writeUTF(java.lang.String str)
-
Field Details
-
INITIAL_LENGTH
protected static final int INITIAL_LENGTHInitial length of a new file.- See Also:
- Constant Field Values
-
buffer
protected java.nio.ByteBuffer bufferBacking ByteBuffer.
-
-
Constructor Details
-
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(java.nio.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 Details
-
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 java.nio.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 java.io.IOExceptionDescription copied from class:AbstractNIOHandle
Sets the new length of the handle.- Specified by:
setLength
in classAbstractNIOHandle
- Parameters:
length
- New length.- Throws:
java.io.IOException
- If there is an error changing the handle's length.
-
close
public void close()Description copied from interface:IRandomAccess
Closes this random access stream and releases any system resources associated with the stream. -
getFilePointer
public long getFilePointer()Description copied from interface:IRandomAccess
Returns 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:IRandomAccess
Returns the length of this stream.- Returns:
- the length in bytes of the stream
-
read
public int read(byte[] b) throws java.io.IOExceptionDescription copied from interface:IRandomAccess
Reads 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:
java.io.IOException
- if reading is not possible
-
read
public int read(byte[] b, int off, int len) throws java.io.IOExceptionDescription copied from interface:IRandomAccess
Reads 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 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:
java.io.IOException
- if reading is not possible
-
read
public int read(java.nio.ByteBuffer buf) throws java.io.IOExceptionDescription copied from interface:IRandomAccess
Reads 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:
java.io.IOException
- if reading is not possible
-
read
public int read(java.nio.ByteBuffer buf, int off, int len) throws java.io.IOExceptionDescription copied from interface:IRandomAccess
Reads up to len bytes of data from this stream into a ByteBuffer.- Parameters:
buf
- 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:
java.io.IOException
- if reading is not possible
-
seek
public void seek(long pos) throws java.io.IOExceptionDescription copied from interface:IRandomAccess
Sets 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:
java.io.IOException
- ifpos
is invalid or the seek fails- See Also:
IRandomAccess.getFilePointer()
-
getOrder
public java.nio.ByteOrder getOrder()Description copied from interface:IRandomAccess
Returns the current order (endianness) of the stream.- Returns:
- See above.
-
setOrder
public void setOrder(java.nio.ByteOrder order)Description copied from interface:IRandomAccess
Sets the byte order (endianness) of the stream.- Parameters:
order
- Order to set.
-
readBoolean
public boolean readBoolean() throws java.io.IOException- Throws:
java.io.IOException
-
readByte
public byte readByte() throws java.io.IOException- Throws:
java.io.IOException
-
readChar
public char readChar() throws java.io.IOException- Throws:
java.io.IOException
-
readDouble
public double readDouble() throws java.io.IOException- Throws:
java.io.IOException
-
readFloat
public float readFloat() throws java.io.IOException- Throws:
java.io.IOException
-
readFully
public void readFully(byte[] b) throws java.io.IOException- Throws:
java.io.IOException
-
readFully
public void readFully(byte[] b, int off, int len) throws java.io.IOException- Throws:
java.io.IOException
-
readInt
public int readInt() throws java.io.IOException- Throws:
java.io.IOException
-
readLine
public java.lang.String readLine() throws java.io.IOException- Throws:
java.io.IOException
-
readLong
public long readLong() throws java.io.IOException- Throws:
java.io.IOException
-
readShort
public short readShort() throws java.io.IOException- Throws:
java.io.IOException
-
readUnsignedByte
public int readUnsignedByte() throws java.io.IOException- Throws:
java.io.IOException
-
readUnsignedShort
public int readUnsignedShort() throws java.io.IOException- Throws:
java.io.IOException
-
readUTF
public java.lang.String readUTF() throws java.io.IOException- Throws:
java.io.IOException
-
skipBytes
public int skipBytes(int n) throws java.io.IOException- Throws:
java.io.IOException
-
skipBytes
public long skipBytes(long n) throws java.io.IOExceptionDescription copied from interface:IRandomAccess
Along
variant ofDataInput.skipBytes(int)
.- Parameters:
n
- the number of bytes to skip- Returns:
- the number of bytes skipped
- Throws:
java.io.IOException
- if the operation failed
-
write
public void write(byte[] b) throws java.io.IOException- Throws:
java.io.IOException
-
write
public void write(byte[] b, int off, int len) throws java.io.IOException- Throws:
java.io.IOException
-
write
public void write(java.nio.ByteBuffer buf) throws java.io.IOExceptionDescription copied from interface:IRandomAccess
Writes 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:
java.io.IOException
- if writing is not possible
-
write
public void write(java.nio.ByteBuffer buf, int off, int len) throws java.io.IOExceptionDescription copied from interface:IRandomAccess
Writes 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 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:
java.io.IOException
- if writing is not possible
-
write
public void write(int b) throws java.io.IOException- Throws:
java.io.IOException
-
writeBoolean
public void writeBoolean(boolean v) throws java.io.IOException- Throws:
java.io.IOException
-
writeByte
public void writeByte(int v) throws java.io.IOException- Throws:
java.io.IOException
-
writeBytes
public void writeBytes(java.lang.String s) throws java.io.IOException- Throws:
java.io.IOException
-
writeChar
public void writeChar(int v) throws java.io.IOException- Throws:
java.io.IOException
-
writeChars
public void writeChars(java.lang.String s) throws java.io.IOException- Throws:
java.io.IOException
-
writeDouble
public void writeDouble(double v) throws java.io.IOException- Throws:
java.io.IOException
-
writeFloat
public void writeFloat(float v) throws java.io.IOException- Throws:
java.io.IOException
-
writeInt
public void writeInt(int v) throws java.io.IOException- Throws:
java.io.IOException
-
writeLong
public void writeLong(long v) throws java.io.IOException- Throws:
java.io.IOException
-
writeShort
public void writeShort(int v) throws java.io.IOException- Throws:
java.io.IOException
-
writeUTF
public void writeUTF(java.lang.String str) throws java.io.IOException- Throws:
java.io.IOException
-