posted 21 years ago
Pretty much just the way you've shown it except for one small detail.
A lot people don't realize that there are data structures and there are storage structures. Data Structures are abstract objects and are simply containers for data. How the data is stored internally is immaterial.
Storage Structures, on the other hand are expected to map specifically into memory. The C struct is a storage structure in actual usage in many cases. Developers fine-tune the order and type of data members in order to get the best in hardware performance and memory efficiency. A C++ struct (being a degenerate form of C++ class) is more often a data structure, since when you start putting virtuality in a class, it's quite difficult to predict where everything will land.
Storage structures are very dependent on the OS, hardware and compiler. Data structures are not. If I were to attempt to read your C-generated storage structure back in on my old Amiga computer, it'd probably come out all wrong. Not only would there be differences in padding, but if you'd compiled it on a PC AT back in the days when I actively used my Amiga, I'd be expecting 32-bit integers and you'd be sending 16-bit integers.
Then there's the infamous byte-ordering problem. On the Intel i86 platform integers are stored in a bytewise-discontinuous mode. On the Amiga - and on all IBM mainframes integers are stored bytewise-continuous. This situation has led to the creation of what are known as "swabbers".
OK, now that I've shot any hope of getting a Java (data-structure based) program to reliably read in a storage structure, here's what you can do. Either:
1. Write Java code to pick the data out of a byte buffer and reassemble it.
OR
2. Write another C module to read it in and convert it using JNI as your glue.
Actually, I don't recommend storing data in binary form these days. Neither disk nor network bandwidth is that scare a resource anymore. Text data formats not only are easy to translate across programming languages and OS's, but you won't have as many problems trying to make sense of old files 10 years from now.
Finally, have you ever had a Windows machine die while it was working on something critical? CHKDISK finds all the fragments of files that were in the process of being modified and puts them into files named FILE0001.CHK, FILE0002.CHL, etc. When the file in question is a bianry file, usually all you can do is delete them, since the completeness and ordering of these files is rarely good enough to be salvageable. Text, however, is still text, long after COM objects are forgotten. You may not get all the data back, but at least you'll have less problems identifying the pieces and in attempting to stitch them back together.
So even
Often the most important part of the news is what they didn't tell.