Skip to content
This repository has been archived by the owner on Apr 3, 2022. It is now read-only.

Commit

Permalink
Changed to read/write configuration files using UTF-8. Also added att…
Browse files Browse the repository at this point in the history
…empts

to detect if existing files could not be correctly read using UTF-8 and
retry using old encoding/charset.
  • Loading branch information
Stekeblad committed Jun 11, 2020
1 parent 81ebe94 commit 4f1dace
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
27 changes: 17 additions & 10 deletions src/main/java/io/github/stekeblad/videouploader/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public class Main extends Application {

@Override
public void start(Stage primaryStage) {
try {
configManager = ConfigManager.INSTANCE;
configManager.configManager();
} catch (Exception e) {
e.printStackTrace();
AlertUtils.exceptionDialog("ERROR",
"Failed to load settings or other configurations file, unable to launch.", e);
}
try {
loadTranslations();
} catch (Exception e) {
Expand Down Expand Up @@ -90,16 +98,15 @@ else if (configManager.getChannelName() == null || configManager.getChannelName(
}

private void loadTranslations() throws Exception {
configManager = ConfigManager.INSTANCE;
configManager.configManager();
String localeString = configManager.getSelectedLanguage();
Locale locale;
if (localeString != null && !localeString.isEmpty()) {
locale = new Locale(localeString);
} else {
locale = Locale.getDefault();
}
TranslationsManager.loadAllTranslations(locale);

String localeString = configManager.getSelectedLanguage();
Locale locale;
if (localeString != null && !localeString.isEmpty()) {
locale = new Locale(localeString);
} else {
locale = Locale.getDefault();
}
TranslationsManager.loadAllTranslations(locale);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.*;
import java.util.*;
Expand Down Expand Up @@ -218,10 +219,29 @@ public static List<String> getContentOfResourceDir(String path) {
* @throws NullPointerException if path is null
*/
public static ArrayList<String> readAllLines(String path) throws IOException {
// Attempt to read file as UTF-8 (added in version 1.4.1)
ArrayList<String> result = null;
try {
result = innerReadAllLines(path, true);
} catch (IOException e) {
// But that may not work for old files that uses characters outside ASCII, so try again with old format
if (e.getMessage().equals("Bad character encoding!"))
result = innerReadAllLines(path, false);
}
return result;
}

private static ArrayList<String> innerReadAllLines(String path, boolean useUtf8) throws IOException {
BufferedReader reader = null;
ArrayList<String> lines = new ArrayList<>();
try {
reader = new BufferedReader(new FileReader(new File(path)));
if (useUtf8) {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(new File(path)), StandardCharsets.UTF_8));
} else {
reader = reader = new BufferedReader(new FileReader(new File(path)));
}

String line = reader.readLine();
while (line != null) { // while not end of file
lines.add(line);
Expand All @@ -236,6 +256,12 @@ public static ArrayList<String> readAllLines(String path) throws IOException {
}
}
}
if (useUtf8) {
for (String l : lines) {
if (l.contains("\ufffd"))
throw new IOException("Bad character encoding!");
}
}
return lines;
}

Expand All @@ -249,10 +275,29 @@ public static ArrayList<String> readAllLines(String path) throws IOException {
* @throws NullPointerException if path is null
*/
public static String readAll(String path) throws IOException {
// Attempt to read file as UTF-8 (added in version 1.4.1)
String result = null;
try {
result = innerReadAll(path, true);
} catch (IOException e) {
// But that may not work for old files that uses characters outside ASCII, so try again with old format
if (e.getMessage().equals("Bad character encoding!"))
result = innerReadAll(path, false);
}
return result;
}

private static String innerReadAll(String path, boolean useUtf8) throws IOException {
BufferedReader reader = null;
StringBuilder builder = new StringBuilder();
try {
reader = new BufferedReader(new FileReader(new File(path)));
if (useUtf8) {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(new File(path)), StandardCharsets.UTF_8));
} else {
reader = reader = new BufferedReader(new FileReader(new File(path)));
}

String line = reader.readLine();
while (line != null) { // while not end of file
builder.append(line);
Expand All @@ -270,6 +315,10 @@ public static String readAll(String path) throws IOException {
}
}
}

if (useUtf8 && builder.toString().contains("\ufffd"))
throw new IOException("Bad character encoding!");

return builder.toString();
}

Expand All @@ -285,7 +334,8 @@ public static String readAll(String path) throws IOException {
public static void writeAll(String path, String data) throws IOException {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(new File(path)));
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(new File(path)), StandardCharsets.UTF_8));
writer.write(data);
} finally {
if (writer != null) {
Expand Down

0 comments on commit 4f1dace

Please sign in to comment.