mirror of
https://github.com/songoda/SongodaCore.git
synced 2024-11-23 18:45:34 +01:00
use UTF-8 unless given reason otherwise
This commit is contained in:
parent
330ddcfedd
commit
445b47c581
@ -69,6 +69,7 @@ public class Config extends ConfigSection {
|
|||||||
final DumperOptions yamlOptions = new DumperOptions();
|
final DumperOptions yamlOptions = new DumperOptions();
|
||||||
final Representer yamlRepresenter = new YamlRepresenter();
|
final Representer yamlRepresenter = new YamlRepresenter();
|
||||||
final Yaml yaml = new Yaml(new YamlConstructor(), yamlRepresenter, yamlOptions);
|
final Yaml yaml = new Yaml(new YamlConstructor(), yamlRepresenter, yamlOptions);
|
||||||
|
Charset defaultCharset = StandardCharsets.UTF_8;
|
||||||
SaveTask saveTask;
|
SaveTask saveTask;
|
||||||
Timer autosaveTimer;
|
Timer autosaveTimer;
|
||||||
////////////// Config settings ////////////////
|
////////////// Config settings ////////////////
|
||||||
@ -149,6 +150,21 @@ public class Config extends ConfigSection {
|
|||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Charset getDefaultCharset() {
|
||||||
|
return defaultCharset;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the Charset that will be used to save this config
|
||||||
|
*
|
||||||
|
* @param defaultCharset Charset to use
|
||||||
|
* @return this class
|
||||||
|
*/
|
||||||
|
public Config setDefaultCharset(Charset defaultCharset) {
|
||||||
|
this.defaultCharset = defaultCharset;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean getAutosave() {
|
public boolean getAutosave() {
|
||||||
return autosave;
|
return autosave;
|
||||||
}
|
}
|
||||||
@ -352,6 +368,10 @@ public class Config extends ConfigSection {
|
|||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
try (BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file))) {
|
try (BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file))) {
|
||||||
Charset charset = TextUtils.detectCharset(stream, StandardCharsets.UTF_8);
|
Charset charset = TextUtils.detectCharset(stream, StandardCharsets.UTF_8);
|
||||||
|
// upgrade charset if file was saved in a more complex format
|
||||||
|
if(charset == StandardCharsets.UTF_16BE || charset == StandardCharsets.UTF_16LE) {
|
||||||
|
defaultCharset = charset;
|
||||||
|
}
|
||||||
this.load(new InputStreamReader(stream, charset));
|
this.load(new InputStreamReader(stream, charset));
|
||||||
return true;
|
return true;
|
||||||
} catch (IOException | InvalidConfigurationException ex) {
|
} catch (IOException | InvalidConfigurationException ex) {
|
||||||
@ -486,7 +506,7 @@ public class Config extends ConfigSection {
|
|||||||
file.getParentFile().mkdirs();
|
file.getParentFile().mkdirs();
|
||||||
}
|
}
|
||||||
String data = this.saveToString();
|
String data = this.saveToString();
|
||||||
try (OutputStreamWriter writer = new OutputStreamWriter((OutputStream) new FileOutputStream(file), StandardCharsets.UTF_16);) {
|
try (OutputStreamWriter writer = new OutputStreamWriter((OutputStream) new FileOutputStream(file), defaultCharset);) {
|
||||||
writer.write(data);
|
writer.write(data);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
Reference in New Issue
Block a user