我正在尝试通过 java.nio.ByteBuffer 读取 UTF8 字符串。大小是一个未分割的 int,当然,Java 没有。我已将值读入 long 以便我拥有该值。
我遇到的下一个问题是我无法使用 long 创建一个字节数组,并且将他的 long 转换回 int 会导致它被签名。
我也尝试在缓冲区上使用 limit() ,但它同样适用于 int 不长。
我正在做的具体事情是从类文件中读取 UTF8 字符串,因此缓冲区中的内容不仅仅是 UTF8 字符串。
关于如何从 ByteBuffer 读取可能长度为 unsigned int 的 UTF8 字符串的任何想法。
编辑:
SourceDebugExtension_attribute {
u2 attribute_name_index;
u4 attribute_length;
u1 debug_extension[attribute_length];
}
attribute_name_index
The value of the attribute_name_index item must be a valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Utf8_info structure representing the string "SourceDebugExtension".
attribute_length
The value of the attribute_length item indicates the length of the attribute, excluding the initial six bytes. The value of the attribute_length item is thus the number of bytes in the debug_extension[] item.
debug_extension[]
The debug_extension array holds a string, which must be in UTF-8 format. There is no terminating zero byte.
The string in the debug_extension item will be interpreted as extended debugging information. The content of this string has no semantic effect on the Java Virtual Machine.
因此,从技术角度来看,类文件中可能有一个长度为完整 u4(无符号,4 个字节)的字符串。
如果 UTF8 字符串的大小有限制,这些都不是问题(我不是 UTF8 专家,所以可能有这样的限制)。
我可以坚持下去并接受这样一个现实,即不会有这么长的字符串......