Class HexDump

java.lang.Object
cloud.opencode.base.io.hex.HexDump

public final class HexDump extends Object
Hex Dump Utility 十六进制转储工具

Utility class for formatting binary data as hexadecimal dump output. Produces standard hex dump format with offset, hex bytes, and ASCII representation.

用于将二进制数据格式化为十六进制转储输出的工具类。 生成包含偏移量、十六进制字节和ASCII表示的标准十六进制转储格式。

Features | 主要功能:

  • Standard hex dump format (16 bytes per line) - 标准十六进制转储格式(每行16字节)
  • Format byte arrays, InputStreams, and files - 格式化字节数组、输入流和文件
  • Hex encoding/decoding (toHex/fromHex) - 十六进制编码/解码
  • Configurable read limits for streams and files - 可配置的流和文件读取限制

Usage Examples | 使用示例:

// Format a byte array as hex dump
String dump = HexDump.format("Hello, World!".getBytes());
// 00000000  48 65 6C 6C 6F 2C 20 57  6F 72 6C 64 21           |Hello, World!|

// Convert to/from hex string
String hex = HexDump.toHex(new byte[]{0x0A, 0x0B});  // "0a0b"
byte[] data = HexDump.fromHex("0a0b");                // {0x0A, 0x0B}

Security | 安全性:

  • Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具类)
  • Input validation on all methods - 所有方法均有输入验证
Since:
JDK 25, opencode-base-io V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Number of bytes displayed per line in hex dump format 十六进制转储格式中每行显示的字节数
    static final int
    Default maximum bytes to read for unbounded operations (1 MB) 无界操作的默认最大读取字节数(1 MB)
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    format(byte[] data)
    Formats an entire byte array as hex dump 将整个字节数组格式化为十六进制转储
    static String
    format(byte[] data, int offset, int length)
    Formats a range of a byte array as hex dump 将字节数组的指定范围格式化为十六进制转储
    static String
    Formats data from an InputStream as hex dump, reading up to MAX_DEFAULT_BYTES 从InputStream格式化数据为十六进制转储,最多读取MAX_DEFAULT_BYTES字节
    static String
    format(InputStream input, int maxBytes)
    Formats data from an InputStream as hex dump with a byte limit 从InputStream格式化数据为十六进制转储,带字节限制
    static String
    format(Path path)
    Formats the contents of a file as hex dump, reading up to MAX_DEFAULT_BYTES 将文件内容格式化为十六进制转储,最多读取MAX_DEFAULT_BYTES字节
    static String
    format(Path path, long offset, int length)
    Formats a range of a file as hex dump 将文件的指定范围格式化为十六进制转储
    static byte[]
    Parses a hex string back to a byte array 将十六进制字符串解析回字节数组
    static String
    toHex(byte[] data)
    Converts a byte array to a lowercase hex string (no formatting) 将字节数组转换为小写十六进制字符串(无格式化)
    static String
    toHex(byte[] data, int offset, int length)
    Converts a range of a byte array to a lowercase hex string 将字节数组的指定范围转换为小写十六进制字符串
    static String
    toHexUpper(byte[] data)
    Converts a byte array to an uppercase hex string (no formatting) 将字节数组转换为大写十六进制字符串(无格式化)

    Methods inherited from class Object

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

    • BYTES_PER_LINE

      public static final int BYTES_PER_LINE
      Number of bytes displayed per line in hex dump format 十六进制转储格式中每行显示的字节数
      See Also:
    • MAX_DEFAULT_BYTES

      public static final int MAX_DEFAULT_BYTES
      Default maximum bytes to read for unbounded operations (1 MB) 无界操作的默认最大读取字节数(1 MB)
      See Also:
  • Method Details

    • format

      public static String format(byte[] data)
      Formats an entire byte array as hex dump 将整个字节数组格式化为十六进制转储
      Parameters:
      data - the byte array to format | 要格式化的字节数组
      Returns:
      hex dump string | 十六进制转储字符串
      Throws:
      NullPointerException - if data is null | 当data为null时抛出
    • format

      public static String format(byte[] data, int offset, int length)
      Formats a range of a byte array as hex dump 将字节数组的指定范围格式化为十六进制转储
      Parameters:
      data - the byte array | 字节数组
      offset - the start offset | 起始偏移量
      length - the number of bytes to format | 要格式化的字节数
      Returns:
      hex dump string | 十六进制转储字符串
      Throws:
      NullPointerException - if data is null | 当data为null时抛出
      IllegalArgumentException - if range is invalid | 当范围无效时抛出
    • format

      public static String format(InputStream input)
      Formats data from an InputStream as hex dump, reading up to MAX_DEFAULT_BYTES 从InputStream格式化数据为十六进制转储,最多读取MAX_DEFAULT_BYTES字节
      Parameters:
      input - the input stream | 输入流
      Returns:
      hex dump string | 十六进制转储字符串
      Throws:
      NullPointerException - if input is null | 当input为null时抛出
      OpenIOOperationException - if an IO error occurs | 当IO错误发生时抛出
    • format

      public static String format(InputStream input, int maxBytes)
      Formats data from an InputStream as hex dump with a byte limit 从InputStream格式化数据为十六进制转储,带字节限制
      Parameters:
      input - the input stream | 输入流
      maxBytes - maximum bytes to read | 最大读取字节数
      Returns:
      hex dump string | 十六进制转储字符串
      Throws:
      NullPointerException - if input is null | 当input为null时抛出
      IllegalArgumentException - if maxBytes is negative | 当maxBytes为负时抛出
      OpenIOOperationException - if an IO error occurs | 当IO错误发生时抛出
    • format

      public static String format(Path path)
      Formats the contents of a file as hex dump, reading up to MAX_DEFAULT_BYTES 将文件内容格式化为十六进制转储,最多读取MAX_DEFAULT_BYTES字节
      Parameters:
      path - the file path | 文件路径
      Returns:
      hex dump string | 十六进制转储字符串
      Throws:
      NullPointerException - if path is null | 当path为null时抛出
      OpenIOOperationException - if an IO error occurs or file not found | 当IO错误或文件未找到时抛出
    • format

      public static String format(Path path, long offset, int length)
      Formats a range of a file as hex dump 将文件的指定范围格式化为十六进制转储
      Parameters:
      path - the file path | 文件路径
      offset - the byte offset to start reading from | 开始读取的字节偏移量
      length - the number of bytes to read | 要读取的字节数
      Returns:
      hex dump string | 十六进制转储字符串
      Throws:
      NullPointerException - if path is null | 当path为null时抛出
      IllegalArgumentException - if offset or length is negative | 当offset或length为负时抛出
      OpenIOOperationException - if an IO error occurs | 当IO错误发生时抛出
    • toHex

      public static String toHex(byte[] data)
      Converts a byte array to a lowercase hex string (no formatting) 将字节数组转换为小写十六进制字符串(无格式化)
      Parameters:
      data - the byte array | 字节数组
      Returns:
      lowercase hex string | 小写十六进制字符串
      Throws:
      NullPointerException - if data is null | 当data为null时抛出
    • toHex

      public static String toHex(byte[] data, int offset, int length)
      Converts a range of a byte array to a lowercase hex string 将字节数组的指定范围转换为小写十六进制字符串
      Parameters:
      data - the byte array | 字节数组
      offset - the start offset | 起始偏移量
      length - the number of bytes | 字节数
      Returns:
      lowercase hex string | 小写十六进制字符串
      Throws:
      NullPointerException - if data is null | 当data为null时抛出
      IllegalArgumentException - if range is invalid | 当范围无效时抛出
    • toHexUpper

      public static String toHexUpper(byte[] data)
      Converts a byte array to an uppercase hex string (no formatting) 将字节数组转换为大写十六进制字符串(无格式化)
      Parameters:
      data - the byte array | 字节数组
      Returns:
      uppercase hex string | 大写十六进制字符串
      Throws:
      NullPointerException - if data is null | 当data为null时抛出
    • fromHex

      public static byte[] fromHex(String hex)
      Parses a hex string back to a byte array 将十六进制字符串解析回字节数组

      The hex string must have even length and contain only valid hex characters (0-9, a-f, A-F).

      十六进制字符串必须为偶数长度,且仅包含有效的十六进制字符(0-9, a-f, A-F)。

      Parameters:
      hex - the hex string | 十六进制字符串
      Returns:
      decoded byte array | 解码后的字节数组
      Throws:
      NullPointerException - if hex is null | 当hex为null时抛出
      IllegalArgumentException - if hex has odd length or contains invalid characters | 当hex长度为奇数或包含无效字符时抛出