如何处理Java程序中Java类文件的无符号类型(尤其是u4)? [英] How to handle the unsigned types (especially u4) of a Java class file in a Java program?

查看:176
本文介绍了如何处理Java程序中Java类文件的无符号类型(尤其是u4)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 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 and java.io.DataOutput and classes such as java.io.DataInputStream and java.io.DataOutputStream.

本章定义了自己的代表class文件数据的数据类型集:类型u1u2u4分别表示无符号的1字节,2字节或4字节的数量.在Java平台中,可以通过接口java.io.DataInputreadUnsignedBytereadUnsignedShortreadInt之类的方法读取这些类型.

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.

除了令人讨厌的"64位数量"提及(没有u8

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.

对于u1u2,很明显:

  • u1:用readUnsignedByte读取,存储在int
  • u2:用readUnsignedShort读取,存储在int
  • u1: read with readUnsignedByte, store in an int
  • u2: read with readUnsignedShort, store in an int

规范建议:

  • u4:用readInt读取,存储在int(?)
  • u4: read with readInt, store in an int (?)

大于 ?这个建议是否默默地暗示所有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 with readUnsignedInt, store in a long

不幸的是,没有这样的方法.但这不是问题,因为您可以轻松编写自己的内容:

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:

  1. 代码属性:

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属性:

    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 the debug_extension array may denote a string longer than that which can be represented with an instance of class String.

    为什么? 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)?

    推荐答案

    1. 如有必要,轻松解除64K代码长度限制.
    2. 由于没有提到u4值不能超过Integer.MAX_VALUE,因此必须假定u4值可以超过Integer.MAX_VALUE. JVM规范不隐含任何内容.

    这篇关于如何处理Java程序中Java类文件的无符号类型(尤其是u4)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆