out.put ((byte) (0xF0 | (value >> 18)));
out.put ((byte) (0xF0 | ((value >> 18) & 0x07)));
    protected CoderResult encodeLoop (CharBuffer in, ByteBuffer out)    {      int inPos = in.position();      try        {          // TODO: Optimize this in the case in.hasArray() / out.hasArray()          while (in.hasRemaining ())          {            int remaining = out.remaining ();            char c = in.get ();            // UCS-4 range (hex.)           UTF-8 octet sequence (binary)            // 0000 0000-0000 007F   0xxxxxxx            // 0000 0080-0000 07FF   110xxxxx 10xxxxxx            // 0000 0800-0000 FFFF   1110xxxx 10xxxxxx 10xxxxxx            //        Scalar Value          UTF-16                byte 1     byte 2     byte 3     byte 4            //        0000 0000 0xxx xxxx   0000 0000 0xxx xxxx   0xxx xxxx            //        0000 0yyy yyxx xxxx   0000 0yyy yyxx xxxx   110y yyyy  10xx xxxx            //        zzzz yyyy yyxx xxxx   zzzz yyyy yyxx xxxx   1110 zzzz  10yy yyyy  10xx xxxx            // u uuuu zzzz yyyy yyxx xxxx   1101 10ww wwzz zzyy   1111 0uuu  10uu zzzz  10yy yyyy  10xx xxxx            //                            + 1101 11yy yyxx xxxx            // Note: uuuuu = wwww + 1            if (c <= 0x7F)              {                if (remaining < 1)                  return CoderResult.OVERFLOW;                out.put ((byte) c);                inPos++;              }            else if (c <= 0x7FF)              {                if (remaining < 2)                  return CoderResult.OVERFLOW;                out.put ((byte) (0xC0 | (c >> 6)));                out.put ((byte) (0x80 | (c & 0x3F)));                inPos++;              }            else if (0xD800 <= c && c <= 0xDFFF)              {                if (remaining < 4)                  return CoderResult.OVERFLOW;                // we got a low surrogate without a preciding high one                if (c > 0xDBFF)                  return CoderResult.malformedForLength (1);                // high surrogates                if (!in.hasRemaining ())                  return CoderResult.UNDERFLOW;                char d = in.get ();                // make sure d is a low surrogate                if (d < 0xDC00 || d > 0xDFFF)                  return CoderResult.malformedForLength (1);                // make the 32 bit value                // int value2 = (c - 0xD800) * 0x400 + (d - 0xDC00) + 0x10000;                int value = (((c & 0x3FF) << 10) | (d & 0x3FF)) + 0x10000;                // assert value == value2;                out.put ((byte) (0xF0 | (value >> 18)));                out.put ((byte) (0x80 | ((value >> 12) & 0x3F)));                out.put ((byte) (0x80 | ((value >>  6) & 0x3F)));                out.put ((byte) (0x80 | ((value      ) & 0x3F)));                                inPos += 2;              }            else              {                if (remaining < 3)                  return CoderResult.OVERFLOW;                out.put ((byte) (0xE0 | (c >> 12)));                out.put ((byte) (0x80 | ((c >> 6) & 0x3F)));                out.put ((byte) (0x80 | (c & 0x3F)));                inPos++;              }          }          return CoderResult.UNDERFLOW;        }      finally        {          // In case we did a get(), then encountered an error, reset the          // position to before the error.  If there was no error, this          // will benignly reset the position to the value it already has.          in.position (inPos);        }    }