package fr.aumgn.bukkitutils.gconf;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

import org.bukkit.plugin.Plugin;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

public class GConfLoader {

    private final Gson gson;
    private final Plugin plugin;

    public GConfLoader(Gson gson, Plugin plugin) {
        this.gson = gson;
        this.plugin = plugin;
    }

    private Charset charset() {
        if (Charset.isSupported("UTF-8")) {
            return Charset.forName("UTF-8");
        }
        return Charset.defaultCharset();
    }

    public <T> T loadOrCreate(String filename, Class<T> klass)
            throws GConfLoadException {

        try {
            File folder = plugin.getDataFolder();
            if (!folder.exists() && !folder.mkdirs()) {
                throw new GConfLoadException(
                        "Impossible de créer le dossier :" + folder.getPath());
            }

            File file = new File(plugin.getDataFolder(), filename);
            T config;
            if (file.createNewFile()) {
                config = klass.newInstance();
            } else {
                config = load(file, klass);
            }

            // This ensures user file is updated with newer fields. 
            write(file, config);

            return config;
        } catch (IOException exc) {
            throw new GConfLoadException(exc);
        } catch (InstantiationException exc) {
            throw new GConfLoadException(exc);
        } catch (IllegalAccessException exc) {
            throw new GConfLoadException(exc);
        }
    }

    private <T> T load(File file, Class<T> klass) throws IOException {
        InputStreamReader isr = new InputStreamReader(
                new FileInputStream(file), charset());
        JsonReader reader = new JsonReader(isr);

        try {
            return gson.fromJson(reader, klass);
        } finally {
            reader.close();
        }
    }

    private void write(File file, Object object) throws IOException {
        OutputStreamWriter osw = new OutputStreamWriter(
                new FileOutputStream(file), charset());
        BufferedWriter writer = new BufferedWriter(osw);

        try {
            writer.write(gson.toJson(object));
        } finally {
            writer.close();
        }
    }
}

@@ -777,31 +777,26 @@
    p
-ublic %3CT%3E T loadOrCreat
+rivate File getFil
 e(St
@@ -808,32 +808,16 @@
 filename
-, Class%3CT%3E klass
 )%0A      
@@ -850,35 +850,16 @@
 ption %7B%0A
-%0A        try %7B%0A    
         
@@ -900,28 +900,24 @@
 ();%0A        
-  
-  
 if (!folder.
@@ -952,36 +952,32 @@
 ) %7B%0A            
-    
 throw new GConfL
@@ -1010,20 +1010,16 @@
         
-   
- 
 %22Impossi
@@ -1078,38 +1078,25 @@
     
-    %7D%0A%0A            File file =
+%7D%0A%0A        return
  new
@@ -1136,16 +1136,184 @@
 ename);%0A
+    %7D%0A%0A    public %3CT%3E T loadOrCreate(String filename, Class%3CT%3E klass)%0A            throws GConfLoadException %7B%0A%0A        try %7B%0A            File file = getFile(filename);%0A
         
@@ -2261,32 +2261,274 @@
        %7D%0A    %7D%0A%0A
+    public void write(String filename, Object object) throws GConfLoadException %7B%0A        try %7B%0A            write(getFile(filename), object);%0A        %7D catch (IOException exc) %7B%0A            throw new GConfLoadException(exc);%0A        %7D%0A    %7D%0A%0A
     private void

Add GconfLoader#write(String, Object)