This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes URLy 1.1.2 - Reading the DB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "URLy 1.1.2 - Reading the DB" Watch "URLy 1.1.2 - Reading the DB" New topic
Author

URLy 1.1.2 - Reading the DB

Shan Jun Hao
Ranch Hand

Joined: May 23, 2006
Posts: 39
I tried to read the db file... yes I am very happy that I managed to did it! I can read the magic cookie, the offset and number of fields.

While reading the schema, everything is fine until I reach the maxOccupancy. I believe somehow I miss out something or misread something. The rest of the data like smokingRestriction, price and etc have wrong/incorrect value.

Data File Format
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

Schema description section.
Repeated for each field in a record:
2 byte numeric, length in bytes of field name
n bytes (defined by previous entry), field name
2 byte numeric, field length in bytes
end of repeating block

Data section. (offset into file equal to "offset to start of record zero" value)
Repeat to end of file:
2 byte flag. 00 implies valid record, 0x8000 implies deleted record
Record containing fields in order specified in schema section, no separators between fields, each field fixed length at maximum specified in schema information

End of 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.




Can someone help me on this? Did I set the offset incorrectly?


SCJP, SCWCD, SCBCD, SCJD (In progress)
Daniel Bryant
Ranch Hand

Joined: Jul 15, 2006
Posts: 54
Hi,

I think the problem is connected with the values of the field name length (i.e. the actual length of the String that represents the name of the field) you are using - I can't tell how you are calculating these values with the code provided, but I would recommend something like the following pseudocode to calculate the length of the field name correctly before attempting to read:



You could adapt this code to only synchronized on the RAF for a short period of time (which is a very good idea), but as I only build the schema once at the start of the application (when no-one should be reading a record), I don't worry about this. I hope this helps.

Best wishes,

Daniel


SCJP 1.4,<br />SCJD (URLyBird 1.2.1, JDK 5.0)
Shan Jun Hao
Ranch Hand

Joined: May 23, 2006
Posts: 39
Alright... something weird here. I managed to solve the question I raised earlier on. However, I don't think that's the right solution.

The problem lies with the length of my smoking restriction. In my instruction, it mentioned the field length is 1. However, I realise that I need to set it to 3, then the fields that come after smoking restriction will not display wrong output(bcos of some positioning).

I feel that this is kind of weird. Either SUN has an error in their instruction (which I don't think so) or something wrong with my logic. Can anyone advise?

I am also wondering if it got to do with:

Data section. (offset into file equal to "offset to start of record zero" value)
Repeat to end of file:
2 byte flag. 00 implies valid record, 0x8000 implies deleted record
Record containing fields in order specified in schema section, no separators between fields, each field fixed length at maximum specified in schema information


How do I read this section?
[ August 09, 2006: Message edited by: Shan Jun Hao ]
Daniel Bryant
Ranch Hand

Joined: Jul 15, 2006
Posts: 54
Hi Shan,

Don't forget the two bytes indicating a deleted record will appear at the front of every record in the DB and so you will have to read the record from the file accordingly. This may help...

0. Position file pointer to location directly after the header information
1. Read record completely: length of record in bytes + 2 bytes (for deleted flag)
2. Read the first two bytes - determine if deleted
3a. If not continue to extract data from the record (do not reset the file pointer or you will read the deleted bytes again!)
3b. If deleted move file pointer to next record
4. Repeat from 1

Best wishes,

Daniel
Shan Jun Hao
Ranch Hand

Joined: May 23, 2006
Posts: 39
Daniel thanks for the information! Yes I am able to do it correctly now!! Cheers!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: URLy 1.1.2 - Reading the DB