Skip to content

Commit

Permalink
Explicitly use UTF-8 in IOUtils (#2935)
Browse files Browse the repository at this point in the history
Fixes #2933

Co-authored-by: Tobias Baum <tobias.baum@set.de>
  • Loading branch information
tobiasbaum and Tobias Baum committed Mar 9, 2023
1 parent 9473470 commit fc136e4
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/main/java/org/mockito/internal/util/io/IOUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -24,12 +25,12 @@
public final class IOUtil {

/**
* Writes text to file
* Writes text to file in UTF-8.
*/
public static void writeText(String text, File output) {
PrintWriter pw = null;
OutputStreamWriter pw = null;
try {
pw = new PrintWriter(new FileWriter(output));
pw = new OutputStreamWriter(new FileOutputStream(output), StandardCharsets.UTF_8);
pw.write(text);
} catch (Exception e) {
throw new MockitoException("Problems writing text to file: " + output, e);
Expand All @@ -38,9 +39,12 @@ public static void writeText(String text, File output) {
}
}

/**
* Reads all lines from the given stream. Uses UTF-8.
*/
public static Collection<String> readLines(InputStream is) {
List<String> out = new LinkedList<>();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
BufferedReader r = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
String line;
try {
while ((line = r.readLine()) != null) {
Expand Down

0 comments on commit fc136e4

Please sign in to comment.