Package org.forgerock.opendj.ldap
Class ByteSequenceReader
java.lang.Object
org.forgerock.opendj.ldap.ByteSequenceReader
An interface for iteratively reading data from a
ByteSequence .
ByteSequenceReader must be created using the associated
ByteSequence's asReader() method.-
Method Summary
Modifier and TypeMethodDescriptionReturns anInputStreamfrom the current position in the sequence.bytepeek()Returns the byte situated at the current position.bytepeek(int offset) Returns the byte situated at the given offset from current position.intposition()Returns this reader's position.voidposition(int pos) Sets this reader's position.intRelative read method for reading a multi-byte BER length.bytereadByte()Relative read method.voidreadBytes(byte[] b) Relative bulk read method.voidreadBytes(byte[] b, int offset, int length) Relative bulk read method.readByteSequence(int length) Relative bulk read method.readByteString(int length) Relative bulk read method.intRelative read method for reading a compacted int value.longRelative read method for reading a compacted long value.intreadInt()Relative read method for reading an integer value.longreadLong()Relative read method for reading a long value.shortRelative read method for reading an short value.readStringUtf8(int length) Relative read method for reading a UTF-8 encoded string.intReturns the number of bytes between the current position and the end of the underlying byte sequence.voidrewind()Rewinds this reader's position to zero.voidskip(int length) Skips the given number of bytes.toString()
-
Method Details
-
readByte
Relative read method. Reads the byte at the current position.- Returns:
- The byte at this reader's current position.
- Throws:
IndexOutOfBoundsException- If there are fewer bytes remaining in this reader than are required to satisfy the request, that is, ifremaining() < 1.
-
readBytes
Relative bulk read method. This method transfers bytes from this reader into the given destination array. An invocation of this method of the form:src.readBytes(b);
Behaves in exactly the same way as the invocation:src.readBytes(b, 0, b.length);
- Parameters:
b- The byte array into which bytes are to be written.- Throws:
IndexOutOfBoundsException- If there are fewer bytes remaining in this reader than are required to satisfy the request, that is, ifremaining() < b.length.
-
readBytes
Relative bulk read method. Copieslengthbytes from this reader into the given array, starting at the current position of this reader and at the givenoffsetin the array. The position of this reader is then incremented bylength. 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.- Parameters:
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 thanb.length.length- The number of bytes to be written to the given array; must be non-negative and no larger thanb.length.- Throws:
IndexOutOfBoundsException- If there are fewer bytes remaining in this reader than are required to satisfy the request, that is, ifremaining() < length.
-
readBERLength
Relative read method for reading a multi-byte BER length. Reads the next one to five bytes at this reader's current position, composing them into a integer value and then increments the position by the number of bytes read.- Returns:
- The integer value representing the length at this reader's current position.
- Throws:
IndexOutOfBoundsException- If there are fewer bytes remaining in this reader than are required to satisfy the request.
-
readByteSequence
Relative bulk read method. Returns aByteSequencewhose content is the nextlengthbytes from this reader, starting at the current position of this reader. The position of this reader is then incremented bylength.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.
- Parameters:
length- The length of the byte sequence to be returned.- Returns:
- The byte sequence whose content is the next
lengthbytes from this reader. - Throws:
IndexOutOfBoundsException- If there are fewer bytes remaining in this reader than are required to satisfy the request, that is, ifremaining() < length.
-
readByteString
Relative bulk read method. Returns aByteStringwhose content is the nextlengthbytes from this reader, starting at the current position of this reader. The position of this reader is then incremented bylength.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.- Parameters:
length- The length of the byte string to be returned.- Returns:
- The byte string whose content is the next
lengthbytes from this reader. - Throws:
IndexOutOfBoundsException- If there are fewer bytes remaining in this reader than are required to satisfy the request, that is, ifremaining() < length.
-
readInt
Relative read method for reading an integer value. Reads the next four bytes at this reader's current position, composing them into an integer value according to big-endian byte order, and then increments the position by four.- Returns:
- The integer value at this reader's current position.
- Throws:
IndexOutOfBoundsException- If there are fewer bytes remaining in this reader than are required to satisfy the request, that is, ifremaining() < 4.
-
readLong
Relative read method for reading a long value. Reads the next eight 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 eight.- Returns:
- The long value at this reader's current position.
- Throws:
IndexOutOfBoundsException- If there are fewer bytes remaining in this reader than are required to satisfy the request, that is, ifremaining() < 8.
-
readCompactUnsignedLong
Relative read method for reading a compacted long value. Compaction allows to reduce number of bytes needed to hold long types depending on its value (i.e: if value < 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.- Returns:
- The long value at this reader's current position.
- Throws:
IndexOutOfBoundsException- If there are fewer bytes remaining in this reader than are required to satisfy the request.
-
readCompactUnsignedInt
Relative read method for reading a compacted int value. Compaction allows to reduce number of bytes needed to hold int types depending on its value (i.e: if value < 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.- Returns:
- The int value at this reader's current position.
- Throws:
IndexOutOfBoundsException- If there are fewer bytes remaining in this reader than are required to satisfy the request.
-
readShort
Relative read method for reading an short value. Reads the next 2 bytes at this reader's current position, composing them into an short value according to big-endian byte order, and then increments the position by two.- Returns:
- The integer value at this reader's current position.
- Throws:
IndexOutOfBoundsException- If there are fewer bytes remaining in this reader than are required to satisfy the request, that is, ifremaining() < 2.
-
readStringUtf8
Relative read method for reading a UTF-8 encoded string. Reads the next number of specified bytes at this reader's current position, decoding them into a string using UTF-8 and then increments the position by the number of bytes read. If UTF-8 decoding fails, the platform's default encoding will be used.- Parameters:
length- The number of bytes to read and decode.- Returns:
- The string value at the reader's current position.
- Throws:
IndexOutOfBoundsException- If there are fewer bytes remaining in this reader than are required to satisfy the request, that is, ifremaining() < length.
-
position
Returns this reader's position.- Returns:
- The position of this reader.
-
position
Sets this reader's position.- Parameters:
pos- The new position value; must be non-negative and no larger than the length of the underlying byte sequence.- Throws:
IndexOutOfBoundsException- If the position is negative or larger than the length of the underlying byte sequence.
-
remaining
Returns the number of bytes between the current position and the end of the underlying byte sequence.- Returns:
- The number of bytes between the current position and the end of the underlying byte sequence.
-
rewind
Rewinds this reader's position to zero.An invocation of this method of the form:
src.rewind();
Has exactly the same effect as:src.position(0);
-
peek
Returns the byte situated at the current position. The byte is not consumed.- Returns:
- the byte situated at the current position
- Throws:
IndexOutOfBoundsException- If the position is negative or larger than the length of the underlying byte sequence.
-
peek
Returns the byte situated at the given offset from current position. The byte is not consumed.- Parameters:
offset- The offset where to look at from current position.- Returns:
- the byte situated at the given offset from current position
- Throws:
IndexOutOfBoundsException- If the position is negative or larger than the length of the underlying byte sequence.
-
skip
Skips the given number of bytes. Negative values are allowed.An invocation of this method of the form:
src.skip(length);
Has exactly the same effect as:src.position(position() + length);
- Parameters:
length- The number of bytes to skip.- Throws:
IndexOutOfBoundsException- If the new position is less than 0 or greater than the length of the underlying byte sequence.
-
toString
-
asInputStream
Returns anInputStreamfrom the current position in the sequence. There is only a singleInputStreamfor a given ByteSequence, so multiple calls toasInputStream()will always return the same object. The returnedInputStreamdoes not supportmark(). Callingclose()does nothing.- Returns:
- an
InputStreamfrom the current position in the sequence
-