aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Magic cookie value 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 "Magic cookie value" Watch "Magic cookie value" New topic
Author

Magic cookie value

Clivant Yeo
Ranch Hand

Joined: May 22, 2004
Posts: 124
Hi ranchers,

What is the purpose of the 4 bytes magic cookie value? It seems pretty useless in the project. It is not even mentioned in any other places of the instructions.html file other than the data section. If there is no purpose for it, should I store the value? Cause if I were to use RandomAccessFile, I can choose to leave the value alone.

Thanks for anyone who will answer my questions.


Clivant Yeo
My Personal Website
Frank Bush
Greenhorn

Joined: Apr 27, 2004
Posts: 8
My understanding is that the magic cookie identifies the database file format.

When reading what is supposed to be a database file, you should verify that the first 4 bytes match the magic cookie value. If they do not, you can throw an exception. Chances are that you are reading Resume.doc or MyLastVacationInTheCaribbean.mpeg

Back to your question, I do not see any reason why you would want to store this value.
Clivant Yeo
Ranch Hand

Joined: May 22, 2004
Posts: 124
Is there a fixed value for this magic cookie value? I mean everyone have the same value is it? If not where do I get to find this value other than by reading the flat file in the first place? Cause I need to read the first four bytes than do a comparison test using == when the program starts right?
Frank Bush
Greenhorn

Joined: Apr 27, 2004
Posts: 8
As an integer, mine is 514 (decimal), but yours might be different. You can find out by printing the return value of where dbFile is a freshly opened RandomAccessFile .
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11526
    
100

Hi Clivant,

Is there a fixed value for this magic cookie value? I mean everyone have the same value is it?


No - there are lots of different values out there.

Anyone who has a Fly By Night Services assignment is almost guaranteed to have a different magic number than someone who has a Bodgitt & Scarper assignment and they are almost guaranteed to have a different magic number than someone who has an URLyBird assignment.

Furthermore someone with version 1.1.1 of URLyBird may have a different magic number than someone with version 1.1.2 which may be different from version 1.2.1 and so on.

The reason for this is that the file formats are sometimes different. So if the magic numbers don't match, chances are that the program you wrote won't be able to meaningfully read the file.

If not where do I get to find this value other than by reading the flat file in the first place? Cause I need to read the first four bytes than do a comparison test using == when the program starts right?


You do have to read the file at least once to get the magic number. Thereafter you can store the magic number and validate that it matches in future readings.

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Clivant Yeo
Ranch Hand

Joined: May 22, 2004
Posts: 124
Thanks Andrew and Frank

I got it now. Enlightened by both your guidance.
Andy Zhu
Ranch Hand

Joined: May 26, 2004
Posts: 145
So the magic cookie value is assignment specific, so specific that even the grader's test db file will be checked against with this value for further evaluation. Am I right?


--------<br />Andy Zhu<br />scjp 1.4<br />scjd 1.4<br />SAS Certified Programmer 9.0
Clivant Yeo
Ranch Hand

Joined: May 22, 2004
Posts: 124
Hi Andy,

I think so also.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Magic cookie value