class OrderedIntegerSerialiser implements ToBytesSerialiser<Integer> {

    @Override
    public byte[] serialise(final Integer object) {
        final Integer signedI = object ^ 0x80000000;
        int shift = 56;
        int prefix = (signedI.intValue()) < 0 ? 255 : 0;

        int index;
        for (index = 0; index < 4 && (signedI.intValue() >> shift & 255) == prefix; ++index) {
            shift -= 8;
        }

        byte[] ret = new byte[5 - index];
        ret[0] = (byte) (4 - index);

        for (index = 1; index < ret.length; ++index) {
            ret[index] = (byte) (signedI.intValue() >> shift);
            shift -= 8;
        }

        if (signedI.intValue() < 0) {
            ret[0] = (byte) (8 - ret[0]);
        }
        return ret;
    }

}