src/share/classes/sun/nio/cs/UTF_8.java

Print this page




 665                         return overflow(src, mark);
 666                     dst.put((byte)(0xe0 | ((c >> 12))));
 667                     dst.put((byte)(0x80 | ((c >>  6) & 0x3f)));
 668                     dst.put((byte)(0x80 | (c & 0x3f)));
 669                 }
 670                 mark++;
 671             }
 672             src.position(mark);
 673             return CoderResult.UNDERFLOW;
 674         }
 675 
 676         protected final CoderResult encodeLoop(CharBuffer src,
 677                                                ByteBuffer dst)
 678         {
 679             if (src.hasArray() && dst.hasArray())
 680                 return encodeArrayLoop(src, dst);
 681             else
 682                 return encodeBufferLoop(src, dst);
 683         }
 684 





 685         // returns -1 if there is malformed char(s) and the
 686         // "action" for malformed input is not REPLACE.
 687         public int encode(char[] sa, int sp, int len, byte[] da) {
 688             int sl = sp + len;
 689             int dp = 0;
 690             int dlASCII = dp + Math.min(len, da.length);
 691 
 692             // ASCII only optimized loop
 693             while (dp < dlASCII && sa[sp] < '\u0080')
 694                 da[dp++] = (byte) sa[sp++];
 695 
 696             while (sp < sl) {
 697                 char c = sa[sp++];
 698                 if (c < 0x80) {
 699                     // Have at most seven bits
 700                     da[dp++] = (byte)c;
 701                 } else if (c < 0x800) {
 702                     // 2 bytes, 11 bits
 703                     da[dp++] = (byte)(0xc0 | (c >> 6));
 704                     da[dp++] = (byte)(0x80 | (c & 0x3f));
 705                 } else if (Character.isSurrogate(c)) {
 706                     if (sgp == null)
 707                         sgp = new Surrogate.Parser();
 708                     int uc = sgp.parse(c, sa, sp - 1, sl);
 709                     if (uc < 0) {
 710                         if (malformedInputAction() != CodingErrorAction.REPLACE)
 711                             return -1;
 712                         da[dp++] = replacement()[0];
 713                     } else {
 714                         da[dp++] = (byte)(0xf0 | ((uc >> 18)));
 715                         da[dp++] = (byte)(0x80 | ((uc >> 12) & 0x3f));
 716                         da[dp++] = (byte)(0x80 | ((uc >>  6) & 0x3f));
 717                         da[dp++] = (byte)(0x80 | (uc & 0x3f));
 718                         sp++;  // 2 chars
 719                     }
 720                 } else {
 721                     // 3 bytes, 16 bits
 722                     da[dp++] = (byte)(0xe0 | ((c >> 12)));
 723                     da[dp++] = (byte)(0x80 | ((c >>  6) & 0x3f));
 724                     da[dp++] = (byte)(0x80 | (c & 0x3f));
 725                 }
 726             }
 727             return dp;
 728         }
 729     }
 730 }


 665                         return overflow(src, mark);
 666                     dst.put((byte)(0xe0 | ((c >> 12))));
 667                     dst.put((byte)(0x80 | ((c >>  6) & 0x3f)));
 668                     dst.put((byte)(0x80 | (c & 0x3f)));
 669                 }
 670                 mark++;
 671             }
 672             src.position(mark);
 673             return CoderResult.UNDERFLOW;
 674         }
 675 
 676         protected final CoderResult encodeLoop(CharBuffer src,
 677                                                ByteBuffer dst)
 678         {
 679             if (src.hasArray() && dst.hasArray())
 680                 return encodeArrayLoop(src, dst);
 681             else
 682                 return encodeBufferLoop(src, dst);
 683         }
 684 
 685         private byte repl = (byte)'?';
 686         protected void implReplaceWith(byte[] newReplacement) {
 687             repl = newReplacement[0];
 688         }
 689 
 690         // returns -1 if there is malformed char(s) and the
 691         // "action" for malformed input is not REPLACE.
 692         public int encode(char[] sa, int sp, int len, byte[] da) {
 693             int sl = sp + len;
 694             int dp = 0;
 695             int dlASCII = dp + Math.min(len, da.length);
 696 
 697             // ASCII only optimized loop
 698             while (dp < dlASCII && sa[sp] < '\u0080')
 699                 da[dp++] = (byte) sa[sp++];
 700 
 701             while (sp < sl) {
 702                 char c = sa[sp++];
 703                 if (c < 0x80) {
 704                     // Have at most seven bits
 705                     da[dp++] = (byte)c;
 706                 } else if (c < 0x800) {
 707                     // 2 bytes, 11 bits
 708                     da[dp++] = (byte)(0xc0 | (c >> 6));
 709                     da[dp++] = (byte)(0x80 | (c & 0x3f));
 710                 } else if (Character.isSurrogate(c)) {
 711                     if (sgp == null)
 712                         sgp = new Surrogate.Parser();
 713                     int uc = sgp.parse(c, sa, sp - 1, sl);
 714                     if (uc < 0) {
 715                         if (malformedInputAction() != CodingErrorAction.REPLACE)
 716                             return -1;
 717                         da[dp++] = repl;
 718                     } else {
 719                         da[dp++] = (byte)(0xf0 | ((uc >> 18)));
 720                         da[dp++] = (byte)(0x80 | ((uc >> 12) & 0x3f));
 721                         da[dp++] = (byte)(0x80 | ((uc >>  6) & 0x3f));
 722                         da[dp++] = (byte)(0x80 | (uc & 0x3f));
 723                         sp++;  // 2 chars
 724                     }
 725                 } else {
 726                     // 3 bytes, 16 bits
 727                     da[dp++] = (byte)(0xe0 | ((c >> 12)));
 728                     da[dp++] = (byte)(0x80 | ((c >>  6) & 0x3f));
 729                     da[dp++] = (byte)(0x80 | (c & 0x3f));
 730                 }
 731             }
 732             return dp;
 733         }
 734     }
 735 }