Class Bytes

java.lang.Object
cloud.opencode.base.core.primitives.Bytes

public final class Bytes extends Object
Byte Array Utility Class - Guava-style operations for byte primitive arrays byte 数组工具类 - Guava 风格的 byte 原始类型数组操作

Provides comprehensive byte array operations inspired by Guava Bytes.

提供 byte 原始类型数组的操作方法。

Features | 主要功能:

  • Array operations (concat, contains, indexOf, lastIndexOf) - 数组操作
  • Collection conversion (asList, toArray) - 集合转换
  • Array manipulation (reverse) - 数组操作
  • String operations (join) - 字符串操作

Usage Examples | 使用示例:

// Array operations - 数组操作
byte[] merged = Bytes.concat(arr1, arr2);
boolean found = Bytes.contains(arr, (byte) 0x0F);
int idx = Bytes.indexOf(arr, (byte) 0x00);

Security | 安全性:

  • Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
  • Null-safe: Partially (throws on null array) - 空值安全: 部分 (null 数组抛异常)
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
     
    static final byte[]
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static List<Byte>
    asList(byte... array)
    Converts to a List 转为 List
    static int
    compare(byte[] a, byte[] b)
    Compares two byte arrays 比较两个字节数组
    static int
    compareUnsigned(byte a, byte b)
    Unsigned comparison 无符号比较
    static byte[]
    concat(byte[]... arrays)
    Concatenates multiple arrays | 合并多个数组
    static byte
    constrainToRange(byte value, byte min, byte max)
    Constrains the value to the specified range 约束值在指定范围内
    static boolean
    contains(byte[] array, byte target)
    Checks whether the array contains the specified element | 检查数组是否包含指定元素
    static byte[]
    ensureCapacity(byte[] array, int minLength, int padding)
    Ensures that array has minimum capacity, expanding if needed.
    static byte[]
    ensureNonNull(byte[] array)
     
    static boolean
    equals(byte[] a, byte[] b)
    Checks whether two byte arrays are equal 比较字节数组是否相等
    static int
    hashCode(byte[] array)
    Computes the hash code of a byte array 计算字节数组哈希码
    static int
    indexOf(byte[] array, byte target)
    Finds the index of the specified element | 查找元素索引
    static int
    indexOf(byte[] array, byte[] target)
    Finds the position of a sub-array 查找子数组位置
    static int
    indexOf(byte[] array, byte target, int start, int end)
    Finds the index of the element within the specified range 在指定范围内查找元素索引
    static String
    join(String separator, byte... array)
    Joins array elements into a string 数组转字符串
    static int
    lastIndexOf(byte[] array, byte target)
    Finds the index of the element searching from the end 从后向前查找元素索引
    static void
    reverse(byte[] array)
    Reverses the array 反转数组
    static void
    reverse(byte[] array, int fromIndex, int toIndex)
    Reverses the specified range of the array 反转数组指定范围
    static byte[]
    subarray(byte[] array, int start, int end)
    Returns a sub-array 获取子数组
    static byte[]
    toArray(Collection<? extends Number> collection)
    Converts a Collection to an array 从 Collection 转为数组
    static int
    toUnsignedInt(byte value)
    Converts to an unsigned int 转为无符号 int
    static long
    toUnsignedLong(byte value)
    Converts to an unsigned long 转为无符号 long

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • BYTES

      public static final int BYTES
      See Also:
    • EMPTY_ARRAY

      public static final byte[] EMPTY_ARRAY
  • Method Details

    • concat

      public static byte[] concat(byte[]... arrays)
      Concatenates multiple arrays | 合并多个数组
      Parameters:
      arrays - the arrays to concatenate | 要合并的数组
      Returns:
      the concatenated array | 合并后的数组
    • contains

      public static boolean contains(byte[] array, byte target)
      Checks whether the array contains the specified element | 检查数组是否包含指定元素
      Parameters:
      array - the array to search | 要搜索的数组
      target - the element to find | 要查找的元素
      Returns:
      true if found | 找到返回 true
    • indexOf

      public static int indexOf(byte[] array, byte target)
      Finds the index of the specified element | 查找元素索引
      Parameters:
      array - the array to search | 要搜索的数组
      target - the element to find | 要查找的元素
      Returns:
      the index, or -1 if not found | 索引,未找到返回 -1
    • indexOf

      public static int indexOf(byte[] array, byte target, int start, int end)
      Finds the index of the element within the specified range 在指定范围内查找元素索引
    • indexOf

      public static int indexOf(byte[] array, byte[] target)
      Finds the position of a sub-array 查找子数组位置
    • lastIndexOf

      public static int lastIndexOf(byte[] array, byte target)
      Finds the index of the element searching from the end 从后向前查找元素索引
    • constrainToRange

      public static byte constrainToRange(byte value, byte min, byte max)
      Constrains the value to the specified range 约束值在指定范围内
    • toUnsignedInt

      public static int toUnsignedInt(byte value)
      Converts to an unsigned int 转为无符号 int
    • toUnsignedLong

      public static long toUnsignedLong(byte value)
      Converts to an unsigned long 转为无符号 long
    • asList

      public static List<Byte> asList(byte... array)
      Converts to a List 转为 List
    • toArray

      public static byte[] toArray(Collection<? extends Number> collection)
      Converts a Collection to an array 从 Collection 转为数组
    • reverse

      public static void reverse(byte[] array)
      Reverses the array 反转数组
    • reverse

      public static void reverse(byte[] array, int fromIndex, int toIndex)
      Reverses the specified range of the array 反转数组指定范围
    • compare

      public static int compare(byte[] a, byte[] b)
      Compares two byte arrays 比较两个字节数组
    • compareUnsigned

      public static int compareUnsigned(byte a, byte b)
      Unsigned comparison 无符号比较
    • equals

      public static boolean equals(byte[] a, byte[] b)
      Checks whether two byte arrays are equal 比较字节数组是否相等
    • join

      public static String join(String separator, byte... array)
      Joins array elements into a string 数组转字符串
    • ensureNonNull

      public static byte[] ensureNonNull(byte[] array)
    • hashCode

      public static int hashCode(byte[] array)
      Computes the hash code of a byte array 计算字节数组哈希码
    • subarray

      public static byte[] subarray(byte[] array, int start, int end)
      Returns a sub-array 获取子数组
    • ensureCapacity

      public static byte[] ensureCapacity(byte[] array, int minLength, int padding)
      Ensures that array has minimum capacity, expanding if needed. 确保数组具有最小容量,如果需要则扩展。
      Parameters:
      array - the original array | 原数组
      minLength - the minimum required length | 最小需要长度
      padding - additional padding beyond minLength | 超出 minLength 的额外填充
      Returns:
      the array with ensured capacity | 确保容量后的数组