Class BufferUtils
java.lang.Object
org.apache.sshd.common.util.buffer.BufferUtils
General utilities for working with byte-encoded data.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final IntUnaryOperatorstatic final charstatic final Levelstatic final charstatic final Stringstatic final longMaximum value of auint32fieldstatic final intMaximum value of auint8field -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <A extends Appendable>
AappendHex(A sb, byte[] array, int offset, int len, char sep) static <A extends Appendable>
AappendHex(A sb, char sep, byte... array) static <B extends Buffer>
Bclear(B buffer) InvokesBuffer.clear()static byte[]decodeHex(char separator, CharSequence csq) static byte[]decodeHex(char separator, CharSequence csq, int start, int end) static <S extends OutputStream>
intdecodeHex(S stream, char separator, CharSequence csq) static <S extends OutputStream>
intdecodeHex(S stream, char separator, CharSequence csq, int start, int end) static voiddumpHex(SimplifiedLog logger, Level level, String prefix, char sep, int chunkSize, byte... data) static voiddumpHex(SimplifiedLog logger, Level level, String prefix, char sep, int chunkSize, byte[] data, int offset, int len) static voiddumpHex(SimplifiedLog logger, Level level, String prefix, PropertyResolver resolver, char sep, byte... data) static voiddumpHex(SimplifiedLog logger, Level level, String prefix, PropertyResolver resolver, char sep, byte[] data, int offset, int len) static booleanequals(byte[] a1, byte[] a2) Compares the contents of 2 arrays of bytes - Note: do not use it to execute security related comparisons (e.g.static booleanequals(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.static bytefromHex(char hi, char lo) static BigIntegerfromMPIntBytes(byte[] mpInt) static longgetLong(byte[] buf, int off, int len) static intgetNextPowerOf2(int value) static longgetUInt(byte... buf) static longgetUInt(byte[] buf, int off, int len) static intindexOf(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.static booleanisValidInt32Value(long value) static booleanisValidUint32Value(long value) static intputLong(long value, byte[] buf, int off, int len) static intputUInt(long value, byte[] buf) Writes a 32-bit value in network order (i.e., MSB 1st)static intputUInt(long value, byte[] buf, int off, int len) Writes a 32-bit value in network order (i.e., MSB 1st)static intreadInt(InputStream input, byte[] buf) Read a 32-bit value in network orderstatic intreadInt(InputStream input, byte[] buf, int offset, int len) Read a 32-bit value in network orderstatic longreadUInt(InputStream input, byte[] buf) Read a 32-bit value in network orderstatic longreadUInt(InputStream input, byte[] buf, int offset, int len) Read a 32-bit value in network orderstatic StringtoHex(byte... array) static StringtoHex(byte[] array, int offset, int len) static StringtoHex(byte[] array, int offset, int len, char sep) static StringtoHex(char sep, byte... array) static intupdateLengthPlaceholder(Buffer buffer, int lenPos) Used for encodings where we don't know the data length before adding it to the buffer.static voidupdateLengthPlaceholder(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.static longvalidateInt32Value(long value, String message) static longvalidateInt32Value(long value, String format, Object arg) static longvalidateInt32Value(long value, String format, Object... args) static longvalidateUint32Value(long value, String message) static longvalidateUint32Value(long value, String format, Object arg) static longvalidateUint32Value(long value, String format, Object... args) static voidwriteInt(OutputStream output, int value, byte[] buf) Writes a 32-bit value in network order (i.e., MSB 1st)static voidwriteInt(OutputStream output, int value, byte[] buf, int off, int len) Writes a 32-bit value in network order (i.e., MSB 1st)static voidwriteUInt(OutputStream output, long value, byte[] buf) Writes a 32-bit value in network order (i.e., MSB 1st)static voidwriteUInt(OutputStream output, long value, byte[] buf, int off, int len) Writes a 32-bit value in network order (i.e., MSB 1st)
-
Field Details
-
DEFAULT_HEX_SEPARATOR
public static final char DEFAULT_HEX_SEPARATOR- See Also:
-
EMPTY_HEX_SEPARATOR
public static final char EMPTY_HEX_SEPARATOR- See Also:
-
HEX_DIGITS
- See Also:
-
DEFAULT_HEXDUMP_LEVEL
-
DEFAULT_BUFFER_GROWTH_FACTOR
-
MAX_UINT32_VALUE
public static final long MAX_UINT32_VALUEMaximum value of auint32field- See Also:
-
MAX_UINT8_VALUE
public static final int MAX_UINT8_VALUEMaximum value of auint8field- See Also:
-
-
Constructor Details
-
BufferUtils
private BufferUtils()Private Constructor
-
-
Method Details
-
indexOf
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 anullinput array.A negative startIndex is treated as zero. A startIndex larger than the array length will return
-1.- Parameters:
array- the array to search through for the object, may benullvalueToFind- the value to findstartIndex- the index to start searching atlen- the number of elements to search from the start index- Returns:
- the index of the value within the array,
-1if not found ornullarray input or non-positive number of elements
-
dumpHex
public static void dumpHex(SimplifiedLog logger, Level level, String prefix, PropertyResolver resolver, char sep, byte... data) -
dumpHex
public static void dumpHex(SimplifiedLog logger, Level level, String prefix, PropertyResolver resolver, char sep, byte[] data, int offset, int len) -
dumpHex
public static void dumpHex(SimplifiedLog logger, Level level, String prefix, char sep, int chunkSize, byte... data) -
dumpHex
public static void dumpHex(SimplifiedLog logger, Level level, String prefix, char sep, int chunkSize, byte[] data, int offset, int len) -
toHex
-
toHex
-
toHex
-
toHex
-
appendHex
- Throws:
IOException
-
appendHex
public static <A extends Appendable> A appendHex(A sb, byte[] array, int offset, int len, char sep) throws IOException - Throws:
IOException
-
decodeHex
- Parameters:
separator- The separator between the HEX values - may beEMPTY_HEX_SEPARATORcsq- TheCharSequencecontaining the HEX encoded bytes- Returns:
- The decoded bytes
- Throws:
IllegalArgumentException- If invalid HEX sequence lengthNumberFormatException- If invalid HEX characters found- See Also:
-
decodeHex
- Parameters:
separator- The separator between the HEX values - may beEMPTY_HEX_SEPARATORcsq- TheCharSequencecontaining the HEX encoded bytesstart- Start offset of the HEX sequence (inclusive)end- End offset of the HEX sequence (exclusive)- Returns:
- The decoded bytes
- Throws:
IllegalArgumentException- If invalid HEX sequence lengthNumberFormatException- If invalid HEX characters found
-
decodeHex
public static <S extends OutputStream> int decodeHex(S stream, char separator, CharSequence csq) throws IOException - Type Parameters:
S- TheOutputStreamgeneric type- Parameters:
stream- The targetOutputStreamseparator- The separator between the HEX values - may beEMPTY_HEX_SEPARATORcsq- TheCharSequencecontaining the HEX encoded bytes- Returns:
- The number of bytes written to the stream
- Throws:
IOException- If failed to writeIllegalArgumentException- If invalid HEX sequence lengthNumberFormatException- If invalid HEX characters found- See Also:
-
decodeHex
public static <S extends OutputStream> int decodeHex(S stream, char separator, CharSequence csq, int start, int end) throws IOException - Type Parameters:
S- TheOutputStreamgeneric type- Parameters:
stream- The targetOutputStreamseparator- The separator between the HEX values - may beEMPTY_HEX_SEPARATORcsq- TheCharSequencecontaining the HEX encoded bytesstart- Start offset of the HEX sequence (inclusive)end- End offset of the HEX sequence (exclusive)- Returns:
- The number of bytes written to the stream
- Throws:
IOException- If failed to writeIllegalArgumentException- If invalid HEX sequence lengthNumberFormatException- If invalid HEX characters found
-
fromHex
- Throws:
NumberFormatException
-
readInt
Read a 32-bit value in network order- Parameters:
input- TheInputStreambuf- Work buffer to use- Returns:
- The read 32-bit value
- Throws:
IOException- If failed to read 4 bytes or not enough room in work buffer- See Also:
-
readInt
Read a 32-bit value in network order- Parameters:
input- TheInputStreambuf- Work buffer to useoffset- Offset in buffer to uslen- Available length - must have at least 4 bytes available- Returns:
- The read 32-bit value
- Throws:
IOException- If failed to read 4 bytes or not enough room in work buffer- See Also:
-
readUInt
Read a 32-bit value in network order- Parameters:
input- TheInputStreambuf- Work buffer to use- Returns:
- The read 32-bit value
- Throws:
IOException- If failed to read 4 bytes or not enough room in work buffer- See Also:
-
readUInt
Read a 32-bit value in network order- Parameters:
input- TheInputStreambuf- Work buffer to useoffset- Offset in buffer to uslen- Available length - must have at least 4 bytes available- Returns:
- The read 32-bit value
- Throws:
IOException- If failed to read 4 bytes or not enough room in work buffer- See Also:
-
getUInt
public static long getUInt(byte... buf) - Parameters:
buf- 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:
- The result as a
longwhose 32 high-order bits are zero - See Also:
-
getUInt
public static long getUInt(byte[] buf, int off, int len) - Parameters:
buf- A buffer holding a 32-bit unsigned integer in big endian format.off- The offset of the data in the bufferlen- 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:
- The result as a
longwhose 32 high-order bits are zero
-
getLong
public static long getLong(byte[] buf, int off, int len) -
fromMPIntBytes
-
writeInt
Writes a 32-bit value in network order (i.e., MSB 1st)- Parameters:
output- TheOutputStreamto write the valuevalue- The 32-bit valuebuf- A work buffer to use - must have enough space to contain 4 bytes- Throws:
IOException- If failed to write the value or work buffer too small- See Also:
-
writeInt
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- TheOutputStreamto write the valuevalue- The 32-bit valuebuf- A work buffer to use - must have enough space to contain 4 bytesoff- The offset to write the valuelen- The available space- Throws:
IOException- If failed to write the value or work buffer too small- See Also:
-
writeUInt
Writes a 32-bit value in network order (i.e., MSB 1st)- Parameters:
output- TheOutputStreamto write the valuevalue- The 32-bit valuebuf- A work buffer to use - must have enough space to contain 4 bytes- Throws:
IOException- If failed to write the value or work buffer too small- See Also:
-
writeUInt
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- TheOutputStreamto write the valuevalue- The 32-bit valuebuf- A work buffer to use - must have enough space to contain 4 bytesoff- The offset to write the valuelen- The available space- Throws:
IOException- If failed to write the value or work buffer to small- See Also:
-
putUInt
public static int putUInt(long value, byte[] buf) Writes a 32-bit value in network order (i.e., MSB 1st)- Parameters:
value- The 32-bit valuebuf- The buffer- Returns:
- The number of bytes used in the buffer
- Throws:
IllegalArgumentException- if not enough space available- See Also:
-
putUInt
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- The 32-bit valuebuf- The bufferoff- The offset to write the valuelen- The available space- Returns:
- The number of bytes used in the buffer
- Throws:
IllegalArgumentException- if not enough space available
-
putLong
public static int putLong(long value, byte[] buf, int off, int len) -
equals
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. UseMac#equalsmethod instead.- Parameters:
a1- 1st arraya2- 2nd array- Returns:
trueif all bytes in the compared arrays are equal
-
equals
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. UseMac#equalsmethod instead.- Parameters:
a1- 1st arraya1Offset- Offset to start comparing in 1st arraya2- 2nd arraya2Offset- Offset to start comparing in 2nd arraylength- Number of bytes to compare- Returns:
trueif all bytes in the compared arrays are equal when compared from the specified offsets and up to specified length
-
getNextPowerOf2
public static int getNextPowerOf2(int value) -
updateLengthPlaceholder
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- TheBufferlenPos- 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:
- The amount of data that has been encoded
-
updateLengthPlaceholder
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- TheBufferlenPos- The offset in the buffer where the length placeholder is to be update - Note: assumption is that the encoded data starts immediately after the placeholderdataLength- The length to update - a UINT32 value as along
-
clear
InvokesBuffer.clear()- Type Parameters:
B- The generic buffer type- Parameters:
buffer- ABufferinstance - ignored ifnull- Returns:
- The same as the input instance
-
validateInt32Value
-
validateInt32Value
-
validateInt32Value
-
isValidInt32Value
public static boolean isValidInt32Value(long value) -
validateUint32Value
-
validateUint32Value
-
validateUint32Value
-
isValidUint32Value
public static boolean isValidUint32Value(long value)
-