This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

magic cookie ?

 
steve mcdonald
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the magic cookie purpose is to validate any file that a user supplied ? Am i getting right ? or is there a different purpose ?

Well, i am going ahead with validation of the datafile and inform the user with appropriate message

I read the cookie value from the supplied database file and am defining a static int constant with that hardcoded value.

say
public static int MAGIC_COOKIE_VALUE = 100;

When the user specifies the path of another database file (non sun supplied), i always open the file and then investigate the Datafile magic cookie and check if it is equal to constant MAGIC_COOKIE_VALUE and then validate, accept or reject the file with Error to the user.

please comment.
Thanks and regards for all the help
 
santosh kulkarni
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve, you are correct. The purpose of the magic code/cookie/number is to validate the file. Perhaps it is meant to discourage 'sharing' of code amongst examinees :-).
It also helps to fail early in your program rather than some weird exception while parsing.
HTH
Santosh
 
Lara McCarver
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am doing the same thing as you, Steve. When the user selects a database file, I check if it's valid, and if not, I make the user pick a different file instead.
 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me see if I understand this correctly. You read the database file and print the magic cookie value to the console. Now that you know the valid magic cookie, you hardcode that value as a constant to check for (in)valid database files?

I didn't realize that this has to be done. I thought the magic cookie is there for the sake of being there. LOL!
 
steve mcdonald
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well Liang Anmian,

you may seem to have a magical way to obtain the magic cookie from the magical datafile that sun supplied and magically pass :-) LOL

Little wired chicken and egg issue :-) how do you validate a magic cookie hidden in a datafile, which is hidden unless you read it

x == y where x and y are not known :-)
 
Alan Mc Kernan
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why are you guys hardcoding this magic cookie value?

i just read it in.. and forget about it..

why would we need to use this later on?

if the user later opens a different database file, surely its allowed to have a different magic cookie value?

what dont i get?
 
Alan Mc Kernan
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you're readin the value in once.. then hard coding it and then assuming that all databse files will have this same value?

hrmm.. i dunno
 
steve mcdonald
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well Alan, there is a magic cookie for a reason.

the reason that i atleast think is conveyed from the DBFile that sun gave is :
the DB File metadata is tightly associated with the MagicCookie.

Each Magic cookie value dictates a different file header length of the metadata.

Ex: DB file with magic cookie 100 has Metadata definitions size 100 bytes and Data records start from 100 byte onwards
and
DB file with magic cookie 200 has Metadata definitions size 200 bytes and Data records start from 200 byte onwards.

so with out knowing what schema structure you are dealing with, how would you know to perform CRUD operations ?

MC is there for a reason and your program cannot accept any arbitary file and magically function.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alan,

As Steve says, different assignments have different schemas (and meta-data), and thus they have different magic cookies. Take a look at the following two examples of meta-data and schemas (taken from real assignments):

That has a different magic cookie than the one used for the following meta-data and schema:If I write my Data class such that it checks the magic cookie for the first example, and expects the meta-data and schema for that first example, then it will be able to read any file that has the same magic cookie, meta-data and schema. But as you can easily see, without the check on the magic cookie, the Data class would quickly fail when trying to read the second file format - the field sizes are different, and some fields are different.

Regards, Andrew
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update: This has now been added to the JavaRanch SCJD FAQ.

Regards, Andrew
 
Lara McCarver
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The magic cookie can be used to validate whether a file is a database file. Like so many other things that people do, it is not clear to me that this is a *requirement*. Is sorting the records in the client a requirement? Is an Unbook button a requirement (for B&S)? It just seemed to me like a good idea to do and it wasn't really hard.

Lara
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic