/*
   * Convert the textual message to bytes and then output it.
   */
  private void doWriteText(CharBuffer buffer, boolean finalFragment) throws IOException {
    CharsetEncoder encoder = B2CConverter.getUtf8().newEncoder();
    do {
      CoderResult cr = encoder.encode(buffer, bb, true);
      if (cr.isError()) {
        cr.throwException();
      }
      bb.flip();
      if (buffer.hasRemaining()) {
        doWriteBytes(bb, false);
      } else {
        doWriteBytes(bb, finalFragment);
      }
    } while (buffer.hasRemaining());

    // Reset - bb will be cleared in doWriteBytes()
    cb.clear();
  }