*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes NX:URLyBird Two questions of instructions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "NX:URLyBird Two questions of instructions" Watch "NX:URLyBird Two questions of instructions" New topic
Author

NX:URLyBird Two questions of instructions

Leo Tien
Ranch Hand

Joined: Sep 10, 2002
Posts: 156
The first question:
In my background section of instructions.html file, there is this sentence--"The company's IT
department has a data file ....the new system must reimplement the database code from scratch
without altering the data file format."
What the term "reimplement" is? Whether should I rewrite the db.db file gived by SUN? But at last, the instructions tell me upload the db.db file not to be changed, why there is this contravention?
The second question:
In the data file format section, there is a sentence that "All numeric values are stored in the header information use the formats of the DataInputStream and DataOutputStream classes. All text values, and all fields (which are text only), contain only 8 bit characters, null terminated if less than the maximum length for the field. The character encoding is 8 bit US ASCII."
I don't understand it fully, someone can explain it detaily. Please, thanks.
Andrew, Max, Jim or Vlad or Philippe.
Vlad Rabkin
Ranch Hand

Joined: Jul 07, 2003
Posts: 555
Hi Leo,
What the term "reimplement" is? Whether should I rewrite the db.db file gived by SUN?

It means that you have to implement Data class from scratch (In other assignements, like FBN they have to change the code in data class). By URLyBird there is only DB interface defined, meaning you have to provide your own implementation. That is it. You should not rewrite db.db file.
Remember you have to keep the copy of it and deliver the original version of db.db file!!!

In the data file format section, there is a sentence that "All numeric values are stored in the header information use the formats of the DataInputStream and DataOutputStream classes. All text values, and all fields (which are text only), contain only 8 bit characters, null terminated if less than the maximum length for the field. The character encoding is 8 bit US ASCII.

It means only two things:
1) All numeric values, like length of column name and so on are bit values,l
which you would normally retrieve with DataInputStream like this:
DataInputStream dis = ....
dis.readInt() or dis.readLong() and so on.
I doesn't even mean that you must do it by using DataInputStream: you can use java.nio package.
2) All text values are in "US-ASCII" format. Each letter is 8 bit.
Example:
...


Regards,
Vlad
Davidd Smith
Ranch Hand

Joined: Jul 28, 2003
Posts: 62
Hi, Vlad,
I use the RandomAccessFile's readInt(), readByte(), read() and get the same result as DataInputStream's, could I do that?
Regards
Davidd
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Davidd,
Yes you can. There is no requirement as to what classes / methods you use to read or write to the file.
Just make sure that what you are reading / writing is going to be in "US-ASCII" format.
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Vlad Rabkin
Ranch Hand

Joined: Jul 07, 2003
Posts: 555
Hi Davidd,
I agree with Andrew.
Vlad
Leo Tien
Ranch Hand

Joined: Sep 10, 2002
Posts: 156
Hi Vlad:

Whether I must do this convertion? If I don't do this, what problem will raise?
[ August 17, 2003: Message edited by: Leo Tien ]
Leo Tien
Ranch Hand

Joined: Sep 10, 2002
Posts: 156
Hi Vlad:
All numeric values, like length of column name and so on are bit values

What meaning "bit values" is?
Hi Andrew:
Just make sure that what you are reading / writing is going to be in "US-ASCII" format.

Like the code showed above:

Whether I must do this convertion? If I don't do this, what problem will raise?
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Leo
The instructions tell us that the supplied data file is in US ASCII format, and when we look at the data in the file, it certainly does appear to be in US ASCII format. So I think we should try and ensure that any data we read or write is also in US-ASCII format to keep it consistant.
Regards, Andrew
Bharat Ruparel
Ranch Hand

Joined: Jul 30, 2003
Posts: 493
Hello Andrew/Vlad,
How do you write String data in US-ASCII format? I am also using RandomAccessFile as David is and use the following code to read Strings in US-ASCII format. Is there a matching method to write strings in US-ASCII format?
public class dataHelper {
public static String readFixedString(RandomAccessFile p_in, short p_size) throws IOException {
byte [] bArray;
bArray = new byte[p_size];
p_in.readFully(bArray);
String tempStr = new String(bArray,0,p_size,"US-ASCII");
tempStr = tempStr.trim();
return tempStr;
}
}
Thanks.
Bharat


SCJP,SCJD,SCWCD,SCBCD,SCDJWS,SCEA
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

Hi Bharat
Similar in concept to what you are doing already.
You are currently checking for US-ASCII data on your conversion from bytes to String.
Likewise, when you convert from String back into Bytes, you can specify the conversion.
Regards, Andrew
Bharat Ruparel
Ranch Hand

Joined: Jul 30, 2003
Posts: 493
Hello Andrew,
Thanks for the response. Just to be on the safe side, I am going to post my implementation of writeFixedString method and see what you think.

Regards.
Bharat
Davidd Smith
Ranch Hand

Joined: Jul 28, 2003
Posts: 62
Hi, Bharat,
I do not think you should assign a char '\0' to a byte. If you do not want to change your method too much, you should expand the String to a fixed length first before use getBytes(). Besides, the filling char in my db.db is not '\0' but blank.
Regards
Davidd
Bharat Ruparel
Ranch Hand

Joined: Jul 30, 2003
Posts: 493
Hello Davidd,
I wasn't sure of that since my db file also contains space chars instead of null chars but the instructions say that these are null terminated fixed length strings. That is why I assigned '\0' to it to pad it to the desired length. I wanted to see what others think. It seems like it is better to pad it with spaces (' ') rather than the null character to be consistent and document it in the submission.
I modified my writeFixedString method based on your feedback and it looks as follows:

Let me know what you think.
Regards.
Bharat
Davidd Smith
Ranch Hand

Joined: Jul 28, 2003
Posts: 62
Hi, Bharat,
I also confused about the sun's instructions "null terminated if less than the maximum length for the field" and the blank filler in the db.db, may be there is someone in the forum can explain it to us.
I do not think your method will work. RandomAccessFile's write method do not write String. I think it's better to change the String to char array, pad array with blank to fixed length, new String(char[]), getBytes from String, then write the bytes.
Regards
Davidd
Bharat Ruparel
Ranch Hand

Joined: Jul 30, 2003
Posts: 493
Hello Davidd,
Thanks for the quick response. I got this method from the jdk 141 api for Random Access File from Sun's site:
writeBytes
public final void writeBytes(String s)
throws IOExceptionWrites the string to the file as a sequence of bytes. Each character in the string is written out, in sequence, by discarding its high eight bits. The write starts at the current position of the file pointer.
Specified by:
writeBytes in interface DataOutput
Parameters:
s - a string of bytes to be written.
Throws:
IOException - if an I/O error occurs.
Check it out.
Regards.
Bharat
Davidd Smith
Ranch Hand

Joined: Jul 28, 2003
Posts: 62
Hi,
Bharat,
I am sorry I am out of date and my computer suffered blaster so I could not check the API yestoday. But I still recommend you to use char array first because thus your code need not care about the detail of the encoding and is more adapted.
Regards
Davidd
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NX:URLyBird Two questions of instructions
 
Similar Threads
writeUTF() and writeBytes()
Data File Format and reading header information
NX:[URLyBird]Get the correct field values
NX: db.db file format
Data file format from new assignment