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;
...
Preon uses the language "Limbo" to implement the expression evaluation. In Limbo, an expression evaluated to true, would take the value of "1" (and false "0").
As consequence the expression:
would take probably the form of:
I tested it, and unfortunately it seams that the method size does not accept it. I get the exception:
It seams that the logical tokens are understood only by the "@If" annotation.
As a workaround I suggest you to define two fields prefixed by a "@If" annotation, and then implement a get method witch test the two fields for "null" and return the "non-null" one.