简体   繁体   English

java 的功能与 python 的结构类似

[英]Similar functionality for java to struct for python

I have a program that I made in Python to find specific tags in TIFF IFD's and return the values.我有一个在 Python 中制作的程序,用于在 TIFF IFD 中查找特定标签并返回值。 It was just a proof of concept thing in python, and now I need to move the functionality to java.这只是 python 中的概念证明,现在我需要将功能移至 java。 I think I can just use the String(byteArray[]) constructor for the ASCII data types, but I still need to get Unsigned short (2 byte) and unsigned long (4 byte) values.我想我可以只对 ASCII 数据类型使用String(byteArray[])构造函数,但我仍然需要获取Unsigned short (2 byte)unsigned long (4 byte)值。 I don't need to write them back to the file or modify them, all I need to do is get a Java Integer or Long object from them.我不需要将它们写回文件或修改它们,我需要做的就是从他们那里得到一个 Java IntegerLong object 。 This is easy in python with the struct and mmap classes, does any one know of a similar way in java?这在具有structmmap类的 python 中很容易,有人知道 java 中的类似方法吗? I looked at the DataInput class, but the readUnsignedLong method reads 8 bytes.我查看了DataInput class,但readUnsignedLong方法读取了 8 个字节。

DataInputStream allows you to read shorts and longs. DataInputStream允许您读取 short 和 long。 You should mask them with the appropriate bit mask ( 0xFFFF for short, 0xFFFFFFFF for 32 bit) in order to account for the difference between signed/unsigned types.您应该使用适当的位掩码(简称0xFFFF ,32 位为0xFFFFFFFF )对它们进行掩码,以说明有符号/无符号类型之间的差异。

eg例如

// omits error handling
FileInputStream fis = ...;
DataInputStream stream = new DataInputStream(fis);
int short_value = 0xFFFF & stream.readShort();
long long_value = 0xFFFFFFFF & stream.readInt();

If you're sure that the data won't be towards the high end of the 2 byte field, or 4 byte field, you can forego the bit masking.如果您确定数据不会接近 2 字节字段或 4 字节字段的高端,则可以放弃位掩码。 Otherwise, you need to use a wider data type to account for the fact that unsigned values hold a larger range of values than their signed counterparts.否则,您需要使用更广泛的数据类型来说明无符号值比有符号值包含更大范围的值这一事实。

I looked at the DataInput class, but the readUnsignedLong method reads 8 bytes.我查看了 DataInput class,但readUnsignedLong方法读取了 8 个字节。

Java does not have unsigned types. Java 没有无符号类型。 It takes 4 bytes to make an int , and 8 bytes to make a long , unsigned or otherwise.制作一个int需要 4 个字节,制作一个long 、无符号或其他类型需要 8 个字节。


If you don't want to use DataInput , you can read the bytes into byte arrays ( byte[] ) and use a ByteBuffer to turn those byte values into int s and long s with left padding.如果您不想使用DataInput ,您可以将字节读入字节 arrays ( byte[] ) 并使用ByteBuffer将这些字节值转换为int s 和long s 并带有左填充。 See ByteBuffer#getInt() and ByteBuffer#getLong() .请参阅ByteBuffer#getInt()ByteBuffer#getLong()

DataInput would be the preferred method. DataInput 将是首选方法。 You can use readUnsignedShort for the two byte values.您可以将 readUnsignedShort 用于两个字节值。 For the 4 byte values you'll have to use this workaround ...对于 4 字节值,您必须使用此解决方法...

long l = dis.readInt() & 0xffffffffL;

You could use Javolution's Struct class which provides structure to regions of data.您可以使用 Javolution 的Struct class 为数据区域提供结构。 You set up a wrapper and then use the wrapper to access the data.您设置了一个包装器,然后使用该包装器访问数据。 Simples.简单的。 Java really needs this super-useful class in its default classpath TBQH. Java 在其默认类路径 TBQH 中确实需要这个超级有用的 class。

Preon Library is good to create struct in Java. Preon 库很适合在 Java 中创建结构。 I have tried Javolution's Struct but it was not help full my case.我已经尝试过 Javolution 的 Struct,但这对我的情况没有帮助。 It is open source and very good library.它是开源的,非常好的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM