]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/tools/misc/src/com/ibm/icu/dev/tool/charsetdet/sbcs/InputFile.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / tools / misc / src / com / ibm / icu / dev / tool / charsetdet / sbcs / InputFile.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  */\r
8 \r
9 package com.ibm.icu.dev.tool.charsetdet.sbcs;\r
10 \r
11 import java.io.File;\r
12 import java.io.FileInputStream;\r
13 import java.io.InputStreamReader;\r
14 import java.nio.ByteBuffer;\r
15 import java.nio.CharBuffer;\r
16 import java.nio.charset.CharacterCodingException;\r
17 import java.nio.charset.Charset;\r
18 import java.nio.charset.CharsetDecoder;\r
19 import java.nio.charset.CharsetEncoder;\r
20 import java.nio.charset.CodingErrorAction;\r
21 \r
22 /**\r
23  * @author emader\r
24  *\r
25  * TODO To change the template for this generated type comment go to\r
26  * Window - Preferences - Java - Code Style - Code Templates\r
27  */\r
28 public class InputFile implements NGramList.NGramKeyMapper\r
29 {\r
30 \r
31     private File file;\r
32     private FileInputStream fileStream;\r
33     private InputStreamReader inputStream;\r
34 \r
35     private Charset charset;\r
36     private CharsetDecoder decoder;\r
37     private CharsetEncoder encoder;\r
38     \r
39     private boolean visualOrder;\r
40 \r
41     private static void exceptionError(Exception e)\r
42     {\r
43         System.err.println("ioError: " + e.toString());\r
44     }\r
45 \r
46     /**\r
47      * \r
48      */\r
49     public InputFile(String filename, String encoding, boolean visual)\r
50     {\r
51         file = new File(filename);\r
52         setEncoding(encoding);\r
53         visualOrder = visual;\r
54     }\r
55     \r
56     public boolean open()\r
57     {\r
58         try {\r
59             fileStream = new FileInputStream(file);          \r
60             inputStream = new InputStreamReader(fileStream, "UTF8");\r
61         } catch (Exception e) {\r
62             exceptionError(e);\r
63             return false;\r
64         }\r
65         \r
66         return true;\r
67     }\r
68     \r
69     public void close()\r
70     {\r
71         try {\r
72             inputStream.close();\r
73             fileStream.close();\r
74         } catch (Exception e) {\r
75             // don't really care if this fails...\r
76         }\r
77     }\r
78     \r
79     public String getFilename()\r
80     {\r
81         return file.getName();\r
82     }\r
83     \r
84     public String getParent()\r
85     {\r
86         return file.getParent();\r
87     }\r
88     \r
89     public String getPath()\r
90     {\r
91         return file.getPath();\r
92     }\r
93     \r
94     public int read(char[] buffer)\r
95     {\r
96         int charsRead = -1;\r
97         \r
98         try {\r
99             charsRead = inputStream.read(buffer, 0, buffer.length);\r
100         } catch (Exception e) {\r
101             exceptionError(e);\r
102         }\r
103         \r
104         return charsRead;\r
105     }\r
106     \r
107     public void setEncoding(String encoding)\r
108     {\r
109         charset = Charset.forName(encoding);\r
110         decoder = charset.newDecoder();\r
111         encoder = charset.newEncoder();\r
112         \r
113         encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);\r
114         encoder.onMalformedInput(CodingErrorAction.REPLACE);\r
115         \r
116         decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);\r
117         decoder.onMalformedInput(CodingErrorAction.REPLACE);\r
118     }\r
119     \r
120     public String getEncoding()\r
121     {\r
122         return charset.displayName();\r
123     }\r
124     \r
125     public boolean getVisualOrder()\r
126     {\r
127         return visualOrder;\r
128     }\r
129     \r
130     public Object mapKey(String key)\r
131     {\r
132         byte[] bytes = encode(key.toCharArray());\r
133         int length   = key.length();\r
134         int value    = 0;\r
135         \r
136         for(int b = 0; b < length; b += 1) {\r
137             value <<= 8;\r
138             value += (bytes[b] & 0xFF);\r
139         }\r
140 \r
141         return new Integer(value);\r
142     }\r
143     \r
144     public byte[] encode(char[] chars)\r
145     {\r
146         CharBuffer cb = CharBuffer.wrap(chars);\r
147         ByteBuffer bb;\r
148         \r
149         try {\r
150             bb = encoder.encode(cb);\r
151         } catch (CharacterCodingException e) {\r
152             // don't expect to get any exceptions in normal usage...\r
153             return null;\r
154         }\r
155 \r
156         return bb.array();\r
157     }\r
158     \r
159     public char[] decode(byte[] bytes)\r
160     {\r
161         ByteBuffer bb = ByteBuffer.wrap(bytes);\r
162         CharBuffer cb;\r
163         \r
164         try {\r
165             cb = decoder.decode(bb);\r
166         } catch (CharacterCodingException e) {\r
167             // don't expect to get any exceptions in normal usage...\r
168             return null;\r
169         }\r
170         \r
171         return cb.array();\r
172     }\r
173 }\r