Is it possible to decode a java serialized .dat file in C?
I have a file in a java project which I read as follows in java project:
FileInputStream in = new FileInputStream(.dat file path);
ObjectInputStream si = new ObjectInputStream(in);
si.readUTF();
si.readBoolean()
int num=si.readInt();
How can I convert this code to C? I know the first character is a utf character, the second one is boolean and the third is int.
unsigned char buf[16];
FILE *fp = fopen((const char*)[filePath UTF8String], "rb");
fread(buf, 4, 1, fp); // read 4 bytes
printf("%s", buf);
The official Javadocs for the
DataInputinterface specify exactly whatreadUTF,readBoolean, andreadIntread from the stream. Just implement those in C, following the descriptions exactly, and you’ll be fine.I see you’re using Objective-C; for
readUTFin particular you can construct NSString after you’ve read the appropriate number of bytes using[[NSString alloc] initWithBytes:byteArray length:length encoding:NSUTF8StringEncoding].By the way, your Java code is being wasteful: it is using an
ObjectInputStreamwhen it really only needs aDataInputStream, which is much simpler and provides the same methods you’re using. (This came to my notice in particular because if you had been using the intended functionality ofObjectInputStream, i.e. reading serialized Java objects, then it would be much harder to write decoding in C, because the format is much more complex and designed for use by systems which include the Java standard libraries.)