-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFastScanner.java
More file actions
executable file
·91 lines (82 loc) · 1.69 KB
/
FastScanner.java
File metadata and controls
executable file
·91 lines (82 loc) · 1.69 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
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
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
public class FastScanner {
protected byte[] buffer = new byte[1024];
protected int pos = 0;
protected int max = 0;
protected boolean open = true;
protected BufferedInputStream b = null;
public boolean hasMore()
{
return (open && max > 0);
}
public FastScanner(String file)
{
try {
if(!file.endsWith(".gz"))
{
b = new BufferedInputStream(new FileInputStream(new File(file)),8096);
max = b.read(buffer);
}
else
{
b = new BufferedInputStream(new GZIPInputStream(new FileInputStream(new File(file))),8096);
max = b.read(buffer);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getLine()
{
if(!open) return null;
StringBuilder sb = new StringBuilder(1024);
while(true)
{
while(pos < max)
{
char c = (char) buffer[pos++];
if (c == '\n')
{
String line = sb.toString();
sb = new StringBuilder(1024);
return line;
}
else if(c != '\r')
{
sb.append(c);
}
}
try {
max = b.read(buffer);
pos = 0; //rewind
if(max == -1)
{
open = false;
close();
return sb.toString();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void close()
{
try {
b.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}