Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

serializing a c++ object in java

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,

I have created a binary file using VC++.this file consists of data in terms of user-defined objects(structures).

Now I need to retrieve this data using java.since the objects were not serialized i am unable to do so.

Actually I need some function in java compatible to the following in C++

ifstream in;
in.open("filename");
in.read((char*)&objectname,sizeof(object));

please suggest some way in which i can do so in java.

thanking you ,
Abhijeet
 
Saloon Keeper
Posts: 25465
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Abhijeet Gholkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tim for u r response
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic