jQuery in Action, 2nd edition*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes How to read MAGIC for db-2x2.db Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "How to read MAGIC for db-2x2.db" Watch "How to read MAGIC for db-2x2.db" New topic
Author

How to read MAGIC for db-2x2.db

Jofer Chan
Ranch Hand

Joined: Sep 09, 2003
Posts: 49
The format of data in the database file is as follows:
Start of file
4 byte numeric, magic cookie value identifies this as a data file
4 byte numeric, offset to start of record zero
2 byte numeric, number of fields in each record
How to read MAGIC and what mean about the "4 byte numeric"?
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
Hi Jofer,
How to read MAGIC and what mean about the "4 byte numeric"?

"4 byte numeric" means "int" and you can read it using java.io.RandomAccessFile.readInt().
Best,
Phil.
Jofer Chan
Ranch Hand

Joined: Sep 09, 2003
Posts: 49
Hi,Philippe:
"2 byte numeric" also uses readInt() method?
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
For "2 byte numeric", use readShort() instead.
Best,
Phil.
Dave Knipp
Ranch Hand

Joined: Oct 14, 2003
Posts: 146
Can you use java.io.RandomAccessFile.readInt() and readShort() to read in the db file? I'm asking because sun seemed to be rather specific in their instructions on the format used to read and write from the file: All numeric values are stored in the header information use the formats of the DataInputStream and DataOutputStream classes. All text values, and all fields (which are text only), contain only 8 bit characters, null terminated if less than the maximum length for the field. The character encoding is 8 bit US ASCII. RandomAccessFile implements DataInput/DataOutput just as DataInputStream/DataOuputStream do, but will using RandomAccessFile fulfill the requirement to use the formats within DataInputStream/DataOuputStream??
Dave


SCJP 1.4, SCJD 1.4, SCWCD 1.3, SCBCD 1.3, IBM Certified Solution Developer -WebSphere Studio V5.0
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
Hi Dave,
RandomAccessFile implements DataInput/DataOutput just as DataInputStream/DataOuputStream do, but will using RandomAccessFile fulfill the requirement to use the formats within DataInputStream/DataOuputStream??

As RandomAccessFile implements DataInput/DataOutput, it is format-compatible with both DataInputStream and DataOuputStream. So, I would say that yes, you fulfill the requirement by using it. Notice that your instructions just state that "(...) use the formats of the DataInputStream and DataOutputStream classes", not something like "you must use the DataInputStream and DataOutputStream classes to access the file header".
Best,
Phil.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Agreed - there is no requirement to use DataInputStream or DataOutputStream. There is merely the information that the format used is the one used by DataInputStream and DataOutputStream. What you do with that information is up to you.


"I'm not back." - Bill Harding, Twister
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
Hi Jim,
As I feel well and as we cannot find - for a few days now - any subject on which to disagree with each other, I wonder if you feel ill ... I worry a bit ! Are you still OK ?
Best,
Phil.
Dave Knipp
Ranch Hand

Joined: Oct 14, 2003
Posts: 146
cool, thanks Philippe and Jim for both of your responses, i started using RandomAccessFile and it looks alot easier to use than DataInputStream/DataOutputStream.
Thanks!
Dave
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I'm fine, thanks, PM.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to read MAGIC for db-2x2.db