如何处理Java程序中Java类文件的无符号类型(尤其是u4)? [英] How to handle the unsigned types (especially u4) of a Java class file in a Java program?
问题描述
来自 Java虚拟机规范 :
class
文件由8位字节的流组成.通过分别读取两个,四个和八个连续的8位字节来构造所有16位,32位和64位量.多字节数据项始终以高字节顺序存储,高字节在前.在Java平台中,接口java.io.DataInput和java.io.DataOutput
以及诸如java.io.DataInputStream和java.io.DataOutputStream之类的类都支持这种格式.
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.
本章定义了自己的代表class
文件数据的数据类型集:类型u1
,u2
和u4
分别表示无符号的1字节,2字节或4字节的数量.在Java平台中,可以通过接口java.io.DataInput
的readUnsignedByte
,readUnsignedShort
和readInt
之类的方法读取这些类型.
This chapter defines its own set of data types representing class
file data: The types u1
, u2
, and u4
represent an unsigned one-, two-, or four-byte quantity, respectively. In the Java platform, these types may be read by methods such as readUnsignedByte
, readUnsignedShort
, and readInt
of the interface java.io.DataInput
.
Aside from the irritating mentioning of "64-bit quantities" (there is no u8
, long and double are splitted in two u4
items), I don't understand how to handle the u4
type.
对于u1
和u2
,很明显:
-
u1
:用readUnsignedByte
读取,存储在int
中
-
u2
:用readUnsignedShort
读取,存储在int
中
u1
: read withreadUnsignedByte
, store in anint
u2
: read withreadUnsignedShort
, store in anint
规范建议:
-
u4
:用readInt
读取,存储在int
(?) 中
u4
: read withreadInt
, store in anint
(?)
大于 u4
类型的值都小于或等于Integer.MAX_VALUE
?
What happens to values greater than Integer.MAX_VALUE
? Does this advice silently imply that all values of type u4
are less than or equal to Integer.MAX_VALUE
?
我想到了这个主意:
-
u4
:用readUnsignedInt
读取,存储在long
中
u4
: read withreadUnsignedInt
, store in along
不幸的是,没有这样的方法.但这不是问题,因为您可以轻松编写自己的内容:
Unfortunalety, there is no such method. But that's not the problem, since you can easily write your own:
public long readUnsignedInt() throws IOException {
return readInt() & 0xFFFFFFFFL;
}
所以,这里有两个值得怀疑的地方:
So, here are two questionable spots:
-
代码属性:
Code_attribute {
...
u4 code_length;
u1代码[code_length];
...
}
Code_attribute {
...
u4 code_length;
u1 code[code_length];
...
}
为什么code_length
不是u2
类型? 稍后说 :
Why is code_length
not of type u2
? Later it says:
code_length
项的值必须小于65536.
The value of the
code_length
item must be less than 65536.
SourceDebugExtension_attribute {
...
u4 attribute_length;
u1 debug_extension [attribute_length];
}
...
请注意,debug_extension
数组表示的字符串可能比用String
类的实例表示的字符串长.
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
实例的最大长度)?
Why? Can u4
values indeed exceed Integer.MAX_VALUE
(since I think this is the maximum length of a String
instance)?
推荐答案
- 如有必要,轻松解除64K代码长度限制.
- 由于没有提到u4值不能超过Integer.MAX_VALUE,因此必须假定u4值可以超过Integer.MAX_VALUE. JVM规范不隐含任何内容.
这篇关于如何处理Java程序中Java类文件的无符号类型(尤其是u4)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!