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