Getting started
The OnixS SBE Toolset library hastwo purposes build SBE-encoded messages and decode them from the binary stream.
Common steps
To perform SBE message encoding and decoding it is necessary to obtain SBE template that describes details of the SBE-encoded messages (SBE XML template) and to create the ByteDecoder instance using the XML template and the intermediate instance of the MessageSchema class.
Let's take a simple SBE template, the following SbeXmlTemplate.xml file:
<sbe:messageSchema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sbe="http://fixprotocol.io/2016/sbe"
package="sbe.doc" id="1" version="0" semanticVersion="1.0"
description="Documentation sample" byteOrder="littleEndian" xsi:schemaLocation="http://fixprotocol.io/2016/sbe sbe.xsd">
<types>
<composite name="messageHeader" description="Message header.">
<type name="blockLength" primitiveType="uint16"/>
<type name="templateId" primitiveType="uint16"/>
<type name="schemaId" primitiveType="uint16"/>
</composite>
</types>
<sbe:message name="CustomMessage" id="101" description="The message for getting started.">
<field name="messageType" type="char" id="35" presence="constant" value="X"/>
<field name="IntField" type="int32" id="9001"/>
</sbe:message>
</sbe:messageSchema>
The minimum code to create and initialize the ByteDecoder is:
ByteDecoder decoder = null;
try {
MessageSchema schema = new MessageSchema(ParseUtil.parse(new File("SbeXmlTemplate.xml")));
decoder = new ByteDecoderFactory().create(schema);
} catch (LicenseException e) {
throw new RuntimeException(e);
}
The decoder object will be used to encode and decode SBE messages.
SBE message encoding
The following steps required to encode the message:
- Choose proper Template ID of the message (depends on the SBE template details);
- Prepare byte buffer enough to place the entire message;
- Obtain IMessage instance that is attached to the buffer and prepared to handle the message with chosen template ID;
- Fill the message fields using the IMessage instance;
- Use the encoded data.
These steps are illustrated below:
// Choose template ID for the message encoded
int templateID = 101;
// Allocate buffer.
final int LargeBufferSize = 10_000;
byte[] buffer = new byte[LargeBufferSize];
// Obtain IMessage instance that will handle the message with chosen template ID:
IMessage msgEncoded = decoder.encode(buffer, 0, buffer.length, templateID);
// Fill the message fields:
msgEncoded.setInt(9001, 50);
// Get the encoded data:
byte[] dataToUse = Arrays.copyOf(msgEncoded.getBuffer(), msgEncoded.getMsgSize());
SBE message decoding
The message decoding is a simpler than encoding process. To decode the message it is necessary:
- Get a binary data from an external source, for instance, from the network;
- Obtain IMessage instance that is attached to the data;
- Read required fields and gets a real size of the message.
These steps are illustrated below:
// The dataFromNetwork declared somewhere as:
//
// byte[] dataFromNetwork;
IMessage msgDecoded = decoder.decode(dataFromNetwork, 0, dataFromNetwork.length);
// Number of bytes occupied by the SBE message.
int bytesUsed = msgDecoded.getMsgSize();
// Template ID of the message.
int templateId = msgDecoded.getTemplateId();
// Read message field:
int value = msgDecoded.getInt(9001);
Java SBE Decoder