Class Buffers
java.lang.Object
cl.netswitch.lib.util.Buffers
Provides static utility methods that operate on
ByteBuffers.-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]read7bitBytes(ByteBuffer buffer) Reads a 7bit byte array from the supplied ByteBuffer.static intread7bitInt(ByteBuffer buffer) Reads a 7bit integer from the supplied ByteBuffer.static longread7bitLong(ByteBuffer buffer) Reads a 7bit long from the supplied ByteBuffer.static Stringread7bitString(ByteBuffer buffer) Reads a 7bit string from the supplied ByteBuffer.static intreadByte(ByteBuffer buffer) Reads a byte from the supplied ByteBuffer.static byte[]readBytes(ByteBuffer buffer, int length) Reads a byte array from the supplied ByteBuffer.static doublereadDouble(ByteBuffer buffer) Reads a double from the supplied ByteBuffer.static floatreadFloat(ByteBuffer buffer) Reads a float from the supplied ByteBuffer.static intreadInt(ByteBuffer buffer) Reads an integer from the supplied ByteBuffer.static longreadLong(ByteBuffer buffer) Reads a long from the supplied ByteBuffer.static shortreadShort(ByteBuffer buffer) Reads a short from the supplied ByteBuffer.static StringtoTrace(byte[] bytes, int offset, int length, boolean inbound) Returns a trace representation of the supplied array of bytes.static StringtoTrace(ByteBuffer buffer, boolean inbound) Returns a trace representation of the suppliedByteBuffer.
-
Field Details
-
EMPTY_BUFFER
ByteBufferwith zero bytes (empty).
-
-
Method Details
-
readByte
Reads a byte from the supplied ByteBuffer. The position of the buffer is incremented by 1.- Parameters:
buffer- the byte buffer that will be read.- Returns:
- the next available byte in supplied ByteBuffer.
- Throws:
NullPointerException- ifbufferisnull.BufferUnderflowException- ifbufferis missing bytes.
-
readShort
Reads a short from the supplied ByteBuffer. The position of the buffer is incremented by 2. The bytes of the buffer are always composed in big endian order (most significant to least significant).- Parameters:
buffer- the ByteBuffer containing the input bytes.- Returns:
- the short value read from the supplied ByteBuffer.
- Throws:
NullPointerException- ifbufferisnull.BufferUnderflowException- ifbufferis missing bytes.
-
readInt
Reads an integer from the supplied ByteBuffer. The position of the buffer is incremented by 4. The bytes of the buffer are always composed in big endian order (most significant to least significant).- Parameters:
buffer- the ByteBuffer containing the input bytes.- Returns:
- the integer value read from the supplied ByteBuffer.
- Throws:
NullPointerException- ifbufferisnull.BufferUnderflowException- ifbufferis missing bytes.
-
readLong
Reads a long from the supplied ByteBuffer. The position of the buffer is incremented by 8. The bytes of the buffer are always composed in big endian order (most significant to least significant).- Parameters:
buffer- the ByteBuffer containing the input bytes.- Returns:
- the long value read from the supplied ByteBuffer.
- Throws:
NullPointerException- ifbufferisnull.BufferUnderflowException- ifbufferis missing bytes.
-
readDouble
Reads a double from the supplied ByteBuffer. The position of the buffer is incremented by 8. The bytes of the buffer are always composed in big endian order (most significant to least significant).- Parameters:
buffer- the ByteBuffer containing the input bytes.- Returns:
- the double value read from the supplied ByteBuffer.
- Throws:
NullPointerException- ifbufferisnull.BufferUnderflowException- ifbufferis missing bytes.
-
readFloat
Reads a float from the supplied ByteBuffer. The position of the buffer is incremented by 4. The bytes of the buffer are always composed in big endian order (most significant to least significant).- Parameters:
buffer- the ByteBuffer containing the input bytes.- Returns:
- the float value read from the supplied ByteBuffer.
- Throws:
NullPointerException- ifbufferisnull.BufferUnderflowException- ifbufferis missing bytes.
-
readBytes
Reads a byte array from the supplied ByteBuffer. The position of the buffer is incremented bylength.- Parameters:
buffer- the byte buffer that will be read.length- the number of bytes to read.- Returns:
- the byte array read from the ByteBuffer.
- Throws:
NullPointerException- ifbufferisnull.IllegalArgumentException- iflengthis negative.BufferUnderflowException- ifbufferis missing bytes.
-
read7bitInt
Reads a 7bit integer from the supplied ByteBuffer.- Parameters:
buffer- the byte buffer that will be read.- Returns:
- the integer value read from the ByteBuffer.
- Throws:
NullPointerException- ifbufferisnull.BufferUnderflowException- ifbufferis missing bytes.
-
read7bitLong
Reads a 7bit long from the supplied ByteBuffer.- Parameters:
buffer- the byte buffer that will be read.- Returns:
- the long value read from the ByteBuffer.
- Throws:
NullPointerException- ifbufferisnull.BufferUnderflowException- ifbufferis missing bytes.
-
read7bitBytes
Reads a 7bit byte array from the supplied ByteBuffer.- Parameters:
buffer- the byte buffer that will be read.- Returns:
- the byte array read from the ByteBuffer.
- Throws:
NullPointerException- ifbufferisnull.BufferUnderflowException- ifbufferis missing bytes.
-
read7bitString
Reads a 7bit string from the supplied ByteBuffer.- Parameters:
buffer- the byte buffer that will be read.- Returns:
- the string read from the ByteBuffer.
- Throws:
NullPointerException- ifbufferisnull.BufferUnderflowException- ifbufferis missing bytes.
-
toTrace
Returns a trace representation of the supplied array of bytes.- Parameters:
bytes- the array of bytes to be converted to a string.offset- the offset of the subarray to be used.length- the length of the subarray to be used.inbound-trueif inbound message; otherwise outbound.- Returns:
- a trace representation of the supplied array of bytes.
- Throws:
NullPointerException- ifbytesisnull.
-
toTrace
Returns a trace representation of the suppliedByteBuffer.- Parameters:
buffer- theByteBufferto be converted to a string.inbound-trueif inbound message; otherwise outbound.- Returns:
- a trace representation of the supplied
ByteBuffer. - Throws:
NullPointerException- ifbufferisnull.
-