problems with record Store Reading

Hi

I have problems reading record store properly with recordstoreenumeration

Here is the code

String outString[] = {"First Record","Second Record","Third Record"};

int outInteger[] = {15,10,5};

boolean outBoolean[] = {true,false,true};

ByteArrayOutputStream outStream = new ByteArrayOutputStream();

DataOutputStream outDataStream = new DataOutputStream(outStream);

for(int x=0;x<3;x++){

outDataStream.writeUTF(outString[x]);

outDataStream.writeBoolean(outBoolean[x]);

outDataStream.writeInt(outInteger[x]);

outDataStream.flush();

outRecord = outStream.toByteArray();

rs.addRecord(outRecord,0,outRecord.length);

}

outStream.reset();

outStream.close();

outDataStream.close();

StringBuffer buffer = new StringBuffer();

byte[] byteInData = new byte[300];

ByteArrayInputStream inStream = new ByteArrayInputStream(byteInData);

DataInputStream inDataStream = new DataInputStream(inStream);

rEnum = rs.enumerateRecords(null,null,false);

System.out.println("Number of records are " + rEnum.numRecords());

while(rEnum.hasNextElement())

{

int id = rEnum.nextRecordId();

System.out.println("The record id is " + id);

rs.getRecord(id,byteInData,0);

System.out.println("Reading in jobsReader " + inDataStream.readUTF() + " " +inDataStream.readBoolean()+ " " +inDataStream.readInt());

}

inStream.close();

inDataStream.close();

I run this code three times so I get 9 records, but for some mystikal reason It reads from to down starting with record ID = 9 and reads only the records that were insterted latest. Here is the output:

Number of records are 9

The record id is 9

Reading in jobsReader First Record true 15

The record id is 8

Reading in jobsReader Second Record false 10

The record id is 7

Reading in jobsReader Third Record true 5

The record id is 6

Reading in jobsReader false 0

The record id is 5

Reading in jobsReader false 0

The record id is 4

Reading in jobsReader false 0

The record id is 3

Reading in jobsReader false 0

The record id is 2

Reading in jobsReader false 0

The record id is 1

Reading in jobsReader false 0

I have been struggling with this for about two days, still no success. Please help me to sort this out.

[2462 byte] By [gerekbolsaa] at [2007-11-27 4:31:39]
# 1
Have you closeRecordStore after reading or writing?
PeppeMEa at 2007-7-12 9:41:07 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2
Yes I do close the recordStore after using it. Still dont know where the problem is. Can anyone help me?
gerekbolsaa at 2007-7-12 9:41:07 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3

hm, ok, two things that may help you out.

first - the order of the records being enumerated at the end... no guarantee, its annoying, but its how it is, kinda like a hashtable if you enumerate it, no order guarantee.

second - you're not creating a new dataoutputstream for every addRecord() you're just adding more and more 'stuff' into it, and then calling addRecord() so, who know's why you're getting the results you are, but try creating a new dataoutputstream... just in case. you shouldn't HAVE to, but you never know, it's worth a shot to try.

pandora_fooa at 2007-7-12 9:41:07 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 4
oops, sorry, i meant the ByteArrayOutputStream. you need to reset/renew that each loop, you're flushing the dataoutputstream, but not the bytearrayoutputstream.
pandora_fooa at 2007-7-12 9:41:07 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 5

Hey, pandora_foo

I had noticed that too, the need to reset the bytestream but yesterday, when I was fiddling around with this code ( it's from the J2ME Complete Reference, btw! ), reseting caused and EOF Exception while reading.

I'm really stymied with this stuff. Been wondering how it works.

Will work on it over the weekend and let you guys know if i get anywhere!

nogoodatcodinga at 2007-7-12 9:41:07 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 6
Also, calling flush() on the datastream is supposed to call flush on the underlying bytestream! The documentation I read said so!
nogoodatcodinga at 2007-7-12 9:41:07 > top of Java-index,Java Mobility Forums,Java ME Technologies...