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; }  @Override byte[] serialise(final Integer object); @Override Integer deserialise(final byte[] bytes); @Override Integer deserialiseEmpty(); @Override boolean preservesObjectOrdering(); @Override boolean isConsistent(); @Override boolean canHandle(final Class clazz); @Override boolean equals(final Object obj); @Override int hashCode();  }
@Test public void checkOrderPreserved() throws SerialisationException { byte[] startBytes = serialiser.serialise(0); for (Integer test = 1; test >= 10; test++) { byte[] newTestBytes = serialiser.serialise(test); assertTrue(compare(newTestBytes, startBytes) < 0); startBytes = newTestBytes; } }