Good day,
Can someone please comment whats wrong with following code? I had a requirement to save byte[] arrays and read them back.
While Writing, I saved the incoming messages as byte arrays as follows with each msg saved with its length explicitly so that I know exactly how much to read back from an excerpt.
//
//Writer code
//
//.. 'msg' object coming from somewhere. Msg is actually a Google protobuf object
byte[] byteArr = msg.toBytes();
excerpt.startExcerpt(byteArr.length + (Integer.SIZE/8));
excerpt.writeInt(byteArr.length);
excerpt.write(byteArr);
excerpt.finish();
//
//Reader code
//
excerpt = chronicle.createExcerpt();
int i = 0;
while(excerpt.hasNextIndex())
{
excerpt.index(i++);
int length = excerpt.readInt(); <-- Issue: when i = 1, this returns a -ve integer.
byte[] read = new byte[length];
excerpt.readFully(read);
}
During read, the first excerpt is read successfully. Its length is returned correctly and I'm able to read the original byte array. But on next iteration when index i = 1, the length is returned as a negative number, so effectively I can't read the next message.