Created
April 10, 2015 15:49
-
-
Save Cybermaxke/325d664a3b4892b124ea to your computer and use it in GitHub Desktop.
Utf-8 Yaml Configuration
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.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.OutputStreamWriter; | |
import java.io.Writer; | |
import org.apache.commons.lang.Validate; | |
import org.bukkit.configuration.InvalidConfigurationException; | |
import org.bukkit.configuration.file.YamlConfiguration; | |
import com.google.common.base.Charsets; | |
import com.google.common.io.Files; | |
public class Utf8YamlConfiguration extends YamlConfiguration { | |
@Override | |
public void save(File file) throws IOException { | |
Validate.notNull(file, "File cannot be null"); | |
// Create the parent dirs | |
Files.createParentDirs(file); | |
// Save the data as a string | |
String data = this.saveToString(); | |
// Write the data as utf8 | |
Writer writer = new OutputStreamWriter(new FileOutputStream(file), Charsets.UTF_8); | |
try { | |
writer.write(data); | |
} finally { | |
writer.close(); | |
} | |
} | |
@Override | |
public void load(File file) throws FileNotFoundException, IOException, InvalidConfigurationException { | |
Validate.notNull(file, "File cannot be null"); | |
// Load the content of the target file | |
this.load(new InputStreamReader(new FileInputStream(file), Charsets.UTF_8)); | |
} | |
@Override | |
@Deprecated | |
public void load(InputStream stream) throws IOException, InvalidConfigurationException { | |
Validate.notNull(stream, "Stream cannot be null"); | |
// Load the content of the target stream | |
this.load(new InputStreamReader(stream, Charsets.UTF_8)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment