-
Notifications
You must be signed in to change notification settings - Fork 19
/
KanjiInput.java
217 lines (190 loc) · 5.61 KB
/
KanjiInput.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* $Id: KanjiInput.java,v 1.5 2003/01/01 08:18:44 kawao Exp $
*
* KAKASI/JAVA
* Copyright (C) 2002-2003 KAWAO, Tomoyuki ([email protected])
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
package com.kawao.kakasi;
import java.io.Reader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.Writer;
import java.io.IOException;
import java.util.Set;
import java.util.HashSet;
/**
* An object of this class supplies inputs characters for the conversion.
*
* @see Kakasi#getInput()
* @author Kawao, Tomoyuki ([email protected])
* @version $Revision: 1.5 $ $Date: 2003/01/01 08:18:44 $
*/
public class KanjiInput {
private final StringBuffer buffer = new StringBuffer();
private int nextIndex;
private Reader reader;
private boolean spaceEatMode;
private final char[] oneCharacter = new char[1];
/**
* The constructor is not public.
*/
KanjiInput() {
}
/**
* Sets input as the specified string.
*
* @param string the string object for input.
*/
public void setInputString(String string) {
setReader(new StringReader(string));
}
/**
* Sets the reader object.
*
* @param newReader new reader object.
*/
public synchronized void setReader(Reader newReader) {
reader = newReader;
buffer.setLength(0);
}
/**
* Sets the space eat mode property value. The default value is false.
*
* @param newMode if true skip whitespace characters within jukugo.
*
*/
public void setSpaceEatMode(boolean newMode) {
spaceEatMode = newMode;
}
/**
* Gets the space eat mode property value.
*/
public boolean isSpaceEatMode() {
return spaceEatMode;
}
/**
* Gets the input character.
*
* @return The character read, or -1 if the end of the stream has been
* reached
* @exception IOException If an I/O error occurs
*/
synchronized int get() throws IOException {
if (reader == null) {
setReader(new BufferedReader(new InputStreamReader(System.in)));
}
if (buffer.length() == 0) {
int ch = reader.read();
if (ch < 0) {
return -1;
}
buffer.append((char)ch);
}
nextIndex = 1;
return buffer.charAt(0);
}
/**
* Gets more input character.
*
* @return The character read, or -1 if the end of the word or stream has
* been reached.
* @exception IOException If an I/O error occurs
*/
synchronized int more() throws IOException {
return more(oneCharacter) > 0 ? oneCharacter[0] : -1;
}
/**
* Gets more input characters.
*
* @param chars destination buffer.
* @return The number of characters.
* @exception IOException If an I/O error occurs
*/
synchronized int more(char[] chars) throws IOException {
int bufferLength = buffer.length();
int resultLength = 0;
for (; resultLength < chars.length; nextIndex++) {
if (bufferLength <= nextIndex) {
int ch = reader.read();
if (ch < 0) {
break;
}
buffer.append((char)ch);
++bufferLength;
}
char ch = buffer.charAt(nextIndex);
if (Character.isWhitespace(ch)) {
if (!isSpaceEatMode()) {
break;
}
} else {
chars[resultLength++] = ch;
}
}
return resultLength;
}
/**
* Consumes the specified length of input characters.
*
* @param length the length of characters to consume.
*/
synchronized void consume(int length) {
if (isSpaceEatMode()) {
int start = 0;
int end = length;
for (int index = 1; index < end; index++) {
char ch = buffer.charAt(index);
if (Character.isWhitespace(ch)) {
buffer.setCharAt(start++, ch);
end++;
}
}
buffer.delete(start, end);
} else {
buffer.delete(0, length);
}
nextIndex = 0;
}
/**
* Creates a Writer object that supplies inputs for this object.
*/
Writer createConnectedWriter() {
reader = new NullReader();
return new ConnectedWriter();
}
/**
* Nothing can be read from object of this class.
*/
private static class NullReader extends Reader {
public int read(char cbuf[], int off, int len) {
return -1;
}
public void close() {
}
}
/**
* Writer that supplis inputs to the KanjiInput object.
*/
private class ConnectedWriter extends Writer {
public void write(char cbuf[], int off, int len) {
synchronized (KanjiInput.this) {
buffer.append(cbuf, off, len);
}
}
public void flush() {
}
public void close() {
}
}
}