private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
      char[] sa = src.array();
      int sp = src.arrayOffset() + src.position();
      int sl = src.arrayOffset() + src.limit();

      byte[] da = dst.array();
      int dp = dst.arrayOffset() + dst.position();
      int dl = dst.arrayOffset() + dst.limit();
      int len = Math.min(dl - dp, sl - sp);

      while (len-- > 0) {
        char c = sa[sp];
        int b = encode(c);
        if (b == UNMAPPABLE_ENCODING) {
          if (Character.isSurrogate(c)) {
            if (sgp == null) sgp = new Surrogate.Parser();
            if (sgp.parse(c, sa, sp, sl) < 0) {
              return withResult(sgp.error(), src, sp, dst, dp);
            }
            return withResult(sgp.unmappableResult(), src, sp, dst, dp);
          }
          return withResult(CoderResult.unmappableForLength(1), src, sp, dst, dp);
        }
        da[dp++] = (byte) b;
        sp++;
      }
      return withResult(sp < sl ? CoderResult.OVERFLOW : CoderResult.UNDERFLOW, src, sp, dst, dp);
    }
 public int encode(char[] src, int sp, int len, byte[] dst) {
   int dp = 0;
   int sl = sp + Math.min(len, dst.length);
   while (sp < sl) {
     char c = src[sp++];
     int b = encode(c);
     if (b != UNMAPPABLE_ENCODING) {
       dst[dp++] = (byte) b;
       continue;
     }
     if (Character.isHighSurrogate(c) && sp < sl && Character.isLowSurrogate(src[sp])) {
       if (len > dst.length) {
         sl++;
         len--;
       }
       sp++;
     }
     dst[dp++] = repl;
   }
   return dp;
 }
 private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {
   int mark = src.position();
   try {
     while (src.hasRemaining()) {
       char c = src.get();
       int b = encode(c);
       if (b == UNMAPPABLE_ENCODING) {
         if (Character.isSurrogate(c)) {
           if (sgp == null) sgp = new Surrogate.Parser();
           if (sgp.parse(c, src) < 0) return sgp.error();
           return sgp.unmappableResult();
         }
         return CoderResult.unmappableForLength(1);
       }
       if (!dst.hasRemaining()) return CoderResult.OVERFLOW;
       dst.put((byte) b);
       mark++;
     }
     return CoderResult.UNDERFLOW;
   } finally {
     src.position(mark);
   }
 }