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

Print this page




  94             return (b1 == (byte)0xe0 && (b2 & 0xe0) == 0x80) ||
  95                    (b2 & 0xc0) != 0x80 || (b3 & 0xc0) != 0x80;
  96         }
  97 
  98         // only used when there is only one byte left in src buffer
  99         private static boolean isMalformed3_2(int b1, int b2) {
 100             return (b1 == (byte)0xe0 && (b2 & 0xe0) == 0x80) ||
 101                    (b2 & 0xc0) != 0x80;
 102         }
 103 
 104         //  [F0]     [90..BF] [80..BF] [80..BF]
 105         //  [F1..F3] [80..BF] [80..BF] [80..BF]
 106         //  [F4]     [80..8F] [80..BF] [80..BF]
 107         //  only check 80-be range here, the [0xf0,0x80...] and [0xf4,0x90-...]
 108         //  will be checked by Character.isSupplementaryCodePoint(uc)
 109         private static boolean isMalformed4(int b2, int b3, int b4) {
 110             return (b2 & 0xc0) != 0x80 || (b3 & 0xc0) != 0x80 ||
 111                    (b4 & 0xc0) != 0x80;
 112         }
 113 
 114         // only used when there is less than 4 bytes left in src buffer

 115         private static boolean isMalformed4_2(int b1, int b2) {
 116             return (b1 == 0xf0 && b2 == 0x90) ||

 117                    (b2 & 0xc0) != 0x80;
 118         }
 119 


 120         private static boolean isMalformed4_3(int b3) {
 121             return (b3 & 0xc0) != 0x80;
 122         }
 123 
 124         private static CoderResult lookupN(ByteBuffer src, int n)
 125         {
 126             for (int i = 1; i < n; i++) {
 127                if (isNotContinuation(src.get()))
 128                    return CoderResult.malformedForLength(i);
 129             }
 130             return CoderResult.malformedForLength(n);
 131         }
 132 
 133         private static CoderResult malformedN(ByteBuffer src, int nb) {
 134             switch (nb) {
 135             case 1:
 136             case 2:                    // always 1
 137                 return CoderResult.malformedForLength(1);
 138             case 3:
 139                 int b1 = src.get();


 263                     }
 264                     int b2 = sa[sp + 1];
 265                     int b3 = sa[sp + 2];
 266                     if (isMalformed3(b1, b2, b3))
 267                         return malformed(src, sp, dst, dp, 3);
 268                     char c = (char)
 269                         ((b1 << 12) ^
 270                          (b2 <<  6) ^
 271                          (b3 ^
 272                           (((byte) 0xE0 << 12) ^
 273                            ((byte) 0x80 <<  6) ^
 274                            ((byte) 0x80 <<  0))));
 275                     if (Character.isSurrogate(c))
 276                         return malformedForLength(src, sp, dst, dp, 3);
 277                     da[dp++] = c;
 278                     sp += 3;
 279                 } else if ((b1 >> 3) == -2) {
 280                     // 4 bytes, 21 bits: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
 281                     int srcRemaining = sl - sp;
 282                     if (srcRemaining < 4 || dl - dp < 2) {
 283                         if (srcRemaining > 1 && isMalformed4_2(b1, sa[sp + 1]))


 284                             return malformedForLength(src, sp, dst, dp, 1);
 285                         if (srcRemaining > 2 && isMalformed4_3(sa[sp + 2]))
 286                             return malformedForLength(src, sp, dst, dp, 2);
 287                         return xflow(src, sp, sl, dst, dp, 4);
 288                     }
 289                     int b2 = sa[sp + 1];
 290                     int b3 = sa[sp + 2];
 291                     int b4 = sa[sp + 3];
 292                     int uc = ((b1 << 18) ^
 293                               (b2 << 12) ^
 294                               (b3 <<  6) ^
 295                               (b4 ^
 296                                (((byte) 0xF0 << 18) ^
 297                                 ((byte) 0x80 << 12) ^
 298                                 ((byte) 0x80 <<  6) ^
 299                                 ((byte) 0x80 <<  0))));
 300                     if (isMalformed4(b2, b3, b4) ||
 301                         // shortest form check
 302                         !Character.isSupplementaryCodePoint(uc)) {
 303                         return malformed(src, sp, dst, dp, 4);


 346                     }
 347                     int b2 = src.get();
 348                     int b3 = src.get();
 349                     if (isMalformed3(b1, b2, b3))
 350                         return malformed(src, mark, 3);
 351                     char c = (char)
 352                         ((b1 << 12) ^
 353                          (b2 <<  6) ^
 354                          (b3 ^
 355                           (((byte) 0xE0 << 12) ^
 356                            ((byte) 0x80 <<  6) ^
 357                            ((byte) 0x80 <<  0))));
 358                     if (Character.isSurrogate(c))
 359                         return malformedForLength(src, mark, 3);
 360                     dst.put(c);
 361                     mark += 3;
 362                 } else if ((b1 >> 3) == -2) {
 363                     // 4 bytes, 21 bits: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
 364                     int srcRemaining = limit - mark;
 365                     if (srcRemaining < 4 || dst.remaining() < 2) {
 366                         if (srcRemaining > 1 && isMalformed4_2(b1, src.get()))


 367                             return malformedForLength(src, mark, 1);
 368                         if (srcRemaining > 2 && isMalformed4_3(src.get()))
 369                             return malformedForLength(src, mark, 2);
 370                         return xflow(src, mark, 4);
 371                     }
 372                     int b2 = src.get();
 373                     int b3 = src.get();
 374                     int b4 = src.get();
 375                     int uc = ((b1 << 18) ^
 376                               (b2 << 12) ^
 377                               (b3 <<  6) ^
 378                               (b4 ^
 379                                (((byte) 0xF0 << 18) ^
 380                                 ((byte) 0x80 << 12) ^
 381                                 ((byte) 0x80 <<  6) ^
 382                                 ((byte) 0x80 <<  0))));
 383                     if (isMalformed4(b2, b3, b4) ||
 384                         // shortest form check
 385                         !Character.isSupplementaryCodePoint(uc)) {
 386                         return malformed(src, mark, 4);


 501                                    ((byte) 0x80 << 12) ^
 502                                    ((byte) 0x80 <<  6) ^
 503                                    ((byte) 0x80 <<  0))));
 504                         if (isMalformed4(b2, b3, b4) ||
 505                             // shortest form check
 506                             !Character.isSupplementaryCodePoint(uc)) {
 507                             if (malformedInputAction() != CodingErrorAction.REPLACE)
 508                                 return -1;
 509                             da[dp++] = replacement().charAt(0);
 510                             sp -= 4;
 511                             bb = getByteBuffer(bb, sa, sp);
 512                             sp += malformedN(bb, 4).length();
 513                         } else {
 514                             da[dp++] = Character.highSurrogate(uc);
 515                             da[dp++] = Character.lowSurrogate(uc);
 516                         }
 517                         continue;
 518                     }
 519                     if (malformedInputAction() != CodingErrorAction.REPLACE)
 520                         return -1;
 521 
 522                     if (sp  < sl && isMalformed4_2(b1, sa[sp])) {

 523                         da[dp++] = replacement().charAt(0);
 524                         continue;
 525                     }
 526                     sp++;
 527                     if (sp  < sl && isMalformed4_3(sa[sp])) {
 528                         da[dp++] = replacement().charAt(0);
 529                         continue;
 530                     }
 531                     da[dp++] = replacement().charAt(0);
 532                     return dp;
 533                 } else {
 534                     if (malformedInputAction() != CodingErrorAction.REPLACE)
 535                         return -1;
 536                     da[dp++] = replacement().charAt(0);
 537                 }
 538             }
 539             return dp;
 540         }
 541     }
 542 




  94             return (b1 == (byte)0xe0 && (b2 & 0xe0) == 0x80) ||
  95                    (b2 & 0xc0) != 0x80 || (b3 & 0xc0) != 0x80;
  96         }
  97 
  98         // only used when there is only one byte left in src buffer
  99         private static boolean isMalformed3_2(int b1, int b2) {
 100             return (b1 == (byte)0xe0 && (b2 & 0xe0) == 0x80) ||
 101                    (b2 & 0xc0) != 0x80;
 102         }
 103 
 104         //  [F0]     [90..BF] [80..BF] [80..BF]
 105         //  [F1..F3] [80..BF] [80..BF] [80..BF]
 106         //  [F4]     [80..8F] [80..BF] [80..BF]
 107         //  only check 80-be range here, the [0xf0,0x80...] and [0xf4,0x90-...]
 108         //  will be checked by Character.isSupplementaryCodePoint(uc)
 109         private static boolean isMalformed4(int b2, int b3, int b4) {
 110             return (b2 & 0xc0) != 0x80 || (b3 & 0xc0) != 0x80 ||
 111                    (b4 & 0xc0) != 0x80;
 112         }
 113 
 114         // only used when there is less than 4 bytes left in src buffer.
 115         // both b1 and b2 should be "& 0xff" before passed in.
 116         private static boolean isMalformed4_2(int b1, int b2) {
 117             return (b1 == 0xf0 && (b2  < 0x90 || b2 > 0xbf)) ||
 118                    (b1 == 0xf4 && (b2 & 0xf0) != 0x80) ||
 119                    (b2 & 0xc0) != 0x80;
 120         }
 121 
 122         // only used when there is less than 4 bytes left in src buffer,
 123         // after isMalformed4_2 has been invoked.
 124         private static boolean isMalformed4_3(int b3) {
 125             return (b3 & 0xc0) != 0x80;
 126         }
 127 
 128         private static CoderResult lookupN(ByteBuffer src, int n)
 129         {
 130             for (int i = 1; i < n; i++) {
 131                if (isNotContinuation(src.get()))
 132                    return CoderResult.malformedForLength(i);
 133             }
 134             return CoderResult.malformedForLength(n);
 135         }
 136 
 137         private static CoderResult malformedN(ByteBuffer src, int nb) {
 138             switch (nb) {
 139             case 1:
 140             case 2:                    // always 1
 141                 return CoderResult.malformedForLength(1);
 142             case 3:
 143                 int b1 = src.get();


 267                     }
 268                     int b2 = sa[sp + 1];
 269                     int b3 = sa[sp + 2];
 270                     if (isMalformed3(b1, b2, b3))
 271                         return malformed(src, sp, dst, dp, 3);
 272                     char c = (char)
 273                         ((b1 << 12) ^
 274                          (b2 <<  6) ^
 275                          (b3 ^
 276                           (((byte) 0xE0 << 12) ^
 277                            ((byte) 0x80 <<  6) ^
 278                            ((byte) 0x80 <<  0))));
 279                     if (Character.isSurrogate(c))
 280                         return malformedForLength(src, sp, dst, dp, 3);
 281                     da[dp++] = c;
 282                     sp += 3;
 283                 } else if ((b1 >> 3) == -2) {
 284                     // 4 bytes, 21 bits: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
 285                     int srcRemaining = sl - sp;
 286                     if (srcRemaining < 4 || dl - dp < 2) {
 287                         b1 &= 0xff;
 288                         if (b1 > 0xf4 ||
 289                             srcRemaining > 1 && isMalformed4_2(b1, sa[sp + 1] & 0xff))
 290                             return malformedForLength(src, sp, dst, dp, 1);
 291                         if (srcRemaining > 2 && isMalformed4_3(sa[sp + 2]))
 292                             return malformedForLength(src, sp, dst, dp, 2);
 293                         return xflow(src, sp, sl, dst, dp, 4);
 294                     }
 295                     int b2 = sa[sp + 1];
 296                     int b3 = sa[sp + 2];
 297                     int b4 = sa[sp + 3];
 298                     int uc = ((b1 << 18) ^
 299                               (b2 << 12) ^
 300                               (b3 <<  6) ^
 301                               (b4 ^
 302                                (((byte) 0xF0 << 18) ^
 303                                 ((byte) 0x80 << 12) ^
 304                                 ((byte) 0x80 <<  6) ^
 305                                 ((byte) 0x80 <<  0))));
 306                     if (isMalformed4(b2, b3, b4) ||
 307                         // shortest form check
 308                         !Character.isSupplementaryCodePoint(uc)) {
 309                         return malformed(src, sp, dst, dp, 4);


 352                     }
 353                     int b2 = src.get();
 354                     int b3 = src.get();
 355                     if (isMalformed3(b1, b2, b3))
 356                         return malformed(src, mark, 3);
 357                     char c = (char)
 358                         ((b1 << 12) ^
 359                          (b2 <<  6) ^
 360                          (b3 ^
 361                           (((byte) 0xE0 << 12) ^
 362                            ((byte) 0x80 <<  6) ^
 363                            ((byte) 0x80 <<  0))));
 364                     if (Character.isSurrogate(c))
 365                         return malformedForLength(src, mark, 3);
 366                     dst.put(c);
 367                     mark += 3;
 368                 } else if ((b1 >> 3) == -2) {
 369                     // 4 bytes, 21 bits: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
 370                     int srcRemaining = limit - mark;
 371                     if (srcRemaining < 4 || dst.remaining() < 2) {
 372                         b1 &= 0xff;
 373                         if (b1 > 0xf4 ||
 374                             srcRemaining > 1 && isMalformed4_2(b1, src.get() & 0xff))
 375                             return malformedForLength(src, mark, 1);
 376                         if (srcRemaining > 2 && isMalformed4_3(src.get()))
 377                             return malformedForLength(src, mark, 2);
 378                         return xflow(src, mark, 4);
 379                     }
 380                     int b2 = src.get();
 381                     int b3 = src.get();
 382                     int b4 = src.get();
 383                     int uc = ((b1 << 18) ^
 384                               (b2 << 12) ^
 385                               (b3 <<  6) ^
 386                               (b4 ^
 387                                (((byte) 0xF0 << 18) ^
 388                                 ((byte) 0x80 << 12) ^
 389                                 ((byte) 0x80 <<  6) ^
 390                                 ((byte) 0x80 <<  0))));
 391                     if (isMalformed4(b2, b3, b4) ||
 392                         // shortest form check
 393                         !Character.isSupplementaryCodePoint(uc)) {
 394                         return malformed(src, mark, 4);


 509                                    ((byte) 0x80 << 12) ^
 510                                    ((byte) 0x80 <<  6) ^
 511                                    ((byte) 0x80 <<  0))));
 512                         if (isMalformed4(b2, b3, b4) ||
 513                             // shortest form check
 514                             !Character.isSupplementaryCodePoint(uc)) {
 515                             if (malformedInputAction() != CodingErrorAction.REPLACE)
 516                                 return -1;
 517                             da[dp++] = replacement().charAt(0);
 518                             sp -= 4;
 519                             bb = getByteBuffer(bb, sa, sp);
 520                             sp += malformedN(bb, 4).length();
 521                         } else {
 522                             da[dp++] = Character.highSurrogate(uc);
 523                             da[dp++] = Character.lowSurrogate(uc);
 524                         }
 525                         continue;
 526                     }
 527                     if (malformedInputAction() != CodingErrorAction.REPLACE)
 528                         return -1;
 529                     b1 &= 0xff;
 530                     if (b1 > 0xf4 ||
 531                         sp  < sl && isMalformed4_2(b1, sa[sp] & 0xff)) {
 532                         da[dp++] = replacement().charAt(0);
 533                         continue;
 534                     }
 535                     sp++;
 536                     if (sp  < sl && isMalformed4_3(sa[sp])) {
 537                         da[dp++] = replacement().charAt(0);
 538                         continue;
 539                     }
 540                     da[dp++] = replacement().charAt(0);
 541                     return dp;
 542                 } else {
 543                     if (malformedInputAction() != CodingErrorAction.REPLACE)
 544                         return -1;
 545                     da[dp++] = replacement().charAt(0);
 546                 }
 547             }
 548             return dp;
 549         }
 550     }
 551