code: public static int encode(String value, byte[] dst, int dstOffset) {
          if (value == null) {
              dst[dstOffset] = NULL_BYTE_HIGH;
              return 1;
          }
          final int originalOffset = dstOffset;
  
          int valueLength = value.length();
  
          // Write the value length first, in a variable amount of bytes.
          dstOffset += encodeUnsignedVarInt(valueLength, dst, dstOffset);
  
          for (int i = 0; i < valueLength; i++) {
              int c = value.charAt(i);
              if (c <= 0x7f) {
                  dst[dstOffset++] = (byte)c;
              } else if (c <= 0x3fff) {
                  dst[dstOffset++] = (byte)(0x80 | (c >> 8));
                  dst[dstOffset++] = (byte)(c & 0xff);
              } else {
                  if (c >= 0xd800 && c <= 0xdbff) {
                      // Found a high surrogate. Verify that surrogate pair is
                      // well-formed. Low surrogate must follow high surrogate.
                      if (i + 1 < valueLength) {
                          int c2 = value.charAt(i + 1);
                          if (c2 >= 0xdc00 && c2 <= 0xdfff) {
                              c = 0x10000 + (((c & 0x3ff) << 10) | (c2 & 0x3ff));
                              i++;
                          }
                      }
                  }
                  dst[dstOffset++] = (byte)(0xc0 | (c >> 16));
                  dst[dstOffset++] = (byte)((c >> 8) & 0xff);
                  dst[dstOffset++] = (byte)(c & 0xff);
              }
          }
  
          return dstOffset - originalOffset;
      }
docs: Encodes the given optional String into a variable amount of bytes. The
  amount written can be determined by calling
  calculateEncodedStringLength.
  <p>
  Strings are encoded in a fashion similar to UTF-8, in that ASCII
  characters are written in one byte. This encoding is more efficient than
  UTF-8, but it isn't compatible with UTF-8.
  
  @param value String value to encode, may be null
  @param dst destination for encoded bytes
  @param dstOffset offset into destination array
  @return amount of bytes written