Skip to content

Commit 7650c27

Browse files
committed
🐛 Fix UTF_8 Codeset references
1 parent f55a4ca commit 7650c27

File tree

4 files changed

+18
-23
lines changed

4 files changed

+18
-23
lines changed

src/main/java/dev/ebullient/convert/io/Tui.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.ebullient.convert.io;
22

33
import java.io.BufferedInputStream;
4+
import java.io.BufferedWriter;
45
import java.io.File;
56
import java.io.FileOutputStream;
67
import java.io.IOException;
@@ -11,11 +12,12 @@
1112
import java.net.URL;
1213
import java.nio.channels.Channels;
1314
import java.nio.channels.ReadableByteChannel;
14-
import java.nio.charset.Charset;
15+
import java.nio.charset.StandardCharsets;
1516
import java.nio.file.Files;
1617
import java.nio.file.Path;
1718
import java.nio.file.Paths;
1819
import java.nio.file.StandardCopyOption;
20+
import java.nio.file.StandardOpenOption;
1921
import java.util.Collection;
2022
import java.util.Iterator;
2123
import java.util.List;
@@ -83,7 +85,7 @@ public static Tui instance() {
8385
};
8486

8587
public final static PrintWriter streamToWriter(PrintStream stream) {
86-
return new PrintWriter(stream, true, Charset.forName("UTF-8"));
88+
return new PrintWriter(stream, true, StandardCharsets.UTF_8);
8789
}
8890

8991
public final static ObjectMapper MAPPER = initMapper(JsonMapper.builder()
@@ -229,7 +231,10 @@ public void init(CommandSpec spec, boolean debug, boolean verbose, boolean log)
229231
if (log) {
230232
Path p = Path.of("ttrpg-convert.out.txt");
231233
try {
232-
this.log = new PrintWriter(Files.newOutputStream(p));
234+
BufferedWriter writer = Files.newBufferedWriter(p, StandardCharsets.UTF_8,
235+
StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
236+
this.log = new PrintWriter(writer, true);
237+
233238
VersionProvider vp = new VersionProvider();
234239
List.of(vp.getVersion()).forEach(this.log::println);
235240
} catch (IOException e) {

src/main/java/dev/ebullient/convert/qute/ImageRef.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package dev.ebullient.convert.qute;
22

33
import java.io.IOException;
4-
import java.io.UnsupportedEncodingException;
54
import java.net.URL;
65
import java.nio.charset.StandardCharsets;
76
import java.nio.file.Path;
@@ -215,12 +214,7 @@ public ImageRef build() {
215214
sourceUrl = imageRoot.getFallbackPath(sourceUrl)
216215
.replace('\\', '/');
217216

218-
try {
219-
// Remove escaped characters here (inconsistent escaping in the source URL)
220-
sourceUrl = java.net.URLDecoder.decode(sourceUrl, StandardCharsets.UTF_8.name());
221-
} catch (UnsupportedEncodingException e) {
222-
Tui.instance().errorf("Error decoding image URL %s: %s", sourceUrl, e.getMessage());
223-
}
217+
sourceUrl = java.net.URLDecoder.decode(sourceUrl, StandardCharsets.UTF_8);
224218

225219
boolean copyToVault = false;
226220

@@ -277,7 +271,7 @@ public static String escapeUrlImagePath(String url) {
277271
StringBuilder encodedPath = new StringBuilder();
278272
for (char ch : path.toCharArray()) {
279273
if (allowedCharacters.indexOf(ch) == -1) {
280-
byte[] bytes = String.valueOf(ch).getBytes("UTF-8");
274+
byte[] bytes = String.valueOf(ch).getBytes(StandardCharsets.UTF_8);
281275
for (byte b : bytes) {
282276
encodedPath.append(String.format("%%%02X", b));
283277
}
@@ -294,14 +288,9 @@ public static String escapeUrlImagePath(String url) {
294288
}
295289

296290
public static final String fixUrl(String sourceUrl) {
297-
try {
298-
// Remove escaped characters here (inconsistent escaping in the source URL)
299-
sourceUrl = java.net.URLDecoder.decode(sourceUrl, StandardCharsets.UTF_8.name());
300-
return escapeUrlImagePath(sourceUrl);
301-
} catch (UnsupportedEncodingException e) {
302-
Tui.instance().errorf("Error fixing URL %s: %s", sourceUrl, e.getMessage());
303-
return sourceUrl;
304-
}
291+
// Remove escaped characters here (inconsistent escaping in the source URL)
292+
sourceUrl = java.net.URLDecoder.decode(sourceUrl, StandardCharsets.UTF_8);
293+
return escapeUrlImagePath(sourceUrl);
305294
}
306295
}
307296
}

src/test/java/dev/ebullient/convert/Pf2eDataConvertTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import static org.assertj.core.api.Assertions.assertThat;
44

55
import java.io.IOException;
6-
import java.nio.charset.Charset;
6+
import java.nio.charset.StandardCharsets;
77
import java.nio.file.Files;
88
import java.nio.file.Path;
99
import java.nio.file.StandardCopyOption;
@@ -59,7 +59,7 @@ public void clear() throws IOException {
5959
Path filePath = testOutput.resolve(logFile);
6060
Files.move(logFile, filePath, StandardCopyOption.REPLACE_EXISTING);
6161

62-
String content = Files.readString(filePath, Charset.forName("UTF-8"));
62+
String content = Files.readString(filePath, StandardCharsets.UTF_8);
6363
if (content.contains("Exception")) {
6464
tui.errorf("Exception found in %s", filePath);
6565
}

src/test/java/dev/ebullient/convert/Tools5eDataConvertTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import static org.assertj.core.api.Assertions.assertThat;
44

55
import java.io.IOException;
6-
import java.nio.charset.Charset;
6+
import java.nio.charset.StandardCharsets;
77
import java.nio.file.Files;
88
import java.nio.file.Path;
99
import java.nio.file.StandardCopyOption;
@@ -66,10 +66,11 @@ public void clear() throws IOException {
6666

6767
Path logFile = Path.of("ttrpg-convert.out.txt");
6868
if (Files.exists(logFile)) {
69+
String content = Files.readString(logFile, StandardCharsets.UTF_8);
70+
6971
Path filePath = testOutput.resolve(logFile);
7072
Files.move(logFile, filePath, StandardCopyOption.REPLACE_EXISTING);
7173

72-
String content = Files.readString(filePath, Charset.forName("UTF-8"));
7374
if (content.contains("Exception")) {
7475
tui.errorf("Exception found in %s", filePath);
7576
}

0 commit comments

Comments
 (0)