Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Magic Cookie in Max's Book?

 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
I just obtained Max's book and i must say i am totally impressed by the book and the book is a must buy if anyone is thinking of the SCJD exams. However i have a silly question(if you guys think is silly). In Max's book, they didnt mention anything about a Magic Cookie. I know they are not gonna give us a sample assignment similar to that of the SCJD but simply solve concepts required to solve the SCJD assignment, but i would like to know where "The magic cookie" fits in Max's book. Thank u.
[ January 30, 2005: Message edited by: Saheed Adepoju ]
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Saheed,

The Magic Cookie identifies that the file you are reading contains data that is likely to be a database file, and that it should be in the format that you expect. If you read the Magic Cookie and the value read does not match the expected value then you know that you cannot use this file.

Max's book persists entire Java objects to file (take a look at the persistDVD() and retrieveDVD() methods of the DVDDatabase class). By using Java's object serialization & deserialization, the same effect can be realised. If the file being read is a serialized version of a DVD object then it can be retrieved - otherwise an exception will be thrown.

So Max's book achieves the same effect as the Magic Cookie, but in a different way.

Regards, Andrew
 
author
Posts: 580
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Andrew:

How would you know what to expect? I think the value is an arbitrary two/four byte value, no?

Reza
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not that arbitrary. As Dieskun kindly pointed out to me in this thread, it is related to the version number of your assignment.

I have assumed that the value in the supplied database file is the magic value and my implementation will only accept files with this value.

Frans.
 
Andrew Monkhouse
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Reza,

How would you know what to expect? I think the value is an arbitrary two/four byte value, no?



When you downloaded your assignment, you were given:
  • A set of instructions that will tell you whether your cookie is a two or a four byte value.
  • A sample data file.

  • Based on that, you can read your data file to determine your magic cookie value. This is a one time task. From that point onwards, you always know what value to expect, and you can write your code to use this known value.

    Regards, Andrew
     
    Ranch Hand
    Posts: 113
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hello ranchers,

    Do I understand it correct:

    I read this number once. Then I save it as a constant in my Program.

    E.g.:


    and when I try to open my db file, this number should be checked.

    Is it a good idea to save this number as a constant? or Should I save it in the properties file?

    Thanks a lot for your answer!
    Lena.
     
    Frans Janssen
    Ranch Hand
    Posts: 357
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Olena Golub:
    Is it a good idea to save this number as a constant? or Should I save it in the properties file?



    Hi Lena,

    I stored it as a constant in my code. Since the code was made to read that one specific format of files and the magic cookie is part of that format, I think it is the most appropriate way.

    For that same reason storing it in a properties file does not seem a good idea to me. This would allow other users to control what kind of files your application can read.

    Frans.
     
    pie. tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic