From the Java Virtual Machine specification:
A
classfile 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.DataOutputand classes such as java.io.DataInputStream and java.io.DataOutputStream.
This chapter defines its own set of data types representing
classfile data: The typesu1,u2, andu4represent an unsigned one-, two-, or four-byte quantity, respectively. In the Java platform, these types may be read by methods such asreadUnsignedByte,readUnsignedShort, andreadIntof the interfacejava.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.
For u1 and u2 it’s clear:
u1: read withreadUnsignedByte, store in anintu2: read withreadUnsignedShort, store in anint
The specification advises this:
u4: read withreadInt, store in anint(?)
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?
I came up with this idea:
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:
- The Code attribute:
Code_attribute {
…
u4 code_length;
u1 code[code_length];
…
}
Why is code_length not of type u2? Later it says:
The value of the
code_lengthitem must be less than 65536.
SourceDebugExtension_attribute {
…
u4 attribute_length;
u1 debug_extension[attribute_length];
}
…
Note that thedebug_extensionarray may denote a string longer than that which can be represented with an instance of classString.
Why? Can u4 values indeed exceed Integer.MAX_VALUE (since I think this is the maximum length of a String instance)?
1 Answer