File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Monkhouse book and readFully method 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 "Monkhouse book and readFully method" Watch "Monkhouse book and readFully method" New topic

Monkhouse book and readFully method

Margarita Babkova
Ranch Hand

Joined: May 09, 2012
Posts: 32
Hi there,

I am reading a Monkhouse book and it says that "read method does not guarantee that entire record wood be read, but at list one byte would be read. and strongly suggest to use readFully.

I want only read a valid room record with delete flag==0; So before using this readFully, i am reading a flag byte value and do a validation of it.
but statement in the book puts doubts in me. Will it really work - 100% times?. it works now ...
what would happen if it just read my one byte flag and does not want to read my database.readFylly?

[edit] do not post complete code snippets

thank you in advance.
Anayonkar Shivalkar

Joined: Dec 08, 2010
Posts: 1545

Hello Margarita Babkova,

Welcome to CodeRanch!

Frankly speaking, I did not read thorough code from MonkHouse - I used it basically for tackling design level issues.

But yes, looking from code, the text makes pretty much sense.

In the code, they are first using read method to simply check that if record is not marked as deleted. If not, then readFully method is used to actually read complete record.

I hope this helps.

Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Roel De Nijs

Joined: Jul 19, 2004
Posts: 8372

I have 2 concerns with the code you showed:
1/ Room.RECORD_LENGTH seems to me to be an integer, the readFully-method expects a byte array
2/ why throw a database exception if the flag's value is different from 0? It could be an indication for a deleted record and according to the instructions that's completely valid.

About the readFully-method. According to its javadoc Reads b.length bytes from this file into the byte array, starting at the current file pointer. This method reads repeatedly from the file until the requested number of bytes are read. This method blocks until the requested number of bytes are read, the end of the stream is detected, or an exception is thrown. I can't see no reason why your approach wouldn't work.

SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
Margarita Babkova
Ranch Hand

Joined: May 09, 2012
Posts: 32
thank you all,
I am spending 90% time trying to understand the logic behind the code in a book - and call it a "Discovery" phase of my own asignment.
but not all of it makes sence from the first read.

in my real code i used a byte array for the readFully method, and throw RecordNotFoundException

[edit] do not post complete code snippets
I agree. Here's the link:
subject: Monkhouse book and readFully method
jQuery in Action, 3rd edition