code: public void ser(DataOutput out) throws IOException {
      if (bits.length == 0) {
        out.writeByte(0);
        return;
      }
      // removing trailing empty bytes.
      int bytesToWrite;
      for (bytesToWrite = bits.length; bytesToWrite > 1 && bits[bytesToWrite - 1] == 0; bytesToWrite--) ;
      // Writing first bytes, with the rightmost bit set
      for (int i = 0; i < (bytesToWrite - 1); i++) {
        out.writeByte((bits[i] | 1));
      }
      // Writing the last byte, with the rightmost bit unset
      out.writeByte((bits[bytesToWrite - 1] & ~1));
    }
docs: Serializes the bit field to the data output. It uses one byte per each 7 bits. If the
  rightmost bit of the read byte is set, that means that there are more bytes to consume.
  The latest byte has the rightmost bit unset.