Top Description Fields Constructors Methods
org.apache.sshd.common.util.buffer

public final Class BufferUtils

extends Object
Class Inheritance
Imports
java.io.IOException, .InputStream, .OutputStream, .StreamCorruptedException, java.math.BigInteger, java.util.function.IntUnaryOperator, java.util.logging.Level, org.apache.sshd.common.CommonModuleProperties, .PropertyResolver, org.apache.sshd.common.util.GenericUtils, .NumberUtils, .ValidateUtils, org.apache.sshd.common.util.io.IoUtils, org.apache.sshd.common.util.logging.SimplifiedLog

General utilities for working with byte-encoded data.
Author
Apache MINA SSHD Project

Field Summary

Modifier and TypeField and Description
public static final IntUnaryOperator
public static final char
public static final Level
public static final char
public static final String
public static final long
MAX_UINT32_VALUE

Maximum value of a uint32 field

public static final int
MAX_UINT8_VALUE

Maximum value of a uint8 field

Constructor Summary

AccessConstructor and Description
private
BufferUtils()

Private Constructor

Method Summary

Modifier and TypeMethod and Description
public static <A extends Appendable> A
appendHex(A sb, char sep, byte... array)

public static <A extends Appendable> A
appendHex(A sb, byte[] array, int offset, int len, char sep)

public static <
The generic buffer type
B extends Buffer
>
B

Returns:

The same as the input instance
clear
(B
A Buffer instance - ignored if null
buffer
)

Invokes Buffer#clear()

public static byte[]

Returns:

The decoded bytes
decodeHex
(char
The separator between the HEX values - may be EMPTY_HEX_SEPARATOR
separator
,
CharSequence
The CharSequence containing the HEX encoded bytes
csq
)

public static byte[]

Returns:

The decoded bytes
decodeHex
(char
The separator between the HEX values - may be EMPTY_HEX_SEPARATOR
separator
,
CharSequence
The CharSequence containing the HEX encoded bytes
csq
,
int
Start offset of the HEX sequence (inclusive)
start
,
int
End offset of the HEX sequence (exclusive)
end
)

public static <
The OutputStream generic type
S extends OutputStream
>
int

Returns:

The number of bytes written to the stream
decodeHex
(S
The target OutputStream
stream
,
char
The separator between the HEX values - may be EMPTY_HEX_SEPARATOR
separator
,
CharSequence
The CharSequence containing the HEX encoded bytes
csq
)

public static <
The OutputStream generic type
S extends OutputStream
>
int

Returns:

The number of bytes written to the stream
decodeHex
(S
The target OutputStream
stream
,
char
The separator between the HEX values - may be EMPTY_HEX_SEPARATOR
separator
,
CharSequence
The CharSequence containing the HEX encoded bytes
csq
,
int
Start offset of the HEX sequence (inclusive)
start
,
int
End offset of the HEX sequence (exclusive)
end
)

public static void
dumpHex(SimplifiedLog logger, Level level, String prefix, PropertyResolver resolver, char sep, byte... data)

public static void
dumpHex(SimplifiedLog logger, Level level, String prefix, PropertyResolver resolver, char sep, byte[] data, int offset, int len)

public static void
dumpHex(SimplifiedLog logger, Level level, String prefix, char sep, int chunkSize, byte... data)

public static void
dumpHex(SimplifiedLog logger, Level level, String prefix, char sep, int chunkSize, byte[] data, int offset, int len)

public static boolean

Returns:

true if all bytes in the compared arrays are equal
equals
(byte[]
1st array
a1
,
byte[]
2nd array
a2
)

Compares the contents of 2 arrays of bytes - Note: do not use it to execute security related comparisons (e.g. MACs) since the method leaks timing information.

public static boolean

Returns:

true if all bytes in the compared arrays are equal when compared from the specified offsets and up to specified length
equals
(byte[]
1st array
a1
,
int
Offset to start comparing in 1st array
a1Offset
,
byte[]
2nd array
a2
,
int
Offset to start comparing in 2nd array
a2Offset
,
int
Number of bytes to compare
length
)

Compares the contents of 2 arrays of bytes - Note: do not use it to execute security related comparisons (e.g. MACs) since the method leaks timing information.

public static byte
fromHex(char hi, char lo)

public static BigInteger
fromMPIntBytes(byte[] mpInt)

public static int

Returns:

The integer value read
getInt
(byte[]
A buffer holding a 32-bit signed integer in big endian format.
buf
,
int
The offset of the data in the buffer
off
,
int
The available data length

Note

if more than 4 bytes are available, then only the first 4 bytes in the buffer will be used (starting at the specified offset)

len
)

public static long
getLong(byte[] buf, int off, int len)

public static int
getNextPowerOf2(int value)

public static long

Returns:

The result as a long whose 32 high-order bits are zero
getUInt
(byte...
A buffer holding a 32-bit unsigned integer in big endian format

Note

if more than 4 bytes are available, then only the first 4 bytes in the buffer will be used

buf
)

public static long

Returns:

The result as a long whose 32 high-order bits are zero
getUInt
(byte[]
A buffer holding a 32-bit unsigned integer in big endian format.
buf
,
int
The offset of the data in the buffer
off
,
int
The available data length

Note

if more than 4 bytes are available, then only the first 4 bytes in the buffer will be used (starting at the specified offset)

len
)

public static int

Returns:

the index of the value within the array, -1 if not found or null array input or non-positive number of elements
indexOf
(byte[]
the array to search through for the object, may be null
array
,
byte
the value to find
valueToFind
,
int
the index to start searching at
startIndex
,
int
the number of elements to search from the start index
len
)

Finds the index of the given value in the array starting at the given index and checking up to specified number of elements.

public static boolean
isValidInt32Value(long value)

public static boolean
isValidUint32Value(long value)

public static int
putLong(long value, byte[] buf, int off, int len)

public static int

Returns:

The number of bytes used in the buffer
putUInt
(long
The 32-bit value
value
,
byte[]
The buffer
buf
)

Writes a 32-bit value in network order (i.e., MSB 1st)

public static int

Returns:

The number of bytes used in the buffer
putUInt
(long
The 32-bit value
value
,
byte[]
The buffer
buf
,
int
The offset to write the value
off
,
int
The available space
len
)

Writes a 32-bit value in network order (i.e., MSB 1st)

public static int

Returns:

The read 32-bit value
readInt
(InputStream input, byte[]
Work buffer to use
buf
)

Read a 32-bit value in network order

public static int

Returns:

The read 32-bit value
readInt
(InputStream input, byte[]
Work buffer to use
buf
,
int
Offset in buffer to us
offset
,
int
Available length - must have at least 4 bytes available
len
)

Read a 32-bit value in network order

public static long

Returns:

The read 32-bit value
readUInt
(InputStream input, byte[]
Work buffer to use
buf
)

Read a 32-bit value in network order

public static long

Returns:

The read 32-bit value
readUInt
(InputStream input, byte[]
Work buffer to use
buf
,
int
Offset in buffer to us
offset
,
int
Available length - must have at least 4 bytes available
len
)

Read a 32-bit value in network order

public static String
toHex(byte... array)

public static String
toHex(char sep, byte... array)

public static String
toHex(byte[] array, int offset, int len)

public static String
toHex(byte[] array, int offset, int len, char sep)

public static int

Returns:

The amount of data that has been encoded
updateLengthPlaceholder
(Buffer
The Buffer
buffer
,
int
The offset in the buffer where the length placeholder is to be update - Note: assumption is that the encoded data starts immediately after the placeholder
lenPos
)

Used for encodings where we don't know the data length before adding it to the buffer.

public static void
updateLengthPlaceholder(Buffer
The Buffer
buffer
,
int
The offset in the buffer where the length placeholder is to be update - Note: assumption is that the encoded data starts immediately after the placeholder
lenPos
,
long
The length to update - a UINT32 value as a long
dataLength
)

Updates a 32-bit "placeholder" location for data length - moves the write position to the specified placeholder position, updates the length value and then moves the write position it back to its original value.

public static long
validateInt32Value(long value, String message)

public static long
validateInt32Value(long value, String format, Object arg)

public static long
validateInt32Value(long value, String format, Object... args)

public static long
validateUint32Value(long value, String message)

public static long
validateUint32Value(long value, String format, Object arg)

public static long
validateUint32Value(long value, String format, Object... args)

public static void
writeInt(OutputStream
The OutputStream to write the value
output
,
int
The 32-bit value
value
,
byte[]
A work buffer to use - must have enough space to contain 4 bytes
buf
)

Writes a 32-bit value in network order (i.e., MSB 1st)

public static void
writeInt(OutputStream
The OutputStream to write the value
output
,
int
The 32-bit value
value
,
byte[]
A work buffer to use - must have enough space to contain 4 bytes
buf
,
int
The offset to write the value
off
,
int
The available space
len
)

Writes a 32-bit value in network order (i.e., MSB 1st)

public static void
writeUInt(OutputStream
The OutputStream to write the value
output
,
long
The 32-bit value
value
,
byte[]
A work buffer to use - must have enough space to contain 4 bytes
buf
)

Writes a 32-bit value in network order (i.e., MSB 1st)

public static void
writeUInt(OutputStream
The OutputStream to write the value
output
,
long
The 32-bit value
value
,
byte[]
A work buffer to use - must have enough space to contain 4 bytes
buf
,
int
The offset to write the value
off
,
int
The available space
len
)

Writes a 32-bit value in network order (i.e., MSB 1st)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_BUFFER_GROWTH_FACTORback to summary
public static final IntUnaryOperator DEFAULT_BUFFER_GROWTH_FACTOR
DEFAULT_HEX_SEPARATORback to summary
public static final char DEFAULT_HEX_SEPARATOR
DEFAULT_HEXDUMP_LEVELback to summary
public static final Level DEFAULT_HEXDUMP_LEVEL
EMPTY_HEX_SEPARATORback to summary
public static final char EMPTY_HEX_SEPARATOR
HEX_DIGITSback to summary
public static final String HEX_DIGITS
MAX_UINT32_VALUEback to summary
public static final long MAX_UINT32_VALUE

Maximum value of a uint32 field

MAX_UINT8_VALUEback to summary
public static final int MAX_UINT8_VALUE

Maximum value of a uint8 field

Constructor Detail

BufferUtilsback to summary
private BufferUtils()

Private Constructor

Method Detail

appendHexback to summary
public static <A extends Appendable> A appendHex(A sb, char sep, byte... array) throws IOException
appendHexback to summary
public static <A extends Appendable> A appendHex(A sb, byte[] array, int offset, int len, char sep) throws IOException
clearback to summary
public static <B extends Buffer> B clear(B buffer)

Invokes Buffer#clear()

Parameters
<B>
The generic buffer type
buffer:B

A Buffer instance - ignored if null

Returns:B

The same as the input instance

decodeHexback to summary
public static byte[] decodeHex(char separator, CharSequence csq)
Parameters
separator:char

The separator between the HEX values - may be EMPTY_HEX_SEPARATOR

csq:CharSequence

The CharSequence containing the HEX encoded bytes

Returns:byte[]

The decoded bytes

Exceptions
IllegalArgumentException:
If invalid HEX sequence length
NumberFormatException:
If invalid HEX characters found
See Also
decodeHex(char, CharSequence, int, int)
decodeHexback to summary
public static byte[] decodeHex(char separator, CharSequence csq, int start, int end)
Parameters
separator:char

The separator between the HEX values - may be EMPTY_HEX_SEPARATOR

csq:CharSequence

The CharSequence containing the HEX encoded bytes

start:int

Start offset of the HEX sequence (inclusive)

end:int

End offset of the HEX sequence (exclusive)

Returns:byte[]

The decoded bytes

Exceptions
IllegalArgumentException:
If invalid HEX sequence length
NumberFormatException:
If invalid HEX characters found
decodeHexback to summary
public static <S extends OutputStream> int decodeHex(S stream, char separator, CharSequence csq) throws IOException
Parameters
<S>
The OutputStream generic type
stream:S

The target OutputStream

separator:char

The separator between the HEX values - may be EMPTY_HEX_SEPARATOR

csq:CharSequence

The CharSequence containing the HEX encoded bytes

Returns:int

The number of bytes written to the stream

Exceptions
IOException:
If failed to write
IllegalArgumentException:
If invalid HEX sequence length
NumberFormatException:
If invalid HEX characters found
See Also
decodeHex(OutputStream, char, CharSequence, int, int)
decodeHexback to summary
public static <S extends OutputStream> int decodeHex(S stream, char separator, CharSequence csq, int start, int end) throws IOException
Parameters
<S>
The OutputStream generic type
stream:S

The target OutputStream

separator:char

The separator between the HEX values - may be EMPTY_HEX_SEPARATOR

csq:CharSequence

The CharSequence containing the HEX encoded bytes

start:int

Start offset of the HEX sequence (inclusive)

end:int

End offset of the HEX sequence (exclusive)

Returns:int

The number of bytes written to the stream

Exceptions
IOException:
If failed to write
IllegalArgumentException:
If invalid HEX sequence length
NumberFormatException:
If invalid HEX characters found
dumpHexback to summary
public static void dumpHex(SimplifiedLog logger, Level level, String prefix, PropertyResolver resolver, char sep, byte... data)
dumpHexback to summary
public static void dumpHex(SimplifiedLog logger, Level level, String prefix, PropertyResolver resolver, char sep, byte[] data, int offset, int len)
dumpHexback to summary
public static void dumpHex(SimplifiedLog logger, Level level, String prefix, char sep, int chunkSize, byte... data)
dumpHexback to summary
public static void dumpHex(SimplifiedLog logger, Level level, String prefix, char sep, int chunkSize, byte[] data, int offset, int len)
equalsback to summary
public static boolean equals(byte[] a1, byte[] a2)

Compares the contents of 2 arrays of bytes - Note: do not use it to execute security related comparisons (e.g. MACs) since the method leaks timing information. Use Mac#equals method instead.

Parameters
a1:byte[]

1st array

a2:byte[]

2nd array

Returns:boolean

true if all bytes in the compared arrays are equal

equalsback to summary
public static boolean equals(byte[] a1, int a1Offset, byte[] a2, int a2Offset, int length)

Compares the contents of 2 arrays of bytes - Note: do not use it to execute security related comparisons (e.g. MACs) since the method leaks timing information. Use Mac#equals method instead.

Parameters
a1:byte[]

1st array

a1Offset:int

Offset to start comparing in 1st array

a2:byte[]

2nd array

a2Offset:int

Offset to start comparing in 2nd array

length:int

Number of bytes to compare

Returns:boolean

true if all bytes in the compared arrays are equal when compared from the specified offsets and up to specified length

Annotations
@SuppressWarnings:PMD.AssignmentInOperand
fromHexback to summary
public static byte fromHex(char hi, char lo) throws NumberFormatException
fromMPIntBytesback to summary
public static BigInteger fromMPIntBytes(byte[] mpInt)
getIntback to summary
public static int getInt(byte[] buf, int off, int len)
Parameters
buf:byte[]

A buffer holding a 32-bit signed integer in big endian format.

off:int

The offset of the data in the buffer

len:int

The available data length

Note

if more than 4 bytes are available, then only the first 4 bytes in the buffer will be used (starting at the specified offset)

Returns:int

The integer value read

getLongback to summary
public static long getLong(byte[] buf, int off, int len)
getNextPowerOf2back to summary
public static int getNextPowerOf2(int value)
getUIntback to summary
public static long getUInt(byte... buf)
Parameters
buf:byte[]

A buffer holding a 32-bit unsigned integer in big endian format

Note

if more than 4 bytes are available, then only the first 4 bytes in the buffer will be used

Returns:long

The result as a long whose 32 high-order bits are zero

See Also
getUInt(byte[], int, int)
getUIntback to summary
public static long getUInt(byte[] buf, int off, int len)
Parameters
buf:byte[]

A buffer holding a 32-bit unsigned integer in big endian format.

off:int

The offset of the data in the buffer

len:int

The available data length

Note

if more than 4 bytes are available, then only the first 4 bytes in the buffer will be used (starting at the specified offset)

Returns:long

The result as a long whose 32 high-order bits are zero

indexOfback to summary
public static int indexOf(byte[] array, byte valueToFind, int startIndex, int len)

Finds the index of the given value in the array starting at the given index and checking up to specified number of elements.

This method returns -1) for a null input array.

A negative startIndex is treated as zero. A startIndex larger than the array length will return -1.

Parameters
array:byte[]

the array to search through for the object, may be null

valueToFind:byte

the value to find

startIndex:int

the index to start searching at

len:int

the number of elements to search from the start index

Returns:int

the index of the value within the array, -1 if not found or null array input or non-positive number of elements

isValidInt32Valueback to summary
public static boolean isValidInt32Value(long value)
isValidUint32Valueback to summary
public static boolean isValidUint32Value(long value)
putLongback to summary
public static int putLong(long value, byte[] buf, int off, int len)
putUIntback to summary
public static int putUInt(long value, byte[] buf)

Writes a 32-bit value in network order (i.e., MSB 1st)

Parameters
value:long

The 32-bit value

buf:byte[]

The buffer

Returns:int

The number of bytes used in the buffer

Exceptions
IllegalArgumentException:
if not enough space available
See Also
putUInt(long, byte[], int, int)
putUIntback to summary
public static int putUInt(long value, byte[] buf, int off, int len)

Writes a 32-bit value in network order (i.e., MSB 1st)

Parameters
value:long

The 32-bit value

buf:byte[]

The buffer

off:int

The offset to write the value

len:int

The available space

Returns:int

The number of bytes used in the buffer

Exceptions
IllegalArgumentException:
if not enough space available
readIntback to summary
public static int readInt(InputStream input, byte[] buf) throws IOException

Read a 32-bit value in network order

Parameters
input:InputStream

The InputStream

buf:byte[]

Work buffer to use

Returns:int

The read 32-bit value

Exceptions
IOException:
If failed to read 4 bytes or not enough room in work buffer
See Also
readInt(InputStream, byte[], int, int)
readIntback to summary
public static int readInt(InputStream input, byte[] buf, int offset, int len) throws IOException

Read a 32-bit value in network order

Parameters
input:InputStream

The InputStream

buf:byte[]

Work buffer to use

offset:int

Offset in buffer to us

len:int

Available length - must have at least 4 bytes available

Returns:int

The read 32-bit value

Exceptions
IOException:
If failed to read 4 bytes or not enough room in work buffer
See Also
readUInt(InputStream, byte[], int, int)
readUIntback to summary
public static long readUInt(InputStream input, byte[] buf) throws IOException

Read a 32-bit value in network order

Parameters
input:InputStream

The InputStream

buf:byte[]

Work buffer to use

Returns:long

The read 32-bit value

Exceptions
IOException:
If failed to read 4 bytes or not enough room in work buffer
See Also
readUInt(InputStream, byte[], int, int)
readUIntback to summary
public static long readUInt(InputStream input, byte[] buf, int offset, int len) throws IOException

Read a 32-bit value in network order

Parameters
input:InputStream

The InputStream

buf:byte[]

Work buffer to use

offset:int

Offset in buffer to us

len:int

Available length - must have at least 4 bytes available

Returns:long

The read 32-bit value

Exceptions
IOException:
If failed to read 4 bytes or not enough room in work buffer
See Also
getUInt(byte[], int, int)
toHexback to summary
public static String toHex(byte... array)
toHexback to summary
public static String toHex(char sep, byte... array)
toHexback to summary
public static String toHex(byte[] array, int offset, int len)
toHexback to summary
public static String toHex(byte[] array, int offset, int len, char sep)
updateLengthPlaceholderback to summary
public static int updateLengthPlaceholder(Buffer buffer, int lenPos)

Used for encodings where we don't know the data length before adding it to the buffer. The idea is to place a 32-bit "placeholder", encode the data and then return back to the placeholder and update the length. The method calculates the encoded data length, moves the write position to the specified placeholder position, updates the length value and then moves the write position it back to its original value.

Parameters
buffer:Buffer

The Buffer

lenPos:int

The offset in the buffer where the length placeholder is to be update - Note: assumption is that the encoded data starts immediately after the placeholder

Returns:int

The amount of data that has been encoded

updateLengthPlaceholderback to summary
public static void updateLengthPlaceholder(Buffer buffer, int lenPos, long dataLength)

Updates a 32-bit "placeholder" location for data length - moves the write position to the specified placeholder position, updates the length value and then moves the write position it back to its original value.

Parameters
buffer:Buffer

The Buffer

lenPos:int

The offset in the buffer where the length placeholder is to be update - Note: assumption is that the encoded data starts immediately after the placeholder

dataLength:long

The length to update - a UINT32 value as a long

validateInt32Valueback to summary
public static long validateInt32Value(long value, String message)
validateInt32Valueback to summary
public static long validateInt32Value(long value, String format, Object arg)
validateInt32Valueback to summary
public static long validateInt32Value(long value, String format, Object... args)
validateUint32Valueback to summary
public static long validateUint32Value(long value, String message)
validateUint32Valueback to summary
public static long validateUint32Value(long value, String format, Object arg)
validateUint32Valueback to summary
public static long validateUint32Value(long value, String format, Object... args)
writeIntback to summary
public static void writeInt(OutputStream output, int value, byte[] buf) throws IOException

Writes a 32-bit value in network order (i.e., MSB 1st)

Parameters
output:OutputStream

The OutputStream to write the value

value:int

The 32-bit value

buf:byte[]

A work buffer to use - must have enough space to contain 4 bytes

Exceptions
IOException:
If failed to write the value or work buffer too small
See Also
writeInt(OutputStream, int, byte[], int, int)
writeIntback to summary
public static void writeInt(OutputStream output, int value, byte[] buf, int off, int len) throws IOException

Writes a 32-bit value in network order (i.e., MSB 1st)

Parameters
output:OutputStream

The OutputStream to write the value

value:int

The 32-bit value

buf:byte[]

A work buffer to use - must have enough space to contain 4 bytes

off:int

The offset to write the value

len:int

The available space

Exceptions
IOException:
If failed to write the value or work buffer too small
See Also
writeUInt(OutputStream, long, byte[], int, int)
writeUIntback to summary
public static void writeUInt(OutputStream output, long value, byte[] buf) throws IOException

Writes a 32-bit value in network order (i.e., MSB 1st)

Parameters
output:OutputStream

The OutputStream to write the value

value:long

The 32-bit value

buf:byte[]

A work buffer to use - must have enough space to contain 4 bytes

Exceptions
IOException:
If failed to write the value or work buffer too small
See Also
writeUInt(OutputStream, long, byte[], int, int)
writeUIntback to summary
public static void writeUInt(OutputStream output, long value, byte[] buf, int off, int len) throws IOException

Writes a 32-bit value in network order (i.e., MSB 1st)

Parameters
output:OutputStream

The OutputStream to write the value

value:long

The 32-bit value

buf:byte[]

A work buffer to use - must have enough space to contain 4 bytes

off:int

The offset to write the value

len:int

The available space

Exceptions
IOException:
If failed to write the value or work buffer to small
See Also
putUInt(long, byte[], int, int)