2 ***********************************************************************
\r
3 * Copyright (C) 2005-2007, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 ***********************************************************************
\r
9 package com.ibm.icu.dev.tool.charsetdet.sbcs;
\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
25 * TODO To change the template for this generated type comment go to
\r
26 * Window - Preferences - Java - Code Style - Code Templates
\r
28 public class InputFile implements NGramList.NGramKeyMapper
\r
32 private FileInputStream fileStream;
\r
33 private InputStreamReader inputStream;
\r
35 private Charset charset;
\r
36 private CharsetDecoder decoder;
\r
37 private CharsetEncoder encoder;
\r
39 private boolean visualOrder;
\r
41 private static void exceptionError(Exception e)
\r
43 System.err.println("ioError: " + e.toString());
\r
49 public InputFile(String filename, String encoding, boolean visual)
\r
51 file = new File(filename);
\r
52 setEncoding(encoding);
\r
53 visualOrder = visual;
\r
56 public boolean open()
\r
59 fileStream = new FileInputStream(file);
\r
60 inputStream = new InputStreamReader(fileStream, "UTF8");
\r
61 } catch (Exception e) {
\r
72 inputStream.close();
\r
74 } catch (Exception e) {
\r
75 // don't really care if this fails...
\r
79 public String getFilename()
\r
81 return file.getName();
\r
84 public String getParent()
\r
86 return file.getParent();
\r
89 public String getPath()
\r
91 return file.getPath();
\r
94 public int read(char[] buffer)
\r
99 charsRead = inputStream.read(buffer, 0, buffer.length);
\r
100 } catch (Exception e) {
\r
107 public void setEncoding(String encoding)
\r
109 charset = Charset.forName(encoding);
\r
110 decoder = charset.newDecoder();
\r
111 encoder = charset.newEncoder();
\r
113 encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
\r
114 encoder.onMalformedInput(CodingErrorAction.REPLACE);
\r
116 decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
\r
117 decoder.onMalformedInput(CodingErrorAction.REPLACE);
\r
120 public String getEncoding()
\r
122 return charset.displayName();
\r
125 public boolean getVisualOrder()
\r
127 return visualOrder;
\r
130 public Object mapKey(String key)
\r
132 byte[] bytes = encode(key.toCharArray());
\r
133 int length = key.length();
\r
136 for(int b = 0; b < length; b += 1) {
\r
138 value += (bytes[b] & 0xFF);
\r
141 return new Integer(value);
\r
144 public byte[] encode(char[] chars)
\r
146 CharBuffer cb = CharBuffer.wrap(chars);
\r
150 bb = encoder.encode(cb);
\r
151 } catch (CharacterCodingException e) {
\r
152 // don't expect to get any exceptions in normal usage...
\r
159 public char[] decode(byte[] bytes)
\r
161 ByteBuffer bb = ByteBuffer.wrap(bytes);
\r
165 cb = decoder.decode(bb);
\r
166 } catch (CharacterCodingException e) {
\r
167 // don't expect to get any exceptions in normal usage...
\r