public static void putCompressedNumber(int n, ByteBuffer buf) {
        int negative = n < 0 ? 0x80 : 0x0;
        if (negative != 0) {
            n = -n;
            if (n == -n) --n; // underflow, caught as "too big" later.
        }
        if (n < (0x1 << 5)) {
            byte b = (byte)(n | negative);
            buf.put(b);
        } else if (n < (0x1 << 13)) {
            n = n | 0x4000 | (negative << 8);
            buf.putShort((short)n);
        } else if ( n < (0x1 << 29)) {
            n = n | 0x60000000 | (negative << 24);
            buf.putInt(n);
        } else {
            throw new IllegalArgumentException("Number '" + ((negative != 0) ? -n : n) + "' too big, must extend encoding");
        }
    }
Utility for bytewise compressing of integers into a ByteBuffer.

@author baldersheim
vespajlib/src/main/java/com/yahoo/compress/IntegerCompressor.java