// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import dk.vajhoej.record.*

@Struct(maxlength=2+32767+1)
class RfmVar {
    @StructField(n=0, type=FieldType.INT2)
    int length
    @StructField(n=1, type=FieldType.FIXSTR, lengthExpr="groovy:rec.length")
    String line
    @StructField(n=2, type=FieldType.FIXSTR, lengthExpr="groovy:rec.length % 2")
    String pad
}

buf = new byte[512]
try(fis = new FileInputStream("varnospn.txt")) {
    while((n = fis.read(buf)) > 0) {
        try(bais = new ByteArrayInputStream(buf, 0, n)) {
            try {
                Util.readAll(RfmVar.class, bais, { o -> println("|${o.line}|") })
            } catch(NegativeArraySizeException ex) {
                // length was end-of-block marker
            }
        }
    }
}
