code: public static int writeIntAsVarIntBytes(int intVal, byte[] bytes, int offset) {
          int pos = offset;
          int v = intVal;
  
          if ((v & ~0x7F) == 0) {
              bytes[pos++] = ((byte) v);
              return 1 + offset;
          }
  
          while (true) {
              if ((v & ~0x7F) == 0) {
                  bytes[pos++] = ((byte) v);
                  return pos;
              } else {
                  bytes[pos++] = (byte) ((v & 0x7F) | 0x80);
                  v >>>= 7;
              }
          }
      }
docs: Writes an integer to the byte array in the Varint format.
  @param intVal - integer value to write to the bytes buffer in the Varint format
  @param bytes - byte buffer to write to - must contain enough space for maximum
  length which is 5 bytes.
  @param offset - the offset within the bytes buffer to start writing
  @return - returns the number of bytes used from the bytes buffer