开发者

Accessing Java byte[] in C++

开发者 https://www.devze.com 2023-03-12 08:04 出处:网络
I have a Java application that persists byte[] structures to a DB (using Hibernate).I\'m writing a C++ application that reads these structures at a later time.

I have a Java application that persists byte[] structures to a DB (using Hibernate). I'm writing a C++ application that reads these structures at a later time.

As you might expect, I'm having problems.... The by开发者_如何学运维te[] structure written to the DB is longer than the original number of bytes - 27 bytes, by the looks of things.

Is there a way of determining (in C++) the byte[] header structure to properly find the beginning of the true data?

I spent some time looking at the JNI source code (GetArrayLength(jbytearray), etc.) to determine how that works, but got quickly mired in the vagaries of JVM code. yuck...

Ideas?


The object is probably being serialized using the Java Object Serialization Protocol. You can verify this by looking for the magic number 0xACED at the beginning. If this is the case, it's just wrapped with some meta information about the class and length, and you can easily parse the actual byte values off the end.

In particular, you would see 0xAC 0xED 0x00 0x05 for the header followed by a classDesc element that would be 0x75 ...bytes... 0x70, followed by a 4 byte length, and the then the bytes themselves. Java serializes the length and other multibyte values in big-endian format.

0

精彩评论

暂无评论...
验证码 换一张
取 消