public final int serialize(int offset, byte[] data) {
int dataSize = getDataSize();
int recSize = 4 + dataSize;
LittleEndianByteArrayOutputStream out = new LittleEndianByteArrayOutputStream(data, offset, recSize);
out.writeShort(getSid());
out.writeShort(dataSize);
serialize(out);
if (out.getWriteIndex() - offset != recSize) {
throw new IllegalStateException("Incorrect number of bytes written - expected "
+ recSize + " but got " + (out.getWriteIndex() - offset));
}