来自Java Virtual Machine specification :
A
class
file consists of a stream of 8-bit bytes. All 16-bit, 32-bit, and 64-bit quantities are constructed by reading in two, four, and eight consecutive 8-bit bytes, respectively. Multibyte data items are always stored in big-endian order, where the high bytes come first. In the Java platform, this format is supported by interfaces java.io.DataInput andjava.io.DataOutput
and classes such as java.io.DataInputStream and java.io.DataOutputStream.This chapter defines its own set of data types representing
class
file data: The typesu1
,u2
, andu4
represent an unsigned one-, two-, or four-byte quantity, respectively. In the Java platform, these types may be read by methods such asreadUnsignedByte
,readUnsignedShort
, andreadInt
of the interfacejava.io.DataInput
.
除了令人恼火地提及“64 位数量”(没有 u8
,long and double 分为两个 u4
项)之外,我不明白如何处理 u4
类型。
对于u1
和u2
很清楚:
u1
:使用readUnsignedByte
读取,存储在int
u2
:使用readUnsignedShort
读取,存储在int
规范建议:
u4
:使用readInt
读取,存储在int
中(?)
大于 Integer.MAX_VALUE
的值会发生什么情况?此建议是否默默暗示 u4
类型的所有值都小于或等于 Integer.MAX_VALUE
?
我想出了这个主意:
u4
:使用readUnsignedInt
读取,存储在long
不幸,there is no such method 。但这不是问题,因为您可以轻松编写自己的:
public long readUnsignedInt() throws IOException {
return readInt() & 0xFFFFFFFFL;
}
所以,这里有两个有问题的地方:
-
Code_attribute {
...
u4 code_length;
u1 code[code_length];
...
}为什么
code_length
不是u2
类型? Later it says :The value of the
code_length
item must be less than 65536. SourceDebugExtension attribute :
SourceDebugExtension_attribute {
...
u4 attribute_length;
u1 debug_extension[attribute_length];
}
...
Note that thedebug_extension
array may denote a string longer than that which can be represented with an instance of classString
.u4
值确实可以超过Integer.MAX_VALUE
(因为我认为这是String
实例的最大长度)?
最佳答案
- 在需要时轻松解除 64K 代码长度限制。
- 由于没有提及 u4 值不能超过 Integer.MAX_VALUE,因此必须假设 u4 值可以超过 Integer.MAX_VALUE。 JVM 规范没有留下任何隐含的内容。
关于java - 如何在Java程序中处理Java类文件的无符号类型(尤其是u4)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11660967/