Class ByteKit

java.lang.Object
com.iohao.net.common.kit.ByteKit

public final class ByteKit extends Object
Byte array conversion utilities for primitive types using big-endian byte order.
Since:
21.15
Author:
渔民小镇
date:
2024-08-10
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    getBoolean(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 int
    getInt(byte[] array)
    Reconstruct an int value from a 4-byte big-endian byte array.
    static long
    getLong(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 String
    getString(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[]
    toBytes(String value)
    Convert a string to a UTF-8 encoded byte array.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 1 for true, 0 for 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:
      true if the first byte is non-zero, false otherwise
    • toBytes

      public static byte[] toBytes(String value)
      Convert a string to a UTF-8 encoded byte array.
      Parameters:
      value - the string to convert
      Returns:
      the UTF-8 encoded byte array
    • getString

      public static String getString(byte[] array)
      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 first payloadLength bytes into a new array.

      Parameters:
      payload - the source byte array
      payloadLength - the desired payload length
      Returns:
      a byte array of exactly payloadLength bytes
    • 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