Hello Folks,
I am working on URLyBird Version 1.2.2.
--------------------
The format of data in the database file is as follows:
Start of file
4 byte numeric, magic cookie value identifies this as a data file
4 byte numeric, offset to start of record zero
2 byte numeric, number of fields in each record
Schema description section.
Repeated for each field in a record:
2 byte numeric, length in bytes of field name
n bytes (defined by previous entry), field name
2 byte numeric, field length in bytes
end of repeating block
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.
--------------------
I am trying to read the data file the following way.
package scjdtest.io;
import java.io.*;
public class ReadTest {
private static RandomAccessFile database = null;
static final int NAME_FIELD_LENGTH = 64;
static final int LOCATION_FIELD_LENGTH = 64;
static final int SIZE_FIELD_LENGTH = 4;
static final int SMOKING_FIELD_LENGTH = 1;
static final int RATE_FIELD_LENGTH = 8;
static final int DATE_FIELD_LENGTH = 10;
static final int OWNER_FIELD_LENGTH = 8;
static final int RECORD_LENGTH = NAME_FIELD_LENGTH
+ LOCATION_FIELD_LENGTH
+ SIZE_FIELD_LENGTH
+ SMOKING_FIELD_LENGTH
+ RATE_FIELD_LENGTH
+ DATE_FIELD_LENGTH
+ OWNER_FIELD_LENGTH;
public static void main (
String [] args) throws Exception {
String [] strRecord = null;
database = new RandomAccessFile("db-1x2.db", "r");
System.out.println("Length of the database is " + database.length());
for (int i = 0; i < database.length(); i++) {
strRecord = readRecord(i);
for (int j = 0; j < strRecord.length; j++) {
System.out.println("[" + i + "," + j + "] = " + strRecord[j]);
}
}
}
private static String [] readRecord(long recNo) throws Exception {
final byte[] input = new byte[RECORD_LENGTH];
synchronized (database) {
database.seek(recNo);
database.readFully(input);
}
// now convert those bytes into a String[]. The
thread that is doing
// this conversion can be running while other threads are doing
// other work - they are no longer being blocked.
/**
* class to assist in converting from the one big byte[] into
* multiple String[] - one String per field.
*/
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, "ISO-8859-1");
offset += length;
return str.trim();
}
}
RecordFieldReader readRecord = new RecordFieldReader();
String returnValue[] = {
readRecord.read(NAME_FIELD_LENGTH),
readRecord.read(LOCATION_FIELD_LENGTH),
readRecord.read(SIZE_FIELD_LENGTH),
readRecord.read(SMOKING_FIELD_LENGTH),
readRecord.read(RATE_FIELD_LENGTH),
readRecord.read(DATE_FIELD_LENGTH),
readRecord.read(OWNER_FIELD_LENGTH)
};
return returnValue;
}
}
--------------------
Output:
Length of the database is 4743
[0,0] = J
[6,4] = Smallvi
[6,5] = lle
[6,6] =
[7,0] = J[8,2] =
[8,3] = S
[8,4] = mallvill
[8,5] = e
[8,6] =
[9,0] = name allv
[14,3] = i
[14,4] = lle
[14,5] =
[14,6] =
...
...
...
--------------------
As seen above, printing the output does not fetch the desired results. Even the order of printing is not coming correctly. After [0,0], [6,4] is printed. After [9,0], [14,3] is printed and so on....
Could anyone help me out in this topic?
Thanks & regards,
Rajesh Moorthy.