Chronicle newbie question - issue when saving/reading byte[] arrays

161 views
Skip to first unread message

Nitin Singh

unread,
Apr 12, 2015, 1:29:10 PM4/12/15
to java-ch...@googlegroups.com
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.


Nitin Singh

unread,
Apr 12, 2015, 2:04:59 PM4/12/15
to java-ch...@googlegroups.com
I'm using following Chronicle version:

<dependency>
    <groupId>com.higherfrequencytrading</groupId>
    <artifactId>chronicle</artifactId>
    <version>1.9.1</version>
</dependency>

Luca Burgazzoli

unread,
Apr 12, 2015, 2:18:09 PM4/12/15
to java-ch...@googlegroups.com
You are using a very old version of chronicle so I'd recommend to use a more recent build which is now on https://github.com/OpenHFT/Chronicle-Queue (on maven http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22net.openhft%22%20AND%20a%3A%22chronicle%22).


--
You received this message because you are subscribed to the Google Groups "Chronicle" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-chronicl...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
lb

Nitin Singh

unread,
Apr 12, 2015, 3:30:11 PM4/12/15
to java-ch...@googlegroups.com
 I'm afraid the latest OpenHFT project works only with Java 7+ . I have to use only Java 6.
To unsubscribe from this group and stop receiving emails from it, send an email to java-chronicle+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
lb

Luca Burgazzoli

unread,
Apr 12, 2015, 4:40:56 PM4/12/15
to java-ch...@googlegroups.com
OpenHFT's Chronicle-Queue 3.2.1 should be Java 1.6 compatible.

The right way to iterate a chronicle in 1.9, if I'm not mistaking, should be:

while (true) {
    if(excerpt.nextIndex()) {
        ...
        excerpt.finish();
    }
}



To unsubscribe from this group and stop receiving emails from it, send an email to java-chronicl...@googlegroups.com.

Nitin Singh

unread,
Apr 15, 2015, 2:56:31 PM4/15/15
to java-ch...@googlegroups.com
Thanks ... I eventually used the latest version with Java 8.


On Sunday, April 12, 2015 at 6:29:10 PM UTC+1, Nitin Singh wrote:
Reply all
Reply to author
Forward
0 new messages