aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes B & S  read method of interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "B & S  read method of interface " Watch "B & S  read method of interface " New topic
Author

B & S read method of interface

mohini lalwani
Greenhorn

Joined: Nov 24, 2008
Posts: 25
I implemented read method this way ...
is this okay ?

public ContractorFileAccess(String suppliedDbPath)
throws FileNotFoundException, IOException {
log.entering("ContractorFileAccess", "getContractorFileAccess",
suppliedDbPath);
if (database == null) {
suppliedDbPath = "C:\\Sun\\SDK\\jdk\\bin\\db-2x1.db";
database = new RandomAccessFile(suppliedDbPath, "rw");
System.out.println("database length ::::" + database.length());
getContractorList(true);
dbPath = suppliedDbPath;
log.fine("database opened and file location table populated");
} else if (dbPath != suppliedDbPath) {
log.warning("Only one database location can be specified. "
+ "Current location: " + dbPath + " "
+ "Ignoring specified path: " + suppliedDbPath);
}
log.exiting("ContractorFileAccess", "ContractorFileAccess");
}

private List<String[]> getContractorList(boolean buildRecordNumbers) throws IOException {
log.entering("DvdFileAccess", "getDvdList", buildRecordNumbers);
List<String[]> returnValue = new ArrayList<String[]>();

if (buildRecordNumbers) {
recordNumbersLock.writeLock().lock();
}

try {
for (int locationInFile = 0; locationInFile < database.length(); locationInFile += Contractor.RECORD_LENGTH) {
String[] contractorRecord = read(locationInFile);
log.fine("retrieving record at " + locationInFile);
if (contractorRecord == null) {
log.fine("found deleted record ");
} else {
log.fine("found record " + contractorRecord);
if (buildRecordNumbers) {
recordNumbers.put(contractorRecord, locationInFile);
}
returnValue.add(contractorRecord);
}
}
} finally {
if (buildRecordNumbers) {
recordNumbersLock.writeLock().unlock();
}
}

log.exiting("DvdFileAccess", "getDvdList");
return returnValue;
}

/**
*
* @param recNo
* @return
*/
public String[] read(int recNo) throws IOException{
log.entering("ContractorFileAccess", "read", recNo);
final byte[] input = new byte[Contractor.RECORD_LENGTH];

// Block other users for minimum time possible. Get exlusive access to
// the file, read the entire set of bytes, then release exclusive lock.
synchronized (database) {
database.seek(recNo);
database.readFully(input);
}

// now convert those bytes into a Dvd value object. Note that the thread
// that is doing this conversion can be running while other threads are
// doing other work - we are no longer blocking them.

/**
* class to assist in converting from the one big byte[] into multiple
* String[] - one String per field. Refer to the book for more info.
*/
class RecordFieldReader {
/** field to track the position within the byte array */
private int offset = 0;
/**
* converts the required number of bytes into a String.
*
* @param length the length to be converted from current offset.
* @return the converted String
* @throws UnsupportedEncodingException if "UTF-8" not known.
*/
String read(int length) throws UnsupportedEncodingException {
String str = new String(input, offset, length, "UTF-8");
offset += length;
return str.trim();
}
}

RecordFieldReader readRecord = new RecordFieldReader();
String subContractorName = readRecord.read(Contractor.SUBCONTRACTOR_NAME_LENGTH);

String[] returnValue={subContractorName};


log.exiting("ContractorFileAccess", "read", returnValue);
return returnValue;
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: B & S read method of interface