public static void writeUnsignedVarint(int value, ByteBuffer buffer) {
        if ((value & (0xFFFFFFFF << 7)) == 0) {
            buffer.put((byte) value);
        } else {
            buffer.put((byte) (value & 0x7F | 0x80));
            if ((value & (0xFFFFFFFF << 14)) == 0) {
                buffer.put((byte) ((value >>> 7) & 0xFF));
            } else {
                buffer.put((byte) ((value >>> 7) & 0x7F | 0x80));
                if ((value & (0xFFFFFFFF << 21)) == 0) {
                    buffer.put((byte) ((value >>> 14) & 0xFF));
                } else {
                    buffer.put((byte) ((value >>> 14) & 0x7F | 0x80));
                    if ((value & (0xFFFFFFFF << 28)) == 0) {
                        buffer.put((byte) ((value >>> 21) & 0xFF));
                    } else {
                        buffer.put((byte) ((value >>> 21) & 0x7F | 0x80));
                        buffer.put((byte) ((value >>> 28) & 0xFF));
                    }
                }
            }
        }
    }
@Test
    public void testCorrectnessWriteUnsignedVarint() {
        // The old well-known implementation for writeUnsignedVarint.
        IntFunction<ByteBuffer> simpleImplementation = (int value) -> {
            ByteBuffer buffer = ByteBuffer.allocate(MAX_LENGTH_VARINT);
            while (true) {
                if ((value & ~0x7F) == 0) {
                    buffer.put((byte) value);
                    break;
                } else {
                    buffer.put((byte) ((value & 0x7F) | 0x80));
                    value >>>= 7;
                }
            }

            return buffer;
        };

        // compare the full range of values
        final ByteBuffer actual = ByteBuffer.allocate(MAX_LENGTH_VARINT);
        for (int i = 0; i < Integer.MAX_VALUE && i >= 0; i += 13) {
            ByteUtils.writeUnsignedVarint(i, actual);
            final ByteBuffer expected = simpleImplementation.apply(i);
            assertArrayEquals(expected.array(), actual.array(), "Implementations do not match for integer=" + i);
            actual.clear();
        }
    }