-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathFastScanner.java
More file actions
56 lines (44 loc) · 1.34 KB
/
FastScanner.java
File metadata and controls
56 lines (44 loc) · 1.34 KB
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
package org.psjava.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.StringTokenizer;
public class FastScanner {
private StringTokenizer tokenizer;
public FastScanner(InputStream is) {
read(is, "\n\r\t ");
}
public FastScanner(InputStream is, String delimiters) {
read(is, delimiters);
}
private void read(InputStream is, String delimiters) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
while (true) {
int read = is.read(buf);
if (read == -1)
break;
bos.write(buf, 0, read);
}
tokenizer = new StringTokenizer(new String(bos.toByteArray()), delimiters);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public boolean hasNext() {
return tokenizer.hasMoreTokens();
}
public int nextInt() {
return Integer.parseInt(tokenizer.nextToken());
}
public long nextLong() {
return Long.parseLong(tokenizer.nextToken());
}
public double nextDouble() {
return Double.parseDouble(tokenizer.nextToken());
}
public String next() {
return tokenizer.nextToken();
}
}