Similar functionality for java for struct for python

I have a program that I made in Python to find specific tags in a TIFF IFD and return values. This was just a proof of concept in python, and now I need to move the functionality to java. I think I can just use the constructor String(byteArray[])for ASCII data types, but I still need to get the Unsigned short (2 byte)and values unsigned long (4 byte). I don’t need to write them back to a file or modify them, all I have to do is get a Java object from them Integeror Long. This is easy in python with classes structand mmapdoes anyone know similar in java? I looked at the class DataInput, but the method readUnsignedLongreads 8 bytes.

+5
source share
5 answers

DataInputStream allows you to read short and long. You must mask them with the appropriate bitmask ( 0xFFFFfor brevity, 0xFFFFFFFFfor 32 bits) to account for the difference between signed / unsigned types.

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 are sure that the data will not be sent to the upper end of the 2 byte or 4 byte field, you can refuse to mask the bits. Otherwise, you need to use a wider data type to account for the fact that unsigned values ​​contain a larger range of values ​​than their signed copies.

+5
source

I looked at the DataInput class, but the method readUnsignedLongreads 8 bytes.

Java . int 8 4 , long, .


DataInput, (byte[]) ByteBuffer, int long . . ByteBuffer#getInt() ByteBuffer#getLong().

+2

DataInput . readUnsignedShort . 4 ...

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

Javolution Struct, . , . Simples. Java - TBQH .

+1

Preon Library is good for creating structure in Java. I tried Javolution Struct, but it did not help me fully. It is open source and a very good library.

+1
source

All Articles