I need to read a binary file where the size of a byte array depends on an optional value as well as on a constant number. How can I achieve this using Preon?
It seems that the byte array size calculation cannot be conditional ie. size="adaptationFieldControl==0b10 || adaptationFieldControl==0b11 ? 184-adaptationFieldLength : 184"
Using a method (see sample below) to calculate the dynamic size makes Preon fail with Caused by: org.codehaus.preon.el.BindingException: Failed to create binding for bound data called getPayloadLength.
public class packet {
@BoundNumber(size = "2")
byte adaptationFieldControl;
/**
* Returns the size of the payload if present in the packet
* @return size corrected for adaptation fields
*/
public int getPayloadLength() {
if(isAdaptationFieldsPresent()) {
return 188 - (4+adaptationFieldLength);
}
return 188-4;
}
@If("adaptationFieldControl==0b10 || adaptationFieldControl==0b11")
@BoundNumber(size="8")
short adaptationFieldLength;
@If("adaptationFieldControl==0b01 || adaptationFieldControl==0b11")
@BoundList(size="payloadLength")
byte[] payload;
...