public int writeVarUint32Aligned(int value) {
    // Mask first 6 bits,
    // bit 7 `unset` indicates have next padding bytes,
    // bit 8 `set` indicates have next data bytes.
    if (value >>> 6 == 0) {
      return writeVarUint32Aligned1(value);
    }
    if (value >>> 12 == 0) { // 2 byte data
      return writeVarUint32Aligned2(value);
    }
    if (value >>> 18 == 0) { // 3 byte data
      return writeVarUint32Aligned3(value);
    }
    if (value >>> 24 == 0) { // 4 byte data
      return writeVarUint32Aligned4(value);
    }
    if (value >>> 30 == 0) { // 5 byte data
      return writeVarUint32Aligned5(value);
    }
    // 6 byte data
    return writeVarUint32Aligned6(value);
  }
@Test
  public void testWriteVarUint32Aligned() {
    MemoryBuffer buf = MemoryUtils.buffer(16);
    assertEquals(buf.writeVarUint32Aligned(1), 4);
    assertEquals(buf.readAlignedVarUint(), 1);
    assertEquals(buf.writeVarUint32Aligned(1 << 5), 4);
    assertEquals(buf.readAlignedVarUint(), 1 << 5);
    assertEquals(buf.writeVarUint32Aligned(1 << 10), 4);
    assertEquals(buf.readAlignedVarUint(), 1 << 10);
    assertEquals(buf.writeVarUint32Aligned(1 << 15), 4);
    assertEquals(buf.readAlignedVarUint(), 1 << 15);
    assertEquals(buf.writeVarUint32Aligned(1 << 20), 4);
    assertEquals(buf.readAlignedVarUint(), 1 << 20);
    assertEquals(buf.writeVarUint32Aligned(1 << 25), 8);
    assertEquals(buf.readAlignedVarUint(), 1 << 25);
    assertEquals(buf.writeVarUint32Aligned(1 << 30), 8);
    assertEquals(buf.readAlignedVarUint(), 1 << 30);
    assertEquals(buf.writeVarUint32Aligned(Integer.MAX_VALUE), 8);
    assertEquals(buf.readAlignedVarUint(), Integer.MAX_VALUE);
    buf.writeByte((byte) 1); // make address unaligned.
    buf.writeInt16((short) 1); // make address unaligned.
    assertEquals(buf.writeVarUint32Aligned(Integer.MAX_VALUE), 9);
    buf.readByte();
    buf.readInt16();
    assertEquals(buf.readAlignedVarUint(), Integer.MAX_VALUE);
    for (int i = 0; i < 32; i++) {
      MemoryBuffer buf1 = MemoryUtils.buffer(16);
      assertAligned(i, buf1);
    }
    MemoryBuffer buf1 = MemoryUtils.buffer(16);
    for (int i = 0; i < 32; i++) {
      assertAligned(i, buf1);
    }
  }