-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
AtomicFileWriter.java
53 lines (45 loc) · 1.81 KB
/
AtomicFileWriter.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
package org.jabref.logic.exporter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
/**
* Writer that similar to the built-in {@link java.io.FileWriter} but uses the {@link AtomicFileOutputStream} as the
* underlying output stream. In this way, we make sure that the errors during the write process do not destroy the
* contents of the target file.
* Moreover, this writer checks if the chosen encoding supports all text that is written. Characters whose encoding
* was problematic can be retrieved by {@link #getEncodingProblems()}.
*/
public class AtomicFileWriter extends OutputStreamWriter {
private final CharsetEncoder encoder;
private final Set<Character> problemCharacters = new TreeSet<>();
public AtomicFileWriter(Path file, Charset encoding) throws IOException {
this(file, encoding, false);
}
public AtomicFileWriter(Path file, Charset encoding, boolean keepBackup) throws IOException {
super(new AtomicFileOutputStream(file, keepBackup), encoding);
encoder = encoding.newEncoder();
}
@Override
public void write(String str) throws IOException {
super.write(str);
if (!encoder.canEncode(str)) {
for (int i = 0; i < str.length(); i++) {
char character = str.charAt(i);
if (!encoder.canEncode(character)) {
problemCharacters.add(character);
}
}
}
}
public boolean hasEncodingProblems() {
return !problemCharacters.isEmpty();
}
public Set<Character> getEncodingProblems() {
return Collections.unmodifiableSet(problemCharacters);
}
}