forked from yegor256/quiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.java
105 lines (97 loc) · 3.23 KB
/
Parser.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
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
/**
* This class is thread safe.
*/
final public class Parser {
/**
* This is our internal File instance. Can be set only once.
*/
private final File file;
/**
* Creates a Parser instance.
* @param file The file to process. Nulls not supported.
* @throws NullPointerException If the file argument is null.
*/
public Parser(File file) {
if(file==null){
throw new NullPointerException();
}
this.file = file;
}
/**
* @return The internal File instance.
*/
public File getFile() {
return file;
}
/**
* Read the contents of the file and return it as a String. No filtering.
* @return A string with the file contents.
* @throws IOException In case the IO operation went wrong.
*/
synchronized public String getContent()
throws IOException {
return getContentInternal(false);
}
/**
* Read the contents of the file and return it as a String. Only bytes under
* 0x80 are added to the resulting string; all other bytes are ignored.
* @return A string with the file contents. Only characters representing bytes under
* 0x80 in the resulting string.
* @throws IOException In case the IO operation went wrong.
*/
synchronized public String getContentWithoutUnicode()
throws IOException {
return getContentInternal(true);
}
/**
* Read the contents of the file and return it as a String. When
* shouldSkipUnicode set to true only characters representing bytes under
* 0x80 are returned. Otherwise - all characters returned.
* @param shouldSkipUnicode "true" means only characters under 0x80 should
* be returned.
* @return A string with the file contents. Filtering depends on the
* shouldSkipUnicode parameter.
* @throws IOException In case the IO operation went wrong.
*/
// TODO Find out what is the REAL PURPOSE of this method. We are reading
// bytes and converting them to characters manually! Looks strange.
// TODO Avoid appending characters to String. Use StringBuilder.
// TODO Use try/finally and close the FileInputStream "i" in the "finally"
// block
// TODO change 0x80 to a constant
private String getContentInternal(boolean shouldSkipUnicode)
throws IOException {
FileInputStream i = new FileInputStream(file);
String output = "";
int data;
while ((data = i.read()) > 0) {
if (shouldSkipUnicode) {
if (data < 0x80) {
output += (char) data;
}
}
else {
output += (char) data;
}
}
return output;
}
/**
* Writes the content argument into the file.
* @param content A string to write into file.
* @throws IOException In case the IO operation went wrong.
*/
synchronized public void saveContent(String content)
throws IOException {
PrintWriter out = new PrintWriter(file);
try {
out.print(content);
} finally {
out.close();
}
}
}