public final class ByteSequenceReader extends Object
ByteSequence
.
ByteSequenceReader
must be created using the associated
ByteSequence
's asReader()
method.Modifier and Type | Method and Description |
---|---|
InputStream |
asInputStream()
Returns an
InputStream from the current position in the sequence. |
byte |
peek()
Returns the byte situated at the current position.
|
byte |
peek(int offset)
Returns the byte situated at the given offset from current position.
|
int |
position()
Returns this reader's position.
|
void |
position(int pos)
Sets this reader's position.
|
int |
readBerLength()
Relative read method for reading a multi-byte BER length.
|
byte |
readByte()
Relative read method.
|
void |
readBytes(byte[] b)
Relative bulk read method.
|
void |
readBytes(byte[] b,
int offset,
int length)
Relative bulk read method.
|
ByteSequence |
readByteSequence(int length)
Relative bulk read method.
|
ByteString |
readByteString(int length)
Relative bulk read method.
|
int |
readCompactUnsignedInt()
Relative read method for reading a compacted int value.
|
long |
readCompactUnsignedLong()
Relative read method for reading a compacted long value.
|
int |
readInt()
Relative read method for reading an integer value.
|
long |
readLong()
Relative read method for reading a long value.
|
short |
readShort()
Relative read method for reading an short value.
|
String |
readStringUtf8(int length)
Relative read method for reading a UTF-8 encoded string.
|
int |
remaining()
Returns the number of bytes between the current position and the end of
the underlying byte sequence.
|
void |
rewind()
Rewinds this reader's position to zero.
|
void |
skip(int length)
Skips the given number of bytes.
|
String |
toString() |
public byte readByte()
IndexOutOfBoundsException
- If there are fewer bytes remaining in this reader than are
required to satisfy the request, that is, if
remaining() < 1
.public void readBytes(byte[] b)
src.readBytes(b);Behaves in exactly the same way as the invocation:
src.readBytes(b, 0, b.length);
b
- The byte array into which bytes are to be written.IndexOutOfBoundsException
- If there are fewer bytes remaining in this reader than are
required to satisfy the request, that is, if
remaining() < b.length
.public void readBytes(byte[] b, int offset, int length)
length
bytes from this reader
into the given array, starting at the current position of this reader and
at the given offset
in the array. The position of this reader is
then incremented by length
. In other words, an invocation of this
method of the form:
src.read(b, offset, length);Has exactly the same effect as the loop:
for (int i = offset; i < offset + length; i++) b[i] = src.readByte();Except that it first checks that there are sufficient bytes in this buffer and it is potentially much more efficient.
b
- The byte array into which bytes are to be written.offset
- The offset within the array of the first byte to be written;
must be non-negative and no larger than b.length
.length
- The number of bytes to be written to the given array; must be
non-negative and no larger than b.length
.IndexOutOfBoundsException
- If there are fewer bytes remaining in this reader than are
required to satisfy the request, that is, if
remaining() < length
.public int readBerLength()
IndexOutOfBoundsException
- If there are fewer bytes remaining in this reader than are
required to satisfy the request.public ByteSequence readByteSequence(int length)
ByteSequence
whose content is
the next length
bytes from this reader, starting at the current
position of this reader. The position of this reader is then incremented
by length
.
NOTE: The value returned from this method should NEVER be cached as it prevents the contents of the underlying byte stream from being garbage collected.
length
- The length of the byte sequence to be returned.length
bytes
from this reader.IndexOutOfBoundsException
- If there are fewer bytes remaining in this reader than are
required to satisfy the request, that is, if
remaining() < length
.public ByteString readByteString(int length)
ByteString
whose content is
the next length
bytes from this reader, starting at the current
position of this reader. The position of this reader is then incremented
by length
.
An invocation of this method of the form:
src.readByteString(length);Has exactly the same effect as:
src.readByteSequence(length).toByteString();NOTE: The value returned from this method should NEVER be cached as it prevents the contents of the underlying byte stream from being garbage collected.
length
- The length of the byte string to be returned.length
bytes
from this reader.IndexOutOfBoundsException
- If there are fewer bytes remaining in this reader than are
required to satisfy the request, that is, if
remaining() < length
.public int readInt()
IndexOutOfBoundsException
- If there are fewer bytes remaining in this reader than are
required to satisfy the request, that is, if
remaining() < 4
.public long readLong()
IndexOutOfBoundsException
- If there are fewer bytes remaining in this reader than are
required to satisfy the request, that is, if
remaining() < 8
.public long readCompactUnsignedLong()
< 128
, value will be encoded using one byte only).
Reads the next bytes at this reader's current position, composing them into a long value
according to big-endian byte order, and then increments the position by the size of the
encoded long.
Note that the maximum value of a compact long is 2^56.IndexOutOfBoundsException
- If there are fewer bytes remaining in this reader than are
required to satisfy the request.public int readCompactUnsignedInt()
< 128
, value will be encoded using one byte only).
Reads the next bytes at this reader's current position, composing them into an int value
according to big-endian byte order, and then increments the position by the size of the
encoded int.IndexOutOfBoundsException
- If there are fewer bytes remaining in this reader than are
required to satisfy the request.public short readShort()
IndexOutOfBoundsException
- If there are fewer bytes remaining in this reader than are
required to satisfy the request, that is, if
remaining() < 2
.public String readStringUtf8(int length)
length
- The number of bytes to read and decode.IndexOutOfBoundsException
- If there are fewer bytes remaining in this reader than are
required to satisfy the request, that is, if
remaining() < length
.public int position()
public void position(int pos)
pos
- The new position value; must be non-negative and no larger
than the length of the underlying byte sequence.IndexOutOfBoundsException
- If the position is negative or larger than the length of the
underlying byte sequence.public int remaining()
public void rewind()
An invocation of this method of the form:
src.rewind();Has exactly the same effect as:
src.position(0);
public byte peek()
IndexOutOfBoundsException
- If the position is negative or larger than the length of the
underlying byte sequence.public byte peek(int offset)
offset
- The offset where to look at from current position.IndexOutOfBoundsException
- If the position is negative or larger than the length of the
underlying byte sequence.public void skip(int length)
An invocation of this method of the form:
src.skip(length);Has exactly the same effect as:
src.position(position() + length);
length
- The number of bytes to skip.IndexOutOfBoundsException
- If the new position is less than 0 or greater than the length
of the underlying byte sequence.public InputStream asInputStream()
InputStream
from the current position in the sequence.
There is only a single InputStream
for a given ByteSequence, so
multiple calls to asInputStream()
will always return the same object.
The returned InputStream
does not support mark()
.
Calling close()
does nothing.InputStream
from the current position in the sequenceCopyright 2010-2022 ForgeRock AS.