CoverityFix CID:1350849,1350850 Dubious method used 00/20400/2
Nimish Kapoor [Tue, 31 Jul 2018 17:12:32 +0000 (22:12 +0530)]
Found a call to a method which will perform a byte to String (or String to
byte) conversion, and will assume that the default platform encoding is
suitable. This will cause the application behaviour to vary between platforms.
Use an alternative API and specify a charset name or Charset object explicitly.
https://stackoverflow.com/questions/696626/java-filereader-encoding-issue

Change-Id: I0092132f2ce12ae3269a5169d1ddcb00b1e07d0b

scilab/modules/helptools/src/java/org/scilab/modules/helptools/image/ImageConverter.java

index 6ed6b6c..6c0bc89 100644 (file)
@@ -25,6 +25,8 @@ import java.io.FileOutputStream;
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
 import java.nio.channels.FileChannel;
 import java.util.HashMap;
 import java.util.Map;
@@ -126,7 +128,7 @@ public final class ImageConverter {
         if (f.exists() && f.canRead()) {
             BufferedReader reader = null;
             try {
-                reader = new BufferedReader(new FileReader(f));
+                reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
                 String line = reader.readLine();
                 while (line != null) {
                     String[] arr = line.split("=");
@@ -168,7 +170,7 @@ public final class ImageConverter {
         if (f.canWrite()) {
             BufferedWriter writer = null;
             try {
-                writer = new BufferedWriter(new FileWriter(f));
+                writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "UTF-8"));
                 Map<String, String> tree = new TreeMap<String, String>(map);
                 for (Map.Entry<String, String> entry : tree.entrySet()) {
                     String s = entry.getKey() + "=" + entry.getValue();