public interface ByteSource
Source for reading bytes (and other data types). This is an interface that can be implemented by any class that can provide a stream of data, regardless of where the data comes from. It allows for reading a block of heterogeneous data in a single "transaction", meaning that either the complete block of data is read, or none of it is read and the data stays available for later read operations.
If data is read outside of an explicit transaction, each get operation is considered as an individual implicit transaction. If the get operation fails, no data will have been read (and the position within the source will not have changed). If it succeeds, all data will have been read.
This interface can be used in a similar way as a ByteBuffer
, however,
it is more flexible, because it does not have to be backed by a single
object.
In general, objects implementing this interface should be considered not thread-safe and if multiple threads access them, the access must be synchronized.
Modifier and Type | Interface and Description |
---|---|
static interface |
ByteSource.AtomicGetOperation<T>
Interface for use with the
atomicGet(AtomicGetOperation) method. |
Modifier and Type | Method and Description |
---|---|
<T> T |
atomicGet(ByteSource.AtomicGetOperation<T> atomicGetOperation)
Starts an atomic get operation.
|
byte |
getByte()
Reads a single byte and advances the current position.
|
ByteSource |
getByteArray(byte[] destination,
int offset,
int length)
Reads the specified number of bytes into the supplied array and advances
the current position.
|
byte[] |
getByteArray(int length)
Reads the specified number of bytes and advances the current position.
|
double |
getDouble()
Reads a single double and advances the current position.
|
ByteSource |
getDoubleArray(double[] destination,
int offset,
int length)
Reads the specified number of double values into the supplied array and
advances the current position.
|
double[] |
getDoubleArray(int length)
Reads the specified number of doubles and advances the current position.
|
float |
getFloat()
Reads a single float and advances the current position.
|
ByteSource |
getFloatArray(float[] destination,
int offset,
int length)
Reads the specified number of float values into the supplied array and
advances the current position.
|
float[] |
getFloatArray(int length)
Reads the specified number of floats and advances the current position.
|
int |
getInt()
Reads a single int and advances the current position.
|
int[] |
getIntArray(int length)
Reads the specified number of ints and advances the current position.
|
ByteSource |
getIntArray(int[] destination,
int offset,
int length)
Reads the specified number of int values into the supplied array and
advances the current position.
|
long |
getLong()
Reads a single long and advances the current position.
|
long[] |
getLongArray(int length)
Reads the specified number of longs and advances the current position.
|
ByteSource |
getLongArray(long[] destination,
int offset,
int length)
Reads the specified number of long values into the supplied array and
advances the current position.
|
short |
getShort()
Reads a single short and advances the current position.
|
short[] |
getShortArray(int length)
Reads the specified number of shorts and advances the current position.
|
ByteSource |
getShortArray(short[] destination,
int offset,
int length)
Reads the specified number of short values into the supplied array and
advances the current position.
|
int |
remaining()
Returns the number of bytes remaining in this source.
|
ByteSource |
skip(int length)
Skips the specified number of bytes.
|
<T> T atomicGet(ByteSource.AtomicGetOperation<T> atomicGetOperation)
ByteSource.AtomicGetOperation.get()
method of
atomicGetOperation
throws an exception) all data read within
the transaction is kept and this byte source's position is reset to the
position before the transaction started. This method will throw any
exception thrown by ByteSource.AtomicGetOperation.get()
. Atomic get
operations may be nested (see ByteSource.AtomicGetOperation.get()
for
details). Despite its name, this method should not be considered to be
thread-safe.T
- type of the return value of the atomic operation.atomicGetOperation
- get operation that shall be performed atomically.ByteSource.AtomicGetOperation.get()
.int remaining()
BufferUnderflowException
is thrown.ByteSource skip(int length)
length
- number of bytes to be skipped.java.nio.BufferUnderflowException
- if there are not enough remaining bytes. The current position
is not changed.java.lang.IllegalArgumentException
- if length
is negative. The current position is
not changed.byte getByte()
java.nio.BufferUnderflowException
- if there are no remaining bytes. The current position is not
changed.byte[] getByteArray(int length)
this.getByteArray(new byte[length], 0, length)
.length
- number of bytes to be read.java.nio.BufferUnderflowException
- if there are not enough remaining bytes. The current position
is not changed.java.lang.IllegalArgumentException
- if length
is negative. The current position is
not changed.ByteSource getByteArray(byte[] destination, int offset, int length)
destination
- array into which the read data is copied. This array's length
must be at least length + destinationOffset
.offset
- offset into the destination array. The destination array is
filled starting at this position.length
- number of bytes to be read.java.nio.BufferUnderflowException
- if there are not enough remaining bytes. The current position
is not changed.java.lang.IndexOutOfBoundsException
- if the destination array is too small. The current position
is not changed.java.lang.IllegalArgumentException
- if offset
, length
or
destinationOffset
is negative. The current
position is not changed.double getDouble()
java.nio.BufferUnderflowException
- if there are not enough remaining bytes. The current position
is not changed.double[] getDoubleArray(int length)
this.getDoubleArray(new double[length], 0, length)
.length
- number of doubles to be read.java.nio.BufferUnderflowException
- if there are not enough remaining bytes. The current position
is not changed.java.lang.IllegalArgumentException
- if length
is negative. The current position is
not changed.ByteSource getDoubleArray(double[] destination, int offset, int length)
destination
- array into which the read values are copied. This array's
length must be at least
length + destinationOffset
.offset
- offset into the destination array. The destination array is
filled starting at this position.length
- number of values to be read.java.nio.BufferUnderflowException
- if there are not enough remaining bytes. The current position
is not changed.java.lang.IndexOutOfBoundsException
- if the destination array is too small. The current position
is not changed.java.lang.IllegalArgumentException
- if offset
, length
or
destinationOffset
is negative. The current
position is not changed.float getFloat()
java.nio.BufferUnderflowException
- if there are not enough remaining bytes. The current position
is not changed.float[] getFloatArray(int length)
this.getFloatArray(new float[length], 0, length)
.length
- number of floats to be read.java.nio.BufferUnderflowException
- if there are not enough remaining bytes. The current position
is not changed.java.lang.IllegalArgumentException
- if length
is negative. The current position is
not changed.ByteSource getFloatArray(float[] destination, int offset, int length)
destination
- array into which the read values are copied. This array's
length must be at least
length + destinationOffset
.offset
- offset into the destination array. The destination array is
filled starting at this position.length
- number of values to be read.java.nio.BufferUnderflowException
- if there are not enough remaining bytes. The current position
is not changed.java.lang.IndexOutOfBoundsException
- if the destination array is too small. The current position
is not changed.java.lang.IllegalArgumentException
- if offset
, length
or
destinationOffset
is negative. The current
position is not changed.int getInt()
java.nio.BufferUnderflowException
- if there are not enough remaining bytes. The current position
is not changed.int[] getIntArray(int length)
this.getIntArray(new int[length], 0, length)
.length
- number of ints to be read.java.nio.BufferUnderflowException
- if there are not enough remaining bytes. The current position
is not changed.java.lang.IllegalArgumentException
- if length
is negative. The current position is
not changed.ByteSource getIntArray(int[] destination, int offset, int length)
destination
- array into which the read values are copied. This array's
length must be at least
length + destinationOffset
.offset
- offset into the destination array. The destination array is
filled starting at this position.length
- number of values to be read.java.nio.BufferUnderflowException
- if there are not enough remaining bytes. The current position
is not changed.java.lang.IndexOutOfBoundsException
- if the destination array is too small. The current position
is not changed.java.lang.IllegalArgumentException
- if offset
, length
or
destinationOffset
is negative. The current
position is not changed.long getLong()
java.nio.BufferUnderflowException
- if there are not enough remaining bytes. The current position
is not changed.long[] getLongArray(int length)
this.getLongArray(new long[length], 0, length)
.length
- number of longs to be read.java.nio.BufferUnderflowException
- if there are not enough remaining bytes. The current position
is not changed.java.lang.IllegalArgumentException
- if length
is negative. The current position is
not changed.ByteSource getLongArray(long[] destination, int offset, int length)
destination
- array into which the read values are copied. This array's
length must be at least
length + destinationOffset
.offset
- offset into the destination array. The destination array is
filled starting at this position.length
- number of values to be read.java.nio.BufferUnderflowException
- if there are not enough remaining bytes. The current position
is not changed.java.lang.IndexOutOfBoundsException
- if the destination array is too small. The current position
is not changed.java.lang.IllegalArgumentException
- if offset
, length
or
destinationOffset
is negative. The current
position is not changed.short getShort()
java.nio.BufferUnderflowException
- if there are not enough remaining bytes. The current position
is not changed.short[] getShortArray(int length)
this.getShortArray(new short[length], 0, length)
.length
- number of shorts to be read.java.nio.BufferUnderflowException
- if there are not enough remaining bytes. The current position
is not changed.java.lang.IllegalArgumentException
- if length
is negative. The current position is
not changed.ByteSource getShortArray(short[] destination, int offset, int length)
destination
- array into which the read values are copied. This array's
length must be at least
length + destinationOffset
.offset
- offset into the destination array. The destination array is
filled starting at this position.length
- number of values to be read.java.nio.BufferUnderflowException
- if there are not enough remaining bytes. The current position
is not changed.java.lang.IndexOutOfBoundsException
- if the destination array is too small. The current position
is not changed.java.lang.IllegalArgumentException
- if offset
, length
or
destinationOffset
is negative. The current
position is not changed.Copyright © 2014–2018 aquenos GmbH. All rights reserved.