1 7 package com.ibm.icu.text; 8 9 import java.io.ByteArrayInputStream ; 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.io.InputStreamReader ; 13 import java.io.Reader ; 14 15 16 30 public class CharsetMatch implements Comparable { 31 32 33 47 public Reader getReader() { 48 InputStream inputStream = fInputStream; 49 50 if (inputStream == null) { 51 inputStream = new ByteArrayInputStream (fRawInput, 0, fRawLength); 52 } 53 54 try { 55 inputStream.reset(); 56 return new InputStreamReader (inputStream, getName()); 57 } catch (IOException e) { 58 return null; 59 } 60 } 61 62 71 public String getString() throws java.io.IOException { 72 return getString(-1); 73 74 } 75 76 91 public String getString(int maxLength) throws java.io.IOException { 92 String result = null; 93 if (fInputStream != null) { 94 StringBuffer sb = new StringBuffer (); 95 char[] buffer = new char[1024]; 96 Reader reader = getReader(); 97 int max = maxLength < 0? Integer.MAX_VALUE : maxLength; 98 int bytesRead = 0; 99 100 while ((bytesRead = reader.read(buffer, 0, Math.min(max, 1024))) >= 0) { 101 sb.append(buffer, 0, bytesRead); 102 max -= bytesRead; 103 } 104 105 reader.close(); 106 107 return sb.toString(); 108 } else { 109 result = new String (fRawInput, getName()); 110 } 111 return result; 112 113 } 114 115 126 public int getConfidence() { 127 return fConfidence; 128 } 129 130 131 138 static public final int ENCODING_SCHEME = 1; 139 140 147 static public final int BOM = 2; 148 149 156 static public final int DECLARED_ENCODING = 4; 157 158 165 static public final int LANG_STATISTICS = 8; 166 167 182 public int getMatchType() { 183 return 0; 185 } 186 187 203 public String getName() { 204 return fRecognizer.getName(); 205 } 206 207 215 public String getLanguage() { 216 return fRecognizer.getLanguage(); 217 } 218 219 220 234 public int compareTo (Object o) { 235 CharsetMatch other = (CharsetMatch)o; 236 int compareResult = 0; 237 if (this.fConfidence > other.fConfidence) { 238 compareResult = 1; 239 } else if (this.fConfidence < other.fConfidence) { 240 compareResult = -1; 241 } 242 return compareResult; 243 } 244 245 250 CharsetMatch(CharsetDetector det, CharsetRecognizer rec, int conf) { 251 fRecognizer = rec; 252 fConfidence = conf; 253 254 if (det.fInputStream == null) { 258 fRawInput = det.fRawInput; 261 fRawLength = det.fRawLength; 262 } 263 fInputStream = det.fInputStream; 264 } 265 266 267 private int fConfidence; 271 private CharsetRecognizer fRecognizer; 272 private byte[] fRawInput = null; private int fRawLength; 276 private InputStream fInputStream = null; } 279 | Popular Tags |