forked from j62/ctbrec
Handle BOM properly in config file
This commit is contained in:
parent
ef19e04751
commit
d56f2a1bc1
|
@ -3,7 +3,6 @@ package ctbrec;
|
|||
import static java.nio.file.StandardOpenOption.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
|
@ -21,8 +20,6 @@ import com.squareup.moshi.Moshi;
|
|||
|
||||
import ctbrec.io.ModelJsonAdapter;
|
||||
import ctbrec.sites.Site;
|
||||
import okio.Buffer;
|
||||
import okio.BufferedSource;
|
||||
|
||||
public class Config {
|
||||
|
||||
|
@ -54,13 +51,21 @@ public class Config {
|
|||
Moshi moshi = new Moshi.Builder()
|
||||
.add(Model.class, new ModelJsonAdapter(sites))
|
||||
.build();
|
||||
JsonAdapter<Settings> adapter = moshi.adapter(Settings.class);
|
||||
JsonAdapter<Settings> adapter = moshi.adapter(Settings.class).lenient();
|
||||
File configFile = new File(configDir, filename);
|
||||
LOG.debug("Loading config from {}", configFile.getAbsolutePath());
|
||||
if(configFile.exists()) {
|
||||
try(FileInputStream fin = new FileInputStream(configFile); Buffer buffer = new Buffer()) {
|
||||
BufferedSource source = buffer.readFrom(fin);
|
||||
settings = adapter.fromJson(source);
|
||||
try {
|
||||
byte[] fileContent = Files.readAllBytes(configFile.toPath());
|
||||
if (fileContent[0] == -17 && fileContent[1] == -69 && fileContent[2] == -65) {
|
||||
// found BOM (byte order mark)
|
||||
LOG.debug("Removing BOM from config file");
|
||||
fileContent[0] = ' ';
|
||||
fileContent[1] = ' ';
|
||||
fileContent[2] = ' ';
|
||||
}
|
||||
String json = new String(fileContent, "UTF-8").trim();
|
||||
settings = adapter.fromJson(json);
|
||||
settings.httpTimeout = Math.max(settings.httpTimeout, 10_000);
|
||||
if (settings.recordingsDir.endsWith("/")) {
|
||||
settings.recordingsDir = settings.recordingsDir.substring(0, settings.recordingsDir.length() - 1);
|
||||
|
|
Loading…
Reference in New Issue