I am using grizzly framework right now and looking for a way to improve latency of our exchange direct market access connection.
Our exchange message format is something like this:
- fixed length header
- type of message and length of payload are stored in header
- fix length String field (e.g. allocating fixed 20 bytes for a string field)
In grizzly I can easily create a message parser like this example: https://javaee.github.io/grizzly/coreframeworksamples.html#/Parsing_an_incoming_message
I am looking for an example or tutorial on how to create same parser in Chronicle-Network and Wire.
Updated:
How to read / write a String from / into Wire with fixed length allocated? say writing '12345678' into a fixed 20 bytes space. The string will packed at first 8 bytes and the remaining 12 bytes will be filled with NULL bytes.
Here is my codes. It works but I believe it is not the proper approach.
private static final byte START_OF_MESSAGE = 0x02;
private int length;
private String compId;
@Override
public void readMarshallable(WireIn in) throws IORuntimeException {
in.bytes().readByte();
// first field
length = in.bytes().readInt();
// second field
byte[] b = new byte[LENGTH_COMP_ID];
in.bytes().read(b);
compId = new String(b).trim();
}
@Override
public void writeMarshallable(WireOut out) {
out.bytes().writeByte(START_OF_MESSAGE);
// first field
out.bytes().writeInt(length);
/** second field, converting the string to byte[] */
out.bytes().write(getBytes(compId, LENGTH_COMP_ID));
}
/**
* retrieve a byte array of str with length = len
*/
private byte[] getBytes(String str, int len) {
if (str == null) {
return null;
}
byte[] b = new byte[len];
if (str.length() > len) {
return str.substring(0, len).getBytes();
} else {
byte[] b2 = str.getBytes();
for (int i = 0 ; i < b2.length ; i++) {
b[i] = b2[i];
}
}
return b;
}