建成300的JDK 17月初访问构建包括用于执行JDK-8251989(“十六进制格式和分析工具”)。这个新引入的用于解析和格式化十六进制值的功能封装在新类中java.util.HexFormat,是本文的主题。
针对新类运行javapjava.util.HexFormat提供了一种简单的方法来查看其API概述。通过运行生成以下输出javap java.util.HexFormat:
Compiled from "HexFormat.java" public final class java.util.HexFormat { static final boolean $assertionsDisabled; public static java.util.HexFormat of(); public static java.util.HexFormat ofDelimiter(java.lang.String); public java.util.HexFormat withDelimiter(java.lang.String); public java.util.HexFormat withPrefix(java.lang.String); public java.util.HexFormat withSuffix(java.lang.String); public java.util.HexFormat withUpperCase(); public java.util.HexFormat withLowerCase(); public java.lang.String delimiter(); public java.lang.String prefix(); public java.lang.String suffix(); public boolean isUpperCase(); public java.lang.String formatHex(byte[]); public java.lang.String formatHex(byte[], int, int); public <A extends java.lang.Appendable> A formatHex(A, byte[]); public <A extends java.lang.Appendable> A formatHex(A, byte[], int, int); public byte[] parseHex(java.lang.CharSequence); public byte[] parseHex(java.lang.CharSequence, int, int); public byte[] parseHex(char[], int, int); public char toLowHexDigit(int); public char toHighHexDigit(int); public <A extends java.lang.Appendable> A toHexDigits(A, byte); public java.lang.String toHexDigits(byte); public java.lang.String toHexDigits(char); public java.lang.String toHexDigits(short); public java.lang.String toHexDigits(int); public java.lang.String toHexDigits(long); public java.lang.String toHexDigits(long, int); public boolean isHexDigit(int); public int fromHexDigit(int); public int fromHexDigits(java.lang.CharSequence); public int fromHexDigits(java.lang.CharSequence, int, int); public long fromHexDigitsToLong(java.lang.CharSequence); public long fromHexDigitsToLong(java.lang.CharSequence, int, int); public boolean equals(java.lang.Object); public int hashCode(); public java.lang.String toString(); static {}; }
上面显示的javap生成的清单表明,有两种static工厂方法可用于获取HexFormat:HexFormat.of()和的实例HexFormat.ofDelimiter(String)。这两个工厂方法都HexFormat使用“预设参数”指定的实例。其余public方法是实例方法,通常用于五类操作之一:
toString(),equals(Object),hashCode()
JavadocHexFormat
在我看到的HexFormat 类源代码的最后一个版本中,它是在广告“ @since 16 ”,这是在实施,检查和合并反馈方面投入该类工作的证据(第33提交是另一项证据)。在官方发布的HexFormat其实是JDK 17,但JDK 17月初访问API文档仍显示“ @Since 16 ”写这篇文章的。
在本文中,我提供了一些简单的应用示例,HexFormat这些代码清单可在GitHub上找到。幸运的是,基于Javadoc的类级API文档提供了Applied的非常好的示例HexFormat。当类的Javadoc展示了如何应用这些类的示例,并且HexFormat文档很好地涵盖了使用该类的许多方面时,我喜欢它。我的示例将覆盖该类API的一小部分,并且仅作为对该类基本可用性的介绍。
获取实例 HexFormat
有两种static获取的实例的方法,HexFormat此处演示了其中一种:
java
/** Instance of {@link HexFormat} used in this demonstration. */ private static final HexFormat HEX_FORMAT_UPPER_CASE = HexFormat.of().withUpperCase();
该withUpperCase()方法指示实例HexFormat“使用大写十六进制字符”(“ 0-9”,“ AF”)。
withUpperCase()
HexFormat
将整数转换为十六进制
接下来显示的代码段演示了如何使用HexFormat.toHexDigits():
/** * Demonstrates use of {@link HexFormat#toHexDigits(int)}. */ public void demoIntegerToHexadecimal() { for (int integerValue = 0; integerValue < 17; integerValue++) { out.println("Hexadecimal representation of integer " + integerValue + ": '" + HEX_FORMAT_UPPER_CASE.toHexDigits(integerValue) + "'."); } }
执行以上代码段后,输出如下所示:
text
Hexadecimal representation of integer 0: '00000000'. Hexadecimal representation of integer 1: '00000001'. Hexadecimal representation of integer 2: '00000002'. Hexadecimal representation of integer 3: '00000003'. Hexadecimal representation of integer 4: '00000004'. Hexadecimal representation of integer 5: '00000005'. Hexadecimal representation of integer 6: '00000006'. Hexadecimal representation of integer 7: '00000007'. Hexadecimal representation of integer 8: '00000008'. Hexadecimal representation of integer 9: '00000009'. Hexadecimal representation of integer 10: '0000000A'. Hexadecimal representation of integer 11: '0000000B'. Hexadecimal representation of integer 12: '0000000C'. Hexadecimal representation of integer 13: '0000000D'. Hexadecimal representation of integer 14: '0000000E'. Hexadecimal representation of integer 15: '0000000F'. Hexadecimal representation of integer 16: '00000010'.
示范 HexFormat.isHexDigit(int)
以下代码演示HexFormat.isHexDigit(int):
HexFormat.isHexDigit(int):
/** * Demonstrates use of {@link HexFormat#isHexDigit(int)}. */ public void demoIsHex() { for (char characterValue = 'a'; characterValue < 'i'; characterValue++) { out.println("Is character '" + characterValue + "' a hexadecimal value? " + HEX_FORMAT_UPPER_CASE.isHexDigit(characterValue)); } for (char characterValue = 'A'; characterValue < 'I'; characterValue++) { out.println("Is character '" + characterValue + "' a hexadecimal value? " + HEX_FORMAT_UPPER_CASE.isHexDigit(characterValue)); } }
这是运行上述代码片段的输出:
Is character 'a' a hexadecimal value? true Is character 'b' a hexadecimal value? true Is character 'c' a hexadecimal value? true Is character 'd' a hexadecimal value? true Is character 'e' a hexadecimal value? true Is character 'f' a hexadecimal value? true Is character 'g' a hexadecimal value? false Is character 'h' a hexadecimal value? false Is character 'A' a hexadecimal value? true Is character 'B' a hexadecimal value? true Is character 'C' a hexadecimal value? true Is character 'D' a hexadecimal value? true Is character 'E' a hexadecimal value? true Is character 'F' a hexadecimal value? true Is character 'G' a hexadecimal value? false Is character 'H' a hexadecimal value? false
示范 HexFormat.toString()
的HexFormat类提供的覆盖版本Object.toString()方法并且这表现在下面的代码段和相应的输出从运行的代码段。
/** * Demonstrates string representation of instance of * {@link HexFormat}. * * The {@link HexFormat#toString()} method provides a string * that shows the instance's parameters (not class name): * "uppercase", "delimiter", "prefix", and "suffix" */ public void demoToString() { out.println("HexFormat.toString(): " + HEX_FORMAT_UPPER_CASE); }
HexFormat.toString(): uppercase: true, delimiter: "", prefix: "", suffix: ""
其他例子 HexFormat
基于Javadoc的类级文档,HexFormat其中包含有关如何应用此类的更多示例。这些示例演示了实例化方法HexFormat.of()和HexFormat.ofDelimiter(String);展示的实用方法toHexDigit(byte),fromHexDigits(CharSequence),formatHex(byte[]),和parseHex(String); 并演示实例专门化方法withUpperCase()和withPrefix(String)。我喜欢后面的示例是在实际情况下(例如使用字节指纹)如何使用操作的“现实”示例。
Javadoc
toHexDigit(byte)
fromHexDigits(CharSequence),formatHex(byte[])
parseHex(String)
withPrefix(String)
JDK的用途 HexFormat
JDK及其测试已使用 HexFormat。以下是一些示例。
原文链接:http://codingdict.com