-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractSystemInputReader.java
33 lines (31 loc) · 1.02 KB
/
AbstractSystemInputReader.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
import java.io.IOException;
/**
* Handles system input stream reading.
*/
public abstract class AbstractSystemInputReader {
/**
* Reads system input stream line by line. All characters are converted to lower case and each
* line is passed for processing to {@link #processLine(String)} method.
*
* @throws IOException if an I/O error occurs
*/
public void readSystemInput() throws IOException {
StringBuilder line = new StringBuilder();
int c;
while ((c = System.in.read()) >= 0) {
if (c == '\r' || c == '\n') {
processLine(line.toString().toLowerCase().trim());
line.setLength(0);
} else {
line = line.append((char)c);
}
}
}
/**
* Process a line read out by {@link #readSystemInput()} method in a way defined by subclass
* implementation.
*
* @param line single, trimmed line of system input
*/
public abstract void processLine(String line);
}