diff options
author | KHobbits <rob@khobbits.co.uk> | 2012-03-23 10:18:16 +0000 |
---|---|---|
committer | KHobbits <rob@khobbits.co.uk> | 2012-03-23 10:18:16 +0000 |
commit | 394155dfcba70a50fb29e58907b9068924067f58 (patch) | |
tree | 5a6b344598e666f9b5decb5534cdf154f06c89fb | |
parent | 6dbb2d405ec485f0c0f336c534194a66ecf82ecd (diff) | |
parent | 089da4b031569819bed4e07c8c23ef637abc1cf4 (diff) | |
download | Essentials-394155dfcba70a50fb29e58907b9068924067f58.tar Essentials-394155dfcba70a50fb29e58907b9068924067f58.tar.gz Essentials-394155dfcba70a50fb29e58907b9068924067f58.tar.lz Essentials-394155dfcba70a50fb29e58907b9068924067f58.tar.xz Essentials-394155dfcba70a50fb29e58907b9068924067f58.zip |
Merge branch 'master' of github.com:essentials/Essentials
-rw-r--r-- | Essentials/src/com/earth2me/essentials/EssentialsConf.java | 97 |
1 files changed, 82 insertions, 15 deletions
diff --git a/Essentials/src/com/earth2me/essentials/EssentialsConf.java b/Essentials/src/com/earth2me/essentials/EssentialsConf.java index 9ffe7e5b6..846a7b4b0 100644 --- a/Essentials/src/com/earth2me/essentials/EssentialsConf.java +++ b/Essentials/src/com/earth2me/essentials/EssentialsConf.java @@ -1,7 +1,14 @@ package com.earth2me.essentials; import static com.earth2me.essentials.I18n._; +import com.google.common.io.Files; import java.io.*; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.channels.FileChannel; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CoderResult; import java.util.HashMap; import java.util.Locale; import java.util.Map; @@ -24,6 +31,7 @@ public class EssentialsConf extends YamlConfiguration private transient File configFile; private transient String templateName = null; private transient Class<?> resourceClass = EssentialsConf.class; + private static final Charset UTF8 = Charset.forName("UTF-8"); public EssentialsConf(final File configFile) { @@ -104,15 +112,46 @@ public class EssentialsConf extends YamlConfiguration try { - super.load(configFile); - } - catch (FileNotFoundException ex) - { - LOGGER.log(Level.SEVERE, null, ex); + final FileInputStream inputStream = new FileInputStream(configFile); + try + { + final FileChannel channel = inputStream.getChannel(); + final ByteBuffer buffer = ByteBuffer.allocate((int)configFile.length()); + channel.read(buffer); + buffer.rewind(); + final CharBuffer data = CharBuffer.allocate((int)configFile.length()); + CharsetDecoder decoder = UTF8.newDecoder(); + CoderResult result = decoder.decode(buffer, data, true); + if (result.isError()) + { + buffer.rewind(); + data.clear(); + LOGGER.log(Level.INFO, "File {0} is not utf-8 encoded, trying {1}", new Object[] + { + configFile.getAbsolutePath().toString(), Charset.defaultCharset().displayName() + }); + decoder = Charset.defaultCharset().newDecoder(); + result = decoder.decode(buffer, data, true); + if (result.isError()) + { + throw new InvalidConfigurationException("Invalid Characters in file " + configFile.getAbsolutePath().toString()); + } else { + decoder.flush(data); + } + } else { + decoder.flush(data); + } + data.rewind(); + super.loadFromString(data.toString()); + } + finally + { + inputStream.close(); + } } catch (IOException ex) { - LOGGER.log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, ex.getMessage(), ex); } catch (InvalidConfigurationException ex) { @@ -301,27 +340,55 @@ public class EssentialsConf extends YamlConfiguration return def; } } - - public void save() { + + public void save() + { try { save(configFile); } catch (IOException ex) { - LOGGER.log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, ex.getMessage(), ex); } } - - public Object getProperty(String path) { + + @Override + public void save(final File file) throws IOException + { + if (file == null) + { + throw new IllegalArgumentException("File cannot be null"); + } + + Files.createParentDirs(file); + + final String data = saveToString(); + + final OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), UTF8); + + try + { + writer.write(data); + } + finally + { + writer.close(); + } + } + + public Object getProperty(String path) + { return get(path); } - - public void setProperty(String path, Object object) { + + public void setProperty(String path, Object object) + { set(path, object); } - - public void removeProperty(String path) { + + public void removeProperty(String path) + { set(path, null); } } |