Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Magic cookie in database file - what for?

 
Jimmy Ho
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the database file provided in my SCJD assignment has its first two bytes reserved as a magic cookie at the very beginning of the file. What is this magic cookie for? I currently don't see a use for it. Am I missing something?
 
Alexander Duenisch
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The magic cookie identifies your file as a database file
in the reqired format for your application.
Your app should check this cookie in the startup process
so it can throw around exceptions if it got fed an unknown
file format.
If the cookie is correct, you can still encounter a corrupted
file, but if it is incorrect, you know for sure the file is bad.

Hope this brought some enlightenment.

Greetings,
alex
 
mohamed sulibi
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi;

i read the Magic cookie value; but because the requirement is not clear for
it, i through this value away. and i will discuss it in the choices document.

what you think ?

Regards.
Mohamed Darim.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, since the Magic Number that the database already has is, in my case, 257, I assumed that this is the right Magic Cookie (Magic Number in my case). When I first open the database file I check if this is the value found; if it is, than it's OK, otherwise, I don't proceed.
 
Jack Yang
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roberto,

I didn't see any Magic cookie number for the assignment. May I ask you how you knew your Magic cookie is 257?

Thanks
[ April 30, 2008: Message edited by: Jack Yang ]
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey my buddy.

So, this number is the first thing that appears in the .db file. It indeed does not appear in the assignment, and all we know is that it identifies whether a particular file is valid or not. Since the valid value is not mentioned, and my .db file has this value (257), I assumed that this value is the correct one. By the way, I hardcoded this value.
This is the first thing of the .db file. As soon as you open it, call the readInt() method of your RAF object, and you'll find out the magic number of your database!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic