-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONFilterReader.java
115 lines (102 loc) · 2.82 KB
/
JSONFilterReader.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
/* This code is released into the public domain, so use it, hack it and
package it as you wish. */
import java.io.Reader;
import java.io.FilterReader;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
public class JSONFilterReader extends FilterReader {
private static final int DEFAULT = 0, STRING = 1, STRING_ESCAPE = 2;
private int state = 0;
public JSONFilterReader(Reader inrd) {
super(inrd);
if (!in.markSupported()) {
throw new UnsupportedOperationException("JSONFilterReader cannot work with a Reader that doesn't support mark()");
}
}
public int read() throws IOException {
int c = in.read();
int d = 0, result = 0;
switch (state) {
case STRING_ESCAPE:
if (c == '"') {
state = 1;
}
result = c;
break;
case STRING:
if (c == '"') {
state = 0;
result = c;
} else if (c == '\\') {
state = 2;
} else {
result = c;
}
break;
default:
if (c == '"') {
state = 1;
result = c;
} else if (c == '/') {
in.mark(1);
d = in.read();
if (d == '*') {
// Hit a block comment, find the end of it and jump there.
while (true) {
c = in.read();
if (c == '*') {
in.mark(1);
d = in.read();
if (d == '/' || d == -1) {
break;
} else {
in.reset();
}
} else if (c == -1) {
break;
}
}
result = in.read();
} else if (d == '/') {
// Hit a line comment, do as above.
while (true) {
c = in.read();
if (c == '\n' || c == '\r' || c == -1) {
break;
}
}
result = c;
} else {
in.reset();
result = c;
}
} else {
result = c;
}
break;
}
return result;
}
public int read(char[] cbuf, int offset, int len) throws IOException {
int c, i;
for (i = 0; i < len; i++) {
c = read();
if (c == -1) {i = c; break;}
cbuf[offset + i] = (char) c;
}
return i;
}
public long skip() throws IOException {
throw new UnsupportedOperationException("this class does not support the skipping of characters");
}
public static void main(String[] args) throws IOException {
JSONFilterReader rdr = new JSONFilterReader(new BufferedReader(new InputStreamReader(System.in)));
int i;
while (true) {
i = rdr.read();
if (i == -1) {break;}
System.out.print((char) i);
}
}
}