• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading a Pascal record file into Java  RSS feed

 
Christopher Arthur
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to read a file off of a disk that is a Pascal record? I have the source code in pascal that defines the record. I ported it to a C struct and can read the same file in C now, but I would like to do it with Java. My plan is to make a class that has instance variables in the same order and of approximate type and just read in the file in an object stream. In order for it to work in C (I am by no means a C expert) I had to play around with the PRAGMA directive, but I guess we don't have that kind of control in Java. Since the file is binary, the last thing I want to do is read it in as binary data then parse the binary data myself. Any suggestions?
 
Christopher Arthur
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way...the PRAMGA directive that I had to experiment with was 'pack'..as I was told that if the boundaries of the words aren't set just right, then the fields might not get the right bytes in the right place.
Chris
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm no expert either, but one point - it'd be pretty much impossible to make an object stream work, since Java object serialisation format is completely different to that used anywhere else.
I'd guess parsing the binary data is your best (well, least-bad) option - Java's crazy zoo of input streams should make this reasonably painless I'd have thought. It does have the potential to raise some interesting questions about the exact format of the data you're reading...stuff like endian issues
Cheers,
--Tim
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and don't even attempt to go into interpreting non-primitives written to the outputfile...
 
Christopher Arthur
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just another idea that perhaps someone could help to develop. Since I already have a C file that reads in the binary data, could I somehow expand that C file into a converter to store the data in some other format that would be more easily read by java? Each record is about 14KB and is mostly strings, doubles, longs and a lot of arrays of them. I guess I could do something like change it all into comma delimited fields in a text file. Then my java program could call the C program to run. Perhaps there is an even better option?
Chris
 
Christopher Arthur
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JNI! It must be JNI! I've never used it before, but if JNI has type conversion methods, then it definitely seems like the way to go.
Chris
 
Igor Ko
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you know very good about EACH BYTE in your record, you can
open file as byte stream, and read the data and convert it
to corresponding integer, double, string.
(for example integer in your record can be 2 or 4 bytes, probably
you should swap bytes (use bits operations) String represent as
fixed size bytes sequence, or character counter (2 bytes) and then
bytes, or it is bytes zero terminated sequence... etc.
But it isn't so simple.
So if you can wrote the record, not as binary representation of
(int, double, strings) but as text representation, of course you
can easily read it in java.
 
Christopher Arthur
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I know all the fields in the record. Adding up all the bytes by hand, I get something around 12KB per record, but I know that on disk the record takes up 14KB. That's 2000 bytes unaccounted. I know where they are though, they're between fields. Adjusting the PRAGMA pack(n) directive in C tunes the read so that the gaps are in the right spot, but figuring out exactly where the gaps are would be a horrible endeavor to do by hand.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!