else if (htmlNeedingEncoding) { String entityName = getHTMLCharacterEntity(c); if (entityName != null) { buf.append('&'); buf.append(entityName); buf.append(';'); } else buf.append(c); } else buf.append(c);
  final byte[] encodeText(String text)    throws IOException  {    encoder.reset();    if (!encoder.canEncode(text))      {        // Check each character        StringBuffer buf = new StringBuffer();        int len = text.length();        for (int i = 0; i < len; i++)          {            char c = text.charAt(i);            if (encoder.canEncode(c))              {                buf.append(c);              }            else              {                // Replace with character entity reference                String hex = Integer.toHexString((int) c);                buf.append("&#x");                buf.append(hex);                buf.append(';');              }          }            text = buf.toString();          }    ByteBuffer encoded = encoder.encode(CharBuffer.wrap(text));    int len = encoded.limit() - encoded.position();    if (encoded.hasArray())      {        byte[] ret = encoded.array();        if (ret.length > len)          {            // Why?            byte[] ret2 = new byte[len];            System.arraycopy(ret, 0, ret2, 0, len);            ret = ret2;          }        return ret;      }    encoded.flip();    byte[] ret = new byte[len];    encoded.get(ret, 0, len);    return ret;  }