-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextInputStream.java
192 lines (179 loc) · 5.81 KB
/
TextInputStream.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
// Class TextInputStream provides methods for reading from an input
// stream. A TextInputStream can be constructed either from an InputStream
// such as System.in or by specifying the name of a file as a String.
//
// The public methods are summarized below.
// public TextInputStream(InputStream input)
// constructs a TextInputStream from the given InputStream
// public TextInputStream(StringReader reader)
// constructs a TextInputStream using the given StringReader
// public TextInputStream(String fileName)
// constructs a TextInputStream using the given file name
// public String readLine()
// reads one line of text and returns it as a String
// public char read()
// reads one character
// public void unread(char ch)
// puts the given character back into the input stream
// public char peek()
// returns next character in input stream without reading it
// public String readWord()
// reads next word from input stream
// public int readInt()
// reads next token from input stream and returns it as an int
// public double readDouble()
// reads next token from input stream and returns it as a double
// public boolean ready()
// returns true if stream is ready for reading; otherwise returns false
import java.io.*;
public class TextInputStream
{
private PushbackReader in; // the input stream
public TextInputStream(InputStream input)
// pre : input stream is open for reading
// post: constructs a TextInputStream from the given InputStream
{
in = new PushbackReader(new InputStreamReader(input));
}
public TextInputStream(StringReader reader)
// pre : input stream is open for reading
// post: constructs a TextInputStream from the given StringReader
{
in = new PushbackReader(reader);
}
public TextInputStream(String fileName)
// pre : fileName is the name of a file that can be opened for reading
// post: constructs a TextInputStream tied to the given file
{
try {
in = new PushbackReader(new FileReader(fileName));
} catch(Exception e) {
System.out.println("Can't open input file '" + fileName + "', exiting");
System.exit(1);
}
}
public String readLine()
// pre : not at end-of-file of the input stream
// post: reads the next line of input and returns it as a String
{
String result = "";
try {
for(;;) {
int next = in.read();
if (next == '\r') // skip carriage-return on Windows systems
continue;
if (next == -1 || next == '\n')
break;
result += (char)next;
}
} catch (Exception e) {
System.out.println("Failure in call on readLine method, exiting.");
System.exit(1);
}
return result;
}
public char read()
// pre : not at end-of-file of the input stream
// post: reads the next character of input and returns it
{
char result = ' ';
try {
result = (char)in.read();
if (result == '\r') // skip carriage-return on Windows systems
result = (char)in.read();
} catch (Exception e) {
System.out.println("Failure in call on read method, exiting.");
System.exit(1);
}
return result;
}
public void unread(char ch)
// post: puts the given character back into the input stream to be
// read again
{
try {
in.unread((byte)ch);
} catch (Exception e) {
System.out.println("Failure in call on unread method, exiting");
System.exit(1);
}
}
public char peek()
// post: returns the next character in the input stream without
// actually reading it
{
int next = 0;
try {
next = in.read();
} catch (Exception e) {
System.out.println("Failure in call on peek method, exiting");
System.exit(1);
}
if (next != -1)
unread((char)next);
return (char)next;
}
public String readWord()
// pre : stream contains at least one nonwhitespace character
// post: skips leading whitespace, reads one word (terminated by
// end-of-file or whitespace)
{
String result = "";
try {
int next;
do
next = in.read();
while (next != -1 && Character.isWhitespace((char)next));
while (next != -1 && !Character.isWhitespace((char)next)) {
result += (char)next;
next = in.read();
}
while (next != -1 && next != '\n' && Character.isWhitespace((char)next))
next = in.read();
if (next != -1 && next != '\n')
unread((char)next);
} catch (Exception e) {
System.out.println("Failure in call on readWord method, exiting.");
System.exit(1);
}
return result;
}
public int readInt()
// pre : next token in input stream is an int
// post: reads int and skips any trailing whitespace on current line
{
int result = 0;
try {
result = Integer.parseInt(readWord());
} catch (Exception e) {
System.out.println("Failure in call on readInt method, returning 0");
}
return result;
}
public double readDouble()
// pre : next token in input stream is a double
// post: reads double and skips any trailing whitespace on current line
{
double result = 0.0;
try {
// Double constructor : deprecated in java11 or later
// result = new Double(readWord()).doubleValue();
result = Double.parseDouble(readWord());
} catch (Exception e) {
System.out.println("Failure in call on readDouble method, returning 0");
}
return result;
}
public boolean ready()
// post: returns true if input stream is ready for reading;
// otherwise returns false
{
boolean result = false;
try {
result = in.ready();
} catch (IOException e) {
System.out.println("Failure in call on ready method, returning false.");
}
return result;
}
}