class OrderedLongSerialiser implements ToBytesSerialiser<Long> {

    @Override
    public byte[] serialise(final Long object) {
        final Long signedL = object ^ 0x8000000000000000L;
        int shift = 56;
        int index;
        int prefix = signedL < 0 ? 0xff : 0x00;

        for (index = 0; index < 8; index++) {
            if (((signedL >> shift) & 0xff) != prefix) {
                break;
            }

            shift -= 8;
        }

        byte[] ret = new byte[9 - index];
        ret[0] = (byte) (8 - index);
        for (index = 1; index < ret.length; index++) {
            ret[index] = (byte) (signedL >> shift);
            shift -= 8;
        }

        if (signedL < 0) {
            ret[0] = (byte) (16 - ret[0]);
        }

        return ret;
    }

}