Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a mobileDB database file 1

Status
Not open for further replies.

tom62

Programmer
Nov 12, 2002
152
DE
Hi,

I'm currently writing a JAVA program that reads a MobileDB database (Palm / PocketPC). Each record in the datatase has the following structure:

[tt]header: |0xFF|oxFF|oxFF|0x01|0xFF|0x00|
1st Field: |# of 1st field (2 bytes)|field (0-n Bytes)|0x00|
nth Field: |# of nth field |nth field|0x00|
trailer: |0x00|[/tt]

Since I'm using a RandomAccessFile I can read any number of bytes from the database file. Since I know the records size but not the length of each field in the record, I read the database on a record by record basis in a byte[] array (skipping the record headers and trailers!).

Question: What would be the easiest way for me to extract the fieldnumbers (= short) and fields (= ASCII String) from the byte[] array?

Thanks in advance for your help,

Tom
 
I think that I've got it, namely to extract only the fields:

private ArrayList getFieldRecords(byte[] record) {
String s = new String(record);
String field = null;

final int MAX = s.length();
int[] index = new int[2];
index[1] = 2;

while (index[1] < MAX) {
index[0] = index[1];
index[1] = s.indexOf("\0", index[0]);
field = s.substring(index[0], index[1]);
result.add(field);
index[1] += 2; // To skip the field number
}
return result;
}
 
This is another way :

Code:
byte[] buf ... // your record
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(buf));
short s = dis.readShort();
int offset = 2;
String s1 = new String(buf, offset, buf.length-offset);

System.out.println("short(" +s +"), string(" +s1 +")");

--------------------------------------------------
Free Database Connection Pooling Software
 
Thank You Sedj!

This is what I was really looking for!!

Best regards,

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top