Class ByteKit
java.lang.Object
com.iohao.net.common.kit.ByteKit
Byte array conversion utilities for primitive types using big-endian byte order.
- Since:
- 21.15
- Author:
- 渔民小镇
- date:
- 2024-08-10
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleangetBoolean(byte[] array) Reconstruct a boolean value from a byte array.static byte[]getBytes(byte[] data) Return the given byte array, or an empty byte array if it is null.static intgetInt(byte[] array) Reconstruct an int value from a 4-byte big-endian byte array.static longgetLong(byte[] array) Reconstruct a long value from an 8-byte big-endian byte array.static byte[]getPayload(byte[] payload, int payloadLength) Extract a payload of the specified length from a byte array.static StringgetString(byte[] array) Decode a UTF-8 byte array into a string.static byte[]ofBytes(int length) Create a byte array of the specified length, returning a shared empty array for length zero.static byte[]toBytes(boolean value) Convert a boolean value to a single-byte array.static byte[]toBytes(int value) Convert an int value to a 4-byte big-endian byte array.static byte[]toBytes(long value) Convert a long value to an 8-byte big-endian byte array.static byte[]Convert a string to a UTF-8 encoded byte array.
-
Method Details
-
toBytes
public static byte[] toBytes(long value) Convert a long value to an 8-byte big-endian byte array.- Parameters:
value- the long value to convert- Returns:
- an 8-byte array in big-endian order (most significant byte at index 0)
-
getLong
public static long getLong(byte[] array) Reconstruct a long value from an 8-byte big-endian byte array.- Parameters:
array- the byte array (must be at least 8 bytes)- Returns:
- the reconstructed long value
-
toBytes
public static byte[] toBytes(int value) Convert an int value to a 4-byte big-endian byte array.- Parameters:
value- the int value to convert- Returns:
- a 4-byte array in big-endian order (most significant byte at index 0)
-
getInt
public static int getInt(byte[] array) Reconstruct an int value from a 4-byte big-endian byte array.- Parameters:
array- the byte array (must be at least 4 bytes)- Returns:
- the reconstructed int value
-
toBytes
public static byte[] toBytes(boolean value) Convert a boolean value to a single-byte array.- Parameters:
value- the boolean value to convert- Returns:
- a 1-byte array containing
1for true,0for false
-
getBoolean
public static boolean getBoolean(byte[] array) Reconstruct a boolean value from a byte array.- Parameters:
array- the byte array (must be at least 1 byte)- Returns:
trueif the first byte is non-zero,falseotherwise
-
toBytes
Convert a string to a UTF-8 encoded byte array.- Parameters:
value- the string to convert- Returns:
- the UTF-8 encoded byte array
-
getString
Decode a UTF-8 byte array into a string.- Parameters:
array- the UTF-8 encoded byte array- Returns:
- the decoded string
-
getPayload
public static byte[] getPayload(byte[] payload, int payloadLength) Extract a payload of the specified length from a byte array.Return the original array if its length matches
payloadLength; otherwise copy the firstpayloadLengthbytes into a new array.- Parameters:
payload- the source byte arraypayloadLength- the desired payload length- Returns:
- a byte array of exactly
payloadLengthbytes
-
getBytes
public static byte[] getBytes(byte[] data) Return the given byte array, or an empty byte array if it is null.- Parameters:
data- the byte array, may be null- Returns:
- the original array, or an empty array if null
-
ofBytes
public static byte[] ofBytes(int length) Create a byte array of the specified length, returning a shared empty array for length zero.- Parameters:
length- the desired array length- Returns:
- a new byte array, or an empty array if length is 0
-