docs: public void write(String str, boolean escape, boolean noBackslashEscapes) throws IOException {
  
      int charsLength = str.length();
  
      //not enough space remaining
      if (charsLength * 3 + 2 >= buf.length - pos) {
        byte[] arr = str.getBytes("UTF-8");
        if (escape) {
          write(QUOTE);
          writeBytesEscaped(arr, arr.length, noBackslashEscapes);
          write(QUOTE);
        } else {
          write(arr, 0, arr.length);
        }
        return;
      }
  
      //create UTF-8 byte array
      //since java char are internally using UTF-16 using surrogate's pattern, 4 bytes unicode characters will
      //represent 2 characters : example "\uD83C\uDFA4" = 🎤 unicode 8 "no microphones"
      //so max size is 3 * charLength
      //(escape characters are 1 byte encoded, so length might only be 2 when escape)
      // + 2 for the quotes for text protocol
      int charsOffset = 0;
      char currChar;
  
      //quick loop if only ASCII chars for faster escape
      if (escape) {
        buf[pos++] = QUOTE;
        if (noBackslashEscapes) {
          for (; charsOffset < charsLength && (currChar = str.charAt(charsOffset)) < 0x80;
              charsOffset++) {
            if (currChar == QUOTE) {
              buf[pos++] = QUOTE;
            }
            buf[pos++] = (byte) currChar;
          }
        } else {
          for (; charsOffset < charsLength && (currChar = str.charAt(charsOffset)) < 0x80;
              charsOffset++) {
            if (currChar == BACKSLASH || currChar == QUOTE || currChar == 0
                || currChar == DBL_QUOTE) {
              buf[pos++] = BACKSLASH;
            }
            buf[pos++] = (byte) currChar;
          }
        }
      } else {
        for (; charsOffset < charsLength && (currChar = str.charAt(charsOffset)) < 0x80;
            charsOffset++) {
          buf[pos++] = (byte) currChar;
        }
      }
  
      //if quick loop not finished
      while (charsOffset < charsLength) {
        currChar = str.charAt(charsOffset++);
        if (currChar < 0x80) {
          if (escape) {
            if (noBackslashEscapes) {
              if (currChar == QUOTE) {
                buf[pos++] = QUOTE;
              }
            } else if (currChar == BACKSLASH || currChar == QUOTE || currChar == ZERO_BYTE
                || currChar == DBL_QUOTE) {
              buf[pos++] = BACKSLASH;
            }
          }
          buf[pos++] = (byte) currChar;
        } else if (currChar < 0x800) {
          buf[pos++] = (byte) (0xc0 | (currChar >> 6));
          buf[pos++] = (byte) (0x80 | (currChar & 0x3f));
        } else if (currChar >= 0xD800 && currChar < 0xE000) {
          //reserved for surrogate - see https://en.wikipedia.org/wiki/UTF-16
          if (currChar < 0xDC00) {
            //is high surrogate
            if (charsOffset + 1 > charsLength) {
              buf[pos++] = (byte) 0x63;
            } else {
              char nextChar = str.charAt(charsOffset);
              if (nextChar >= 0xDC00 && nextChar < 0xE000) {
                //is low surrogate
                int surrogatePairs =
                    ((currChar << 10) + nextChar) + (0x010000 - (0xD800 << 10) - 0xDC00);
                buf[pos++] = (byte) (0xf0 | ((surrogatePairs >> 18)));
                buf[pos++] = (byte) (0x80 | ((surrogatePairs >> 12) & 0x3f));
                buf[pos++] = (byte) (0x80 | ((surrogatePairs >> 6) & 0x3f));
                buf[pos++] = (byte) (0x80 | (surrogatePairs & 0x3f));
                charsOffset++;
              } else {
                //must have low surrogate
                buf[pos++] = (byte) 0x3f;
              }
            }
          } else {
            //low surrogate without high surrogate before
            buf[pos++] = (byte) 0x3f;
          }
        } else {
          buf[pos++] = (byte) (0xe0 | ((currChar >> 12)));
          buf[pos++] = (byte) (0x80 | ((currChar >> 6) & 0x3f));
          buf[pos++] = (byte) (0x80 | (currChar & 0x3f));
        }
      }
      if (escape) {
        buf[pos++] = QUOTE;
      }
  
    }