]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/text/CharsetRecog_UTF8.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / text / CharsetRecog_UTF8.java
1 /**\r
2 *******************************************************************************\r
3 * Copyright (C) 2005 - 2007, International Business Machines Corporation and  *\r
4 * others. All Rights Reserved.                                                *\r
5 *******************************************************************************\r
6 */\r
7 package com.ibm.icu.text;\r
8 \r
9 /**\r
10  * Charset recognizer for UTF-8\r
11  *\r
12  * @internal\r
13  */\r
14 class CharsetRecog_UTF8 extends CharsetRecognizer {\r
15 \r
16     String getName() {\r
17         return "UTF-8";\r
18     }\r
19 \r
20     /* (non-Javadoc)\r
21      * @see com.ibm.icu.text.CharsetRecognizer#match(com.ibm.icu.text.CharsetDetector)\r
22      */\r
23     int match(CharsetDetector det) {\r
24         boolean     hasBOM = false;\r
25         int         numValid = 0;\r
26         int         numInvalid = 0;\r
27         byte        input[] = det.fRawInput;\r
28         int         i;\r
29         int         trailBytes = 0;\r
30         int         confidence;\r
31         \r
32         if (det.fRawLength >= 3 && \r
33                 (input[0] & 0xFF) == 0xef && (input[1] & 0xFF) == 0xbb & (input[2] & 0xFF) == 0xbf) {\r
34             hasBOM = true;\r
35         }\r
36         \r
37         // Scan for multi-byte sequences\r
38         for (i=0; i<det.fRawLength; i++) {\r
39             int b = input[i];\r
40             if ((b & 0x80) == 0) {\r
41                 continue;   // ASCII\r
42             }\r
43             \r
44             // Hi bit on char found.  Figure out how long the sequence should be\r
45             if ((b & 0x0e0) == 0x0c0) {\r
46                 trailBytes = 1;                \r
47             } else if ((b & 0x0f0) == 0x0e0) {\r
48                 trailBytes = 2;\r
49             } else if ((b & 0x0f8) == 0xf0) {\r
50                 trailBytes = 3;\r
51             } else {\r
52                 numInvalid++;\r
53                 if (numInvalid > 5) {\r
54                     break;\r
55                 }\r
56                 trailBytes = 0;\r
57             }\r
58                 \r
59             // Verify that we've got the right number of trail bytes in the sequence\r
60             for (;;) {\r
61                 i++;\r
62                 if (i>=det.fRawLength) {\r
63                     break;\r
64                 }\r
65                 b = input[i];\r
66                 if ((b & 0xc0) != 0x080) {\r
67                     numInvalid++;\r
68                     break;\r
69                 }\r
70                 if (--trailBytes == 0) {\r
71                     numValid++;\r
72                     break;\r
73                 }\r
74             }\r
75                         \r
76         }\r
77         \r
78         // Cook up some sort of confidence score, based on presense of a BOM\r
79         //    and the existence of valid and/or invalid multi-byte sequences.\r
80         confidence = 0;\r
81         if (hasBOM && numInvalid==0) {\r
82             confidence = 100;\r
83         } else if (hasBOM && numValid > numInvalid*10) {\r
84             confidence = 80;\r
85         } else if (numValid > 3 && numInvalid == 0) {\r
86             confidence = 100;            \r
87         } else if (numValid > 0 && numInvalid == 0) {\r
88             confidence = 80;\r
89         } else if (numValid == 0 && numInvalid == 0) {\r
90             // Plain ASCII.  \r
91             confidence = 10;            \r
92         } else if (numValid > numInvalid*10) {\r
93             // Probably corruput utf-8 data.  Valid sequences aren't likely by chance.\r
94             confidence = 25;\r
95         }\r
96         return confidence;\r
97     }\r
98 \r
99 }\r