Filtering

Filtering allows to modify data or to control how data packets go through crusher.

All filtering is made in selector's thread so no any long blocking operation is allowed in filter's code.

Note that byte buffer specified in the filter's parameter must have correct position and limit after filter method returned.

Data filter

Data filter can modify ByteBuffer data.


public class InverseFilter implements TransformFilter {

    public static final TransformFilter INSTANCE = new InverseFilter();

    @Override
    public void transform(ByteBuffer bb) {
        if (bb.hasArray()) {
            final byte[] bytes = bb.array();

            final int offset = bb.arrayOffset() + bb.position();
            final int limit = bb.arrayOffset() + bb.limit();

            for (int i = offset; i < limit; i++) {
                bytes[i] = (byte) ~bytes[i];
            }
        } else {
            for (int i = bb.position(); i < bb.limit(); i++) {
                bb.put(i, (byte) ~bb.get(i));
            }
        }
    }
}

Pass filters

Pass filter is used in DatagramCrusher and allows to control which packets are transferred to the other side.


public class LengthPassFilter implements PassFilter {

    @Override
    public boolean check(ByteBuffer bb) {
        // only small packets will pass
        return bb.remaining() < 100;
    }
}