]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/main/classes/core/src/com/ibm/icu/text/CharsetRecog_Unicode.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / main / classes / core / src / com / ibm / icu / text / CharsetRecog_Unicode.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 1996-2010, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  *
7  */
8
9 package com.ibm.icu.text;
10
11 /**
12  * This class matches UTF-16 and UTF-32, both big- and little-endian. The
13  * BOM will be used if it is present.
14  */
15 abstract class CharsetRecog_Unicode extends CharsetRecognizer {
16
17     /* (non-Javadoc)
18      * @see com.ibm.icu.text.CharsetRecognizer#getName()
19      */
20     abstract String getName();
21
22     /* (non-Javadoc)
23      * @see com.ibm.icu.text.CharsetRecognizer#match(com.ibm.icu.text.CharsetDetector)
24      */
25     abstract int match(CharsetDetector det);
26     
27     static class CharsetRecog_UTF_16_BE extends CharsetRecog_Unicode
28     {
29         String getName()
30         {
31             return "UTF-16BE";
32         }
33         
34         int match(CharsetDetector det)
35         {
36             byte[] input = det.fRawInput;
37             
38             if (input.length>=2 && ((input[0] & 0xFF) == 0xFE && (input[1] & 0xFF) == 0xFF)) {
39                 return 100;
40             }
41             
42             // TODO: Do some statistics to check for unsigned UTF-16BE
43             return 0;
44         }
45     }
46     
47     static class CharsetRecog_UTF_16_LE extends CharsetRecog_Unicode
48     {
49         String getName()
50         {
51             return "UTF-16LE";
52         }
53         
54         int match(CharsetDetector det)
55         {
56             byte[] input = det.fRawInput;
57             
58             if (input.length >= 2 && ((input[0] & 0xFF) == 0xFF && (input[1] & 0xFF) == 0xFE))
59             {
60                // An LE BOM is present.
61                if (input.length>=4 && input[2] == 0x00 && input[3] == 0x00) {
62                    // It is probably UTF-32 LE, not UTF-16
63                    return 0;
64                }
65                return 100;
66             }        
67             
68             // TODO: Do some statistics to check for unsigned UTF-16LE
69             return 0;
70         }
71     }
72     
73     static abstract class CharsetRecog_UTF_32 extends CharsetRecog_Unicode
74     {
75         abstract int getChar(byte[] input, int index);
76         
77         abstract String getName();
78         
79         int match(CharsetDetector det)
80         {
81             byte[] input   = det.fRawInput;
82             int limit      = (det.fRawLength / 4) * 4;
83             int numValid   = 0;
84             int numInvalid = 0;
85             boolean hasBOM = false;
86             int confidence = 0;
87             
88             if (limit==0) {
89                 return 0;
90             }
91             if (getChar(input, 0) == 0x0000FEFF) {
92                 hasBOM = true;
93             }
94             
95             for(int i = 0; i < limit; i += 4) {
96                 int ch = getChar(input, i);
97                 
98                 if (ch < 0 || ch >= 0x10FFFF || (ch >= 0xD800 && ch <= 0xDFFF)) {
99                     numInvalid += 1;
100                 } else {
101                     numValid += 1;
102                 }
103             }
104             
105             
106             // Cook up some sort of confidence score, based on presence of a BOM
107             //    and the existence of valid and/or invalid multi-byte sequences.
108             if (hasBOM && numInvalid==0) {
109                 confidence = 100;
110             } else if (hasBOM && numValid > numInvalid*10) {
111                 confidence = 80;
112             } else if (numValid > 3 && numInvalid == 0) {
113                 confidence = 100;            
114             } else if (numValid > 0 && numInvalid == 0) {
115                 confidence = 80;
116             } else if (numValid > numInvalid*10) {
117                 // Probably corrupt UTF-32BE data.  Valid sequences aren't likely by chance.
118                 confidence = 25;
119             }
120             
121             return confidence;
122         }
123     }
124     
125     static class CharsetRecog_UTF_32_BE extends CharsetRecog_UTF_32
126     {
127         int getChar(byte[] input, int index)
128         {
129             return (input[index + 0] & 0xFF) << 24 | (input[index + 1] & 0xFF) << 16 |
130                    (input[index + 2] & 0xFF) <<  8 | (input[index + 3] & 0xFF);
131         }
132         
133         String getName()
134         {
135             return "UTF-32BE";
136         }
137     }
138
139     
140     static class CharsetRecog_UTF_32_LE extends CharsetRecog_UTF_32
141     {
142         int getChar(byte[] input, int index)
143         {
144             return (input[index + 3] & 0xFF) << 24 | (input[index + 2] & 0xFF) << 16 |
145                    (input[index + 1] & 0xFF) <<  8 | (input[index + 0] & 0xFF);
146         }
147         
148         String getName()
149         {
150             return "UTF-32LE";
151         }
152     }
153 }