Skip to content

Commit

Permalink
🐛 Fix UTF_8 Codeset references
Browse files Browse the repository at this point in the history
  • Loading branch information
ebullient committed Dec 10, 2024
1 parent f55a4ca commit 7650c27
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
11 changes: 8 additions & 3 deletions src/main/java/dev/ebullient/convert/io/Tui.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.ebullient.convert.io;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -11,11 +12,12 @@
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -83,7 +85,7 @@ public static Tui instance() {
};

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

public final static ObjectMapper MAPPER = initMapper(JsonMapper.builder()
Expand Down Expand Up @@ -229,7 +231,10 @@ public void init(CommandSpec spec, boolean debug, boolean verbose, boolean log)
if (log) {
Path p = Path.of("ttrpg-convert.out.txt");
try {
this.log = new PrintWriter(Files.newOutputStream(p));
BufferedWriter writer = Files.newBufferedWriter(p, StandardCharsets.UTF_8,
StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
this.log = new PrintWriter(writer, true);

VersionProvider vp = new VersionProvider();
List.of(vp.getVersion()).forEach(this.log::println);
} catch (IOException e) {
Expand Down
21 changes: 5 additions & 16 deletions src/main/java/dev/ebullient/convert/qute/ImageRef.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.ebullient.convert.qute;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
Expand Down Expand Up @@ -215,12 +214,7 @@ public ImageRef build() {
sourceUrl = imageRoot.getFallbackPath(sourceUrl)
.replace('\\', '/');

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

boolean copyToVault = false;

Expand Down Expand Up @@ -277,7 +271,7 @@ public static String escapeUrlImagePath(String url) {
StringBuilder encodedPath = new StringBuilder();
for (char ch : path.toCharArray()) {
if (allowedCharacters.indexOf(ch) == -1) {
byte[] bytes = String.valueOf(ch).getBytes("UTF-8");
byte[] bytes = String.valueOf(ch).getBytes(StandardCharsets.UTF_8);
for (byte b : bytes) {
encodedPath.append(String.format("%%%02X", b));
}
Expand All @@ -294,14 +288,9 @@ public static String escapeUrlImagePath(String url) {
}

public static final String fixUrl(String sourceUrl) {
try {
// Remove escaped characters here (inconsistent escaping in the source URL)
sourceUrl = java.net.URLDecoder.decode(sourceUrl, StandardCharsets.UTF_8.name());
return escapeUrlImagePath(sourceUrl);
} catch (UnsupportedEncodingException e) {
Tui.instance().errorf("Error fixing URL %s: %s", sourceUrl, e.getMessage());
return sourceUrl;
}
// Remove escaped characters here (inconsistent escaping in the source URL)
sourceUrl = java.net.URLDecoder.decode(sourceUrl, StandardCharsets.UTF_8);
return escapeUrlImagePath(sourceUrl);
}
}
}
4 changes: 2 additions & 2 deletions src/test/java/dev/ebullient/convert/Pf2eDataConvertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
Expand Down Expand Up @@ -59,7 +59,7 @@ public void clear() throws IOException {
Path filePath = testOutput.resolve(logFile);
Files.move(logFile, filePath, StandardCopyOption.REPLACE_EXISTING);

String content = Files.readString(filePath, Charset.forName("UTF-8"));
String content = Files.readString(filePath, StandardCharsets.UTF_8);
if (content.contains("Exception")) {
tui.errorf("Exception found in %s", filePath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
Expand Down Expand Up @@ -66,10 +66,11 @@ public void clear() throws IOException {

Path logFile = Path.of("ttrpg-convert.out.txt");
if (Files.exists(logFile)) {
String content = Files.readString(logFile, StandardCharsets.UTF_8);

Path filePath = testOutput.resolve(logFile);
Files.move(logFile, filePath, StandardCopyOption.REPLACE_EXISTING);

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

0 comments on commit 7650c27

Please sign in to comment.