-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReaderSource.java
49 lines (41 loc) · 1.26 KB
/
ReaderSource.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
/* Skeleton code copyright (C) 2008, 2022 Paul N. Hilfinger and the
* Regents of the University of California. Do not distribute this or any
* derivative work without permission. */
package ataxx;
import java.io.BufferedReader;
import java.io.Reader;
import java.io.IOException;
/** Provides command input from a Reader.
* @author P. N. Hilfinger
*/
class ReaderSource implements CommandSource {
/** A new source that reads from INPUT and prints prompts
* if SHOULDPROMPT. */
ReaderSource(Reader input, boolean shouldPrompt) {
_input = new BufferedReader(input);
_shouldPrompt = shouldPrompt;
}
@Override
public String getCommand(String prompt) {
if (_input == null) {
return null;
}
try {
if (_shouldPrompt) {
System.out.print(prompt);
System.out.flush();
}
String result = _input.readLine();
if (result == null) {
_input.close();
}
return result;
} catch (IOException excp) {
return null;
}
}
/** Input source. */
private BufferedReader _input;
/** True if we request a prompt for each getLine. */
private boolean _shouldPrompt;
}