Skip to content

Commit eec5361

Browse files
Merge pull request #209 from refactorfirst/Replace-Charset.defaultCharset()-with-StandardCharsets.UTF_8
Replace Charset.defaultCharset() with StandardCharsets.UTF_8
2 parents 1bfb84b + debc03e commit eec5361

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

‎report/src/main/java/org/hjug/refactorfirst/report/ReportWriter.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.io.OutputStreamWriter;
1111
import java.nio.channels.Channels;
1212
import java.nio.channels.SeekableByteChannel;
13-
import java.nio.charset.Charset;
13+
import java.nio.charset.StandardCharsets;
1414
import java.nio.file.DirectoryStream;
1515
import java.nio.file.Files;
1616
import java.nio.file.NoSuchFileException;
@@ -162,7 +162,7 @@ private void writeAtomicallySecure(SecureDirectoryStream<Path> directory, Path r
162162
try {
163163
try (SeekableByteChannel channel = directory.newByteChannel(temporaryName, options);
164164
BufferedWriter writer = new BufferedWriter(
165-
new OutputStreamWriter(Channels.newOutputStream(channel), Charset.defaultCharset()))) {
165+
new OutputStreamWriter(Channels.newOutputStream(channel), StandardCharsets.UTF_8))) {
166166
writer.write(content);
167167
}
168168
directory.move(temporaryName, directory, reportName);
@@ -218,7 +218,7 @@ public void writeAtomically(Path reportName, String content) throws IOException
218218
try {
219219
try (SeekableByteChannel channel = Files.newByteChannel(temporaryName, options);
220220
BufferedWriter writer = new BufferedWriter(
221-
new OutputStreamWriter(Channels.newOutputStream(channel), Charset.defaultCharset()))) {
221+
new OutputStreamWriter(Channels.newOutputStream(channel), StandardCharsets.UTF_8))) {
222222
writer.write(content);
223223
}
224224
// Verify temp file is not a symlink before move

‎report/src/test/java/org/hjug/refactorfirst/report/ReportWriterTest.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.io.File;
77
import java.io.IOException;
8+
import java.nio.charset.StandardCharsets;
89
import java.nio.file.Files;
910
import java.nio.file.Path;
1011
import java.nio.file.StandardWatchEventKinds;
@@ -279,4 +280,20 @@ void containReportDirectory_rejectsSymlinkedAncestor(@TempDir Path tempDir) thro
279280
IllegalArgumentException.class,
280281
() -> ReportWriter.containReportDirectory(tempDir.toFile(), "linked/site"));
281282
}
283+
284+
@Test
285+
void writeReportToDisk_emitsUtf8ForNonAsciiContent(@TempDir Path tempDir) throws IOException {
286+
String nonAsciiContent = "café 漢字 😀";
287+
Path outputDir = tempDir.resolve("output");
288+
Files.createDirectories(outputDir);
289+
290+
ReportWriter.writeReportToDisk(outputDir.toString(), "test.html", nonAsciiContent);
291+
292+
Path outputFile = outputDir.resolve("test.html");
293+
assertTrue(Files.exists(outputFile));
294+
295+
byte[] writtenBytes = Files.readAllBytes(outputFile);
296+
byte[] expectedBytes = nonAsciiContent.getBytes(StandardCharsets.UTF_8);
297+
assertArrayEquals(expectedBytes, writtenBytes, "ReportWriter must emit UTF-8 bytes");
298+
}
282299
}

0 commit comments

Comments
 (0)