This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

URLBirdy Collections Advice

 
vijay selvaganapathy
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to extract the data from the file and having small problems.If i could overcome this i can start implementing the interface methods. I am using randomaccessfile class to read/write the file. I am storing the header information(fieldname and fieldlength) in linkedhashmap (as it stores in insertion order) with (name,size) as key-value pair.My code is :


where "it" is the iterator (Iterator it=map.keySet().iterator()


My problem(s) are:
1) How can we check whether a fieldname is less than the maxlength specified
in the header info? (e.g location field is less than 64 bytes ??)

2) How do we check whether the 2byte field(before the start of the record) is 0x8000 as it is 32768 in dec.The instructions are given below:

Data section. (offset into file equal to "offset to start of record zero" value)
Repeat to end of file:
2 byte flag. 00 implies valid record, 0x8000 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

End of file

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.

Your advice would be greatly appreciated

VJ
 
Alex Matute
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vijay,

I think you're missing something in the instructions:
1) How can we check whether a fieldname is less than the maxlength specified in the header info? (e.g location field is less than 64 bytes ??)


My assignment also includes: "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." This implies that all the fields which size is less than the specified, will be filled up with 0 until they reach their specified field size. I've already implemented that and it works fine. I do something like this:
<CODE>recordData = new byte[(int)sizeOfField[i]];
dbFile.read(recordData);
field = new String(recordData).trim(); </CODE>
Best Regards,

Alex
[ July 10, 2005: Message edited by: Alex Matute ]
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vijay
1) How can we check whether a fieldname is less than the maxlength specified in the header info? (e.g location field is less than 64 bytes ??)


It wont be. From the instructions you quoted "...each field fixed length at maximum specified..." It may have trailing spaces or nulls (either of which can be handled via a call to the String trim() method).

Knowing this, you could (you don't have to) reconsider the whole method of reading in records. At the moment you have access to the entire data file blocked while you read in each individual byte and do whatever work you want to with it. If you read in the entire record as one long byte array, you could release the data file to other clients much sooner (improving concurrency), and then deal with converting sections of the byte array within non synchronized code.

2) How do we check whether the 2byte field(before the start of the record) is 0x8000 as it is 32768 in dec.
Have you considered what 0x8000 is when converted to a short? Then you would only need to compare two short values?

Regarding the bit of code:This bit of code is likely to be run many times. Whereas the code reading in the schema is only likely to be run once. So wouldn't it make more sense to convert to an Integer when you read the schema, which would then allow for a simpler / more efficient bit of code:

Regards, Andrew
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alex,

You might want to take a look at the difference between the read() and readFully() methods .

Regards, Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic