@Override
public Object decode(Type type, Short typeLength, Integer typeModifier, ByteBuf buffer, Context context) throws IOException {
CompositeType compType = (CompositeType) type;
Record record = null;
int length = buffer.readInt();
if (length != -1) {
long readStart = buffer.readerIndex();
int itemCount = buffer.readInt();
Object[] attributeVals = new Object[itemCount];
for (int c = 0; c < itemCount; ++c) {
Attribute attribute = compType.getAttribute(c + 1);
Type attributeType = context.getRegistry().loadType(buffer.readInt());
if (attributeType.getId() != attribute.type.getId()) {
context.refreshType(attributeType.getId());
}
Object attributeVal = attributeType.getBinaryCodec().decoder.decode(attributeType, null, null, buffer, context);
attributeVals[c] = attributeVal;
}
if (length != buffer.readerIndex() - readStart) {
throw new IllegalStateException();
}
record = new Record(compType, attributeVals);
}
return record;
}