forked from yegor256/quiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.java
85 lines (78 loc) · 3.14 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
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* This class is thread safe.
*/
public class Parser {
private final int READ_BUFFER_SIZE = 4096;
private final int WRITE_BUFFER_SIZE = 4096;
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private volatile File file;
public void setFile(File file) {
readWriteLock.writeLock().lock();
this.file = file;
readWriteLock.writeLock().unlock();
}
public File getFile() {
return file;
}
public String getContent() throws IOException, InterruptedException {
return read(false);
}
public String getContentWithoutUnicode() throws IOException, InterruptedException {
return read(true);
}
public String read(final boolean withoutUnicode) throws IOException, InterruptedException {
readWriteLock.readLock().lock();
try (BufferedReader reader = new BufferedReader(new FileReader((file)))) {
long fileLength = file.length();
if (fileLength > Integer.MAX_VALUE) {
throw new IOException("File is too big for String, " + fileLength + " bytes.");
} else if (fileLength == 0) {
return "";
}
StringBuilder sb = new StringBuilder((int) fileLength);
char[] readBuffer = new char[READ_BUFFER_SIZE];
int sbIndex = 0;
int read;
do {
read = reader.read(readBuffer);
if (read > 0) {
if (withoutUnicode) {
for (int i = 0; i < read; i++) {
char c = readBuffer[i];
if (c < 0x80) {
sb.insert(sbIndex++, c);
}
}
} else {
sb.append(readBuffer, 0, read);
}
}
if (Thread.currentThread().isInterrupted()) {
throw new InterruptedException("File was not read");
}
} while (read > 0);
return sb.toString();
} finally {
readWriteLock.readLock().unlock();
}
}
public void saveContent(String content) throws IOException, InterruptedException {
readWriteLock.writeLock().lock();
try (FileOutputStream o = new FileOutputStream(file)) {
byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8);
for (int i = 0; i < (contentBytes.length + WRITE_BUFFER_SIZE - 1) / WRITE_BUFFER_SIZE; i++) {
int offset = Math.min(contentBytes.length - i * WRITE_BUFFER_SIZE, WRITE_BUFFER_SIZE);
o.write(contentBytes, i * WRITE_BUFFER_SIZE, i * WRITE_BUFFER_SIZE + offset);
if (Thread.currentThread().isInterrupted()) {
throw new InterruptedException("File was not written");
}
}
} finally {
readWriteLock.writeLock().unlock();
}
}
}