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

Flag Bytes

Kedarnath Bhagwat
Greenhorn

Joined: Oct 27, 2009
Posts: 20
The Data Section for the file URLyBird

Data section.
Repeat to end of file:
1 byte flag. 00 implies valid record, 0xFF 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


When I create a new record do I need to write the value for bytes into File ?

Without writing these Bytes , the program worked fine .

After I wrote the bytes 00 once and then 0xFF I saw the data was misplaced .

PLease guide me on whether I should write it or not .


kmbhagwat , SCJP 6
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Make sure you are using readUnsignedByte() since 0xFF is 255 as an int and the maximum value of a signed byte is 127.

Hope this helps,


Carlos.


SCSA, OCA, SCJP 5.0, SCJD, CCDH, CCAH http://www.linkedin.com/in/carlosamorillo
Kedarnath Bhagwat
Greenhorn

Joined: Oct 27, 2009
Posts: 20
Thanks Carlos .

I am using the RandomAccessFile .
I will have to use DataInputStream .
But then I won't be able to use methods such as seek .

My question is , first of all do I need to write into file a byte indicating whether the record is valid or not ?
Naveen Narayanan
Ranch Hand

Joined: Sep 11, 2007
Posts: 114

Hello Kedarnath,

Probably the question that arises here is
* You are supposed to confirm that newly created record is valid one,
So, How do you make sure that …

SCJP 5.0, SCJD, SCWCD
Matheus Mendes
Ranch Hand

Joined: May 15, 2007
Posts: 66
Yes you must write this byte.

I mean, assume that you created a new record, how your application will know that it is a valid record ? It will check this field, and the field will be blank ( assuming that you didn't put the valid or invalid byte on this field), how does your application will check it ??

Or

You could just write on this field if it was an invalid record, BUT you must justify this decision on your choices.txt and tell why you did things this way.

The Death of one is a tragedy, but the Death of a million is just a statistic. Joseph Stalin

SCJP 6.0, SCJD
Naveen Narayanan
Ranch Hand

Joined: Sep 11, 2007
Posts: 114

Yes Matheus,
Multiple options that may arise here are

  • If you have a null padded (0x00) db file, you might escape, without writing a valid- flag especially for a newly created record.
  • You have a space padded (0x20) db file; writing a valid-flag is mandatory.
  • If creating a new record, reusing a deleted one, here also you will overwrite the deleted-flag with a valid-flag.

  • Matheus Mendes
    Ranch Hand

    Joined: May 15, 2007
    Posts: 66
    Thats a good point

    If creating a new record, reusing a deleted one, here also you will overwrite the deleted-flag with a valid-flag.


    I already saw some friends lost points because they don't reuse the deleted records...
    Roberto Perillo
    Bartender

    Joined: Dec 28, 2007
    Posts: 2267
        
        3

    Howdy, friends!

    Naveen Narayanan wrote:If you have a null padded (0x00) db file, you might escape, without writing a valid- flag especially for a newly created record.


    Hum... I'm not sure if I agree with that. I mean, this value has to be written under any circunstance. Remember that other applications that the guys from URLyBird and B&S use also use the same .db file that we do.

    Matheus Mendes wrote:I already saw some friends lost points because they don't reuse the deleted records...


    Hum... well, the spec doesn't really say that we must reuse deleted entries. The comments of the create() method suggest that we can reuse deleted entries, so it's pretty likely that your friends lost points due to other reasons.


    Cheers, Bob "John Lennon" Perillo
    SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
    Kedarnath Bhagwat
    Greenhorn

    Joined: Oct 27, 2009
    Posts: 20
    Thanks Naveen , Matheuse and Roberto .

    Reading your replies following questions come to my mind

    1:-Is soft Delete allowed ? Am I expected only to set the flag while deleting the record ?But specifications say 'Deleting record makes the record number and associated disk storage available for reuse" So as I understand I need to physically delete it .

    2:-Flag is one byte and when I write 0xFF , placement of data gets disturbed when I read that record.
    Even while declaration
    byte b = 0xFF
    I see error , "Possible Loss of Precision "
    If I print it then it prints as -1 and not 255 .
    I am not sure if one byte space in file can accommodate 0xFF


    Please advice me on how should I proceed on this .









    Naveen Narayanan
    Ranch Hand

    Joined: Sep 11, 2007
    Posts: 114

    Hi Roberto,
    I mean, this value has to be written under any circunstance

    Yes, probably writing that value is secure, otherwise its a bit vulnerable alternative
    the spec doesn't really say that we must reuse deleted entries

    Fair enough, spec says "possibly reusing a deleted entry" ... , choice is on the developer
    Yes Kedarnath,
    So as I understand I need to physically delete it

    If you do that, you might have to go for these things
    * wipe out a record from file.
    * push all the following records, 1 record up.
    Why don't think like just marking a record with a flag, so that you can treat it as a deleted-one and possibly later
    reuse for a new record-creation

    Kedarnath Bhagwat
    Greenhorn

    Joined: Oct 27, 2009
    Posts: 20
    Thanks Naveen for your help .

    I still not have understood how can I write the unsigned byte 0xFF to file If I need to write 0xFF to indicate that record is soft deleted.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: Flag Bytes