Created
January 20, 2014 16:13
-
-
Save DrkMatr1984/8523072 to your computer and use it in GitHub Desktop.
UTF8 Configuration for yaml.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.BufferedReader; | |
| import java.io.File; | |
| import java.io.FileOutputStream; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.io.InputStreamReader; | |
| import java.io.OutputStreamWriter; | |
| import java.nio.charset.Charset; | |
| import org.apache.commons.lang.Validate; | |
| import org.bukkit.configuration.InvalidConfigurationException; | |
| import org.bukkit.configuration.file.YamlConfiguration; | |
| import com.google.common.io.Files; | |
| public class Utf8YamlConfiguration extends YamlConfiguration { | |
| public static Charset UTF8_CHARSET = Charset.forName("UTF-8"); | |
| @Override | |
| public void load(InputStream stream) throws IOException, InvalidConfigurationException { | |
| Validate.notNull(stream, "Stream cannot be null"); | |
| InputStreamReader reader = new InputStreamReader(stream, UTF8_CHARSET); | |
| StringBuilder builder = new StringBuilder(); | |
| BufferedReader input = new BufferedReader(reader); | |
| try { | |
| String line; | |
| while ((line = input.readLine()) != null) { | |
| builder.append(line); | |
| builder.append('\n'); | |
| } | |
| } finally { | |
| input.close(); | |
| } | |
| loadFromString(builder.toString()); | |
| } | |
| @Override | |
| public void save(File file) throws IOException { | |
| Validate.notNull(file, "File cannot be null"); | |
| Files.createParentDirs(file); | |
| String data = saveToString(); | |
| FileOutputStream stream = new FileOutputStream(file); | |
| OutputStreamWriter writer = new OutputStreamWriter(stream, UTF8_CHARSET); | |
| try { | |
| writer.write(data); | |
| } finally { | |
| writer.close(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment