wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Read and Write doubts 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 "Read and Write doubts" Watch "Read and Write doubts" New topic
Author

Read and Write doubts

Michel Bertrand
Greenhorn

Joined: Jan 31, 2003
Posts: 26
Hello !
I'm in doubt about my database reading and writing methods. When I
write any record in the database, if the field length is lower
then its value some "00" fills its size. I could see it checking
the database file using a binary editor. So, at screen, I have
some little squares when showing this record. See the part of the
writing method below:

Where: buffer has the content.
fields is an array of objects encapsulating the database fields
offset control the data structure
CHARSET is US-ASCII
Checking the default database that was send me there's "20" instead of
"00" in the records probably representing the empty part.
Reading my assignment, I found an instruction to write only the content
length for any field. So, probably I cannot complete the field size
using blank spaces "20".
In this context, how could I avoid the little squares ? Did I make a
mistake ?
Regards,
Michel.
[Andrew: put the source code between [code] and [/code] UBB tags]
[ March 16, 2004: Message edited by: Andrew Monkhouse ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11465
    
  94

Hi Michel,
I have edited your post to put the code between [code] and [/code] UBB tags. Doing this ensures that indenting is preserved, which makes the code easier to read.
When you are writing your post, there are a number of buttons just below the edit pane, which will insert the tags for you, so you don't have to remember what they are.
If you would like to edit your original post so that you can see what I have done, you can click on the button that is just above your post.
As for your question ....
If you want to get rid of those squares, you could try doing a trim() on the String - this was a common solution to this problem in the Fly By Night Services assignment where strings were null terminated.
In the newer assignments, the instructions state that the fields are null terminated, but in reality they seem to be padded with spaces. This is apparently a deliberate decision by Sun.
I think most people here are padding the data with spaces when they write it back to disk.

Reading my assignment, I found an instruction to write only the content
length for any field.

Can you please tell us what that instruction is?
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Michel Bertrand
Greenhorn

Joined: Jan 31, 2003
Posts: 26
Hi! Thank you for your reply and your code hints !
I found in my assignment:

But it was not what I saw in the database file. I encontered those padded "20" (blank space characters) instead of null terminated. That's it.
What do you think ? I padded the blanks too, but I'm a little afraid of it. I tried the "trim()", but it did not work for me and I did not find the reason yet.
Thanks again !
Michel.
Satish Avadhanam
Ranch Hand

Joined: Aug 12, 2003
Posts: 697
Hi Michel
Originally posted by Michel Bertrand:


But it was not what I saw in the database file. I encontered those padded "20" (blank space characters) instead of null terminated. That's it.

Yes, that's right. For some they are padded with blank spaces only. But we have to take into consideration that there might be a chance for terminating with null.

What do you think ? I padded the blanks too, but I'm a little afraid of it. I tried the "trim()", but it did not work for me and I did not find the reason yet.

"trim()" should work Michel. I think am using it and worked for me.

Thanks again !
Michel.

Good Luck.
George Marinkovich
Ranch Hand

Joined: Apr 15, 2003
Posts: 619
Hi Michel,
The assignment instructions state that fields may be null-terminated (if they are less than the maximum length), so you should be able to read fields that are null-terminated (even though there may not be any example of that in the Sun supplied database file). You also need to be able to handle fields that are not null-terminated, that is, ones that use the maximum length of the field. One way for this to happen is for the field to be right padded with blank spaces. In this case you probably want to do a trim() to get rid of the useless blank space.
When writing the field it's probably easier to just null-terminate the fields if they are less than the maximum length. But the choice is yours, you could also right pad the fields with blank spaces. Either way is consistent with the assignment instructions requirement. Null-terminating is probably more efficient.


Regards, George
SCJP, SCJD, SCWCD, SCBCD
Michel Bertrand
Greenhorn

Joined: Jan 31, 2003
Posts: 26
Hi guys !
Thanks for all !
Regards,
Michel.
 
GeeCON Prague 2014
 
subject: Read and Write doubts