package pl.dzielins42.dmtools.util;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import com.google.gson.Gson;

public class JsonUtils {

    public static String[] loadStringArrayFromFile(String jsonFilePath) {
        try {
            return loadStringArrayFromFile(ResourcesUtils.getResourceFile(jsonFilePath));
        } catch (URISyntaxException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String[] loadStringArrayFromFiles(String... jsonFilePaths) {
        if (jsonFilePaths == null || jsonFilePaths.length == 0) {
            throw new IllegalArgumentException();
        }

        List<String> list = new LinkedList<String>();
        String[] partial;
        for (int i = 0; i < jsonFilePaths.length; i++) {
            try {
                partial = loadStringArrayFromFile(ResourcesUtils.getResourceFile(jsonFilePaths[i]));
                if (partial != null) {
                    list.addAll(Arrays.asList(partial));
                }
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }

        String[] result = null;
        if (list != null && !list.isEmpty()) {
            result = new String[list.size()];
            for (int i = 0; i < list.size(); i++) {
                result[i] = list.get(i);
            }
        }

        return result;
    }

    public static String[] loadStringArrayFromFile(File jsonFile) {
        if (jsonFile == null || !jsonFile.exists()) {
            throw new IllegalArgumentException();
        }

        FileReader fr = null;
        try {
            Gson gson = new Gson();
            fr = new FileReader(jsonFile);
            return gson.fromJson(fr, String[].class);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }

    public static String[] loadStringArrayFromFiles(File... jsonFiles) {
        if (jsonFiles == null || jsonFiles.length == 0) {
            throw new IllegalArgumentException();
        }

        FileReader fr = null;
        Gson gson = new Gson();
        List<String> list = new LinkedList<String>();
        for (int i = 0; i < jsonFiles.length; i++) {
            try {
                fr = new FileReader(jsonFiles[i]);
                list.addAll(Arrays.asList(gson.fromJson(fr, String[].class)));
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fr != null) {
                    try {
                        fr.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        String[] result = null;
        if (list != null && !list.isEmpty()) {
            result = new String[list.size()];
            for (int i = 0; i < list.size(); i++) {
                result[i] = list.get(i);
            }
        }

        return result;
    }

}
@@ -70,22 +70,27 @@
 .io.File
-Reader
+InputStream
 ;%0Aimport
@@ -127,30 +127,120 @@
 ava.
-net.URISyntaxException
+io.InputStreamReader;%0Aimport java.io.Reader;%0Aimport java.net.URISyntaxException;%0Aimport java.nio.charset.Charset
 ;%0Aim
@@ -1808,36 +1808,31 @@
  %7D%0A%0A        
-File
 Reader 
-f
 r = null;%0A  
@@ -1887,33 +1887,32 @@
             
-f
 r = new 
 FileReader(j
@@ -1899,35 +1899,89 @@
 r = new 
-FileReader(jsonFile
+InputStreamReader(new FileInputStream(jsonFile), Charset.forName(%22UTF-8%22)
 );%0A     
@@ -2000,33 +2000,32 @@
 n gson.fromJson(
-f
 r, String%5B%5D.clas
@@ -2121,33 +2121,32 @@
             if (
-f
 r != null) %7B%0A   
@@ -2176,33 +2176,32 @@
                 
-f
 r.close();%0A     
@@ -2553,20 +2553,15 @@
     
-File
 Reader 
-f
 r = 
@@ -2743,17 +2743,16 @@
     
-f
 r = new 
 File
@@ -2751,31 +2751,85 @@
 new 
-FileReader(jsonFiles%5Bi%5D
+InputStreamReader(new FileInputStream(jsonFiles%5Bi%5D), Charset.forName(%22UTF-8%22)
 );%0A 
@@ -2883,17 +2883,16 @@
 romJson(
-f
 r, Strin
@@ -3022,17 +3022,16 @@
     if (
-f
 r != nul
@@ -3085,17 +3085,16 @@
         
-f
 r.close(

Use UTF-8 encoding for resource files.