package io.github.droidkaigi.confsched2017.view.helper;

import android.content.Context;
import android.support.annotation.StringRes;

import java.io.IOException;
import java.io.InputStream;

import javax.inject.Inject;
import javax.inject.Singleton;

import timber.log.Timber;

@Singleton
public class ResourceResolver {

    private static final String TAG = ResourceResolver.class.getSimpleName();

    private final Context context;

    @Inject
    public ResourceResolver(Context context) {
        this.context = context;
    }

    public String getString(@StringRes int resId) {
        return context.getString(resId);
    }

    public String getString(@StringRes int resId, Object... formatArgs) {
        return context.getString(resId, formatArgs);
    }

    public String loadJSONFromAsset(final String jsonFileName) {
        String json = null;
        InputStream is = null;
        try {
            is = context.getAssets().open("json/" + jsonFileName);
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            json = new String(buffer, "UTF-8");
        } catch (IOException e) {
            Timber.e(TAG, e.getMessage(), e);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                Timber.e(TAG, e.getMessage(), e);
            }
        }
        return json;
    }
}

@@ -50,16 +50,55 @@
 elper;%0A%0A
+import org.apache.commons.io.IOUtils;%0A%0A
 import a
@@ -1023,144 +1023,34 @@
     
-int size = is.available();%0A            byte%5B%5D buffer = new byte%5Bsize%5D;%0A            is.read(buffer);%0A            json = new 
+json = IOUtils.to
 String(
-buffer
+is
 , %22U
@@ -1114,258 +1114,125 @@
 ber.
-e
+tag
 (TAG
-, e.getMessage(), e);%0A        %7D finally %7B%0A            try %7B%0A                if (is != null) %7B%0A                    is.close();%0A                %7D%0A            %7D catch (IOException e) %7B%0A                Timber.e(TAG, e.getMessage(), e);%0A            %7D
+).e(e, %22assets/json/%25s: read failed%22, jsonFileName);%0A        %7D finally %7B%0A            IOUtils.closeQuietly(is);
 %0A   

Fix potential problems in ResourceResolver.loadJSONFromAsset()