Class HexDump
java.lang.Object
cloud.opencode.base.io.hex.HexDump
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
FieldsModifier and TypeFieldDescriptionstatic final intNumber of bytes displayed per line in hex dump format 十六进制转储格式中每行显示的字节数static final intDefault maximum bytes to read for unbounded operations (1 MB) 无界操作的默认最大读取字节数(1 MB) -
Method Summary
Modifier and TypeMethodDescriptionstatic Stringformat(byte[] data) Formats an entire byte array as hex dump 将整个字节数组格式化为十六进制转储static Stringformat(byte[] data, int offset, int length) Formats a range of a byte array as hex dump 将字节数组的指定范围格式化为十六进制转储static Stringformat(InputStream input) Formats data from an InputStream as hex dump, reading up to MAX_DEFAULT_BYTES 从InputStream格式化数据为十六进制转储,最多读取MAX_DEFAULT_BYTES字节static Stringformat(InputStream input, int maxBytes) Formats data from an InputStream as hex dump with a byte limit 从InputStream格式化数据为十六进制转储,带字节限制static StringFormats the contents of a file as hex dump, reading up to MAX_DEFAULT_BYTES 将文件内容格式化为十六进制转储,最多读取MAX_DEFAULT_BYTES字节static StringFormats a range of a file as hex dump 将文件的指定范围格式化为十六进制转储static byte[]Parses a hex string back to a byte array 将十六进制字符串解析回字节数组static StringtoHex(byte[] data) Converts a byte array to a lowercase hex string (no formatting) 将字节数组转换为小写十六进制字符串(无格式化)static StringtoHex(byte[] data, int offset, int length) Converts a range of a byte array to a lowercase hex string 将字节数组的指定范围转换为小写十六进制字符串static StringtoHexUpper(byte[] data) Converts a byte array to an uppercase hex string (no formatting) 将字节数组转换为大写十六进制字符串(无格式化)
-
Field Details
-
BYTES_PER_LINE
public static final int BYTES_PER_LINENumber of bytes displayed per line in hex dump format 十六进制转储格式中每行显示的字节数- See Also:
-
MAX_DEFAULT_BYTES
public static final int MAX_DEFAULT_BYTESDefault maximum bytes to read for unbounded operations (1 MB) 无界操作的默认最大读取字节数(1 MB)- See Also:
-
-
Method Details
-
format
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
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
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
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
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
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
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
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
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
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长度为奇数或包含无效字符时抛出
-