Skip to content

Instantly share code, notes, and snippets.

@Cybermaxke
Created April 10, 2015 15:49
Show Gist options
  • Save Cybermaxke/325d664a3b4892b124ea to your computer and use it in GitHub Desktop.
Save Cybermaxke/325d664a3b4892b124ea to your computer and use it in GitHub Desktop.
Utf-8 Yaml Configuration
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