package com.github.helltar.anpaside.ide;

import static com.github.helltar.anpaside.Consts.ASSET_DIR_FILES;
import static com.github.helltar.anpaside.Consts.ASSET_DIR_STUBS;
import static com.github.helltar.anpaside.Consts.DATA_PKG_PATH;

import android.content.res.AssetManager;

import com.github.helltar.anpaside.logging.Logger;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

public class IdeInit {

    private final AssetManager assetManager;

    public IdeInit(AssetManager assetManager) {
        this.assetManager = assetManager;
    }

    public boolean install() {
        if (copyAssets(ASSET_DIR_STUBS)) {
            return copyAssets(ASSET_DIR_FILES);
        }

        return false;
    }

    private boolean copyAssets(String assetDir) {
        try {
            String[] assets = assetManager.list(assetDir);

            if (assets.length > 0) {
                File dir = new File(DATA_PKG_PATH + assetDir);

                if (!dir.exists()) {
                    dir.mkdir();
                }

                for (String asset : assets) {
                    copyAssets(assetDir + "/" + asset);
                }
            } else {
                FileUtils.copyInputStreamToFile(assetManager.open(assetDir), new File(DATA_PKG_PATH + assetDir));
            }

            return true;

        } catch (IOException ioe) {
            Logger.addLog(ioe);
        }

        return false;
    }
}

@@ -337,34 +337,20 @@
 ort 
-org.apache.commons
+java
 .io.File
 Util
@@ -349,15 +349,9 @@
 File
-Utils;%0A
+;
 %0Aimp
@@ -358,32 +358,44 @@
 ort java.io.File
+OutputStream
 ;%0Aimport java.io
@@ -407,16 +407,73 @@
 ception;
+%0Aimport java.io.InputStream;%0Aimport java.io.OutputStream;
 %0A%0Apublic
@@ -1270,22 +1270,8 @@
     
-FileUtils.copy
 Inpu
@@ -1281,15 +1281,14 @@
 ream
-ToFile(
+ in = 
 asse
@@ -1314,44 +1314,333 @@
 Dir)
-, new File(DATA_PKG_PATH + assetDir)
+;%0A                OutputStream out = new FileOutputStream(DATA_PKG_PATH + assetDir);%0A%0A                byte%5B%5D buf = new byte%5B1024%5D;%0A                int len;%0A%0A                while ((len = in.read(buf)) %3E 0) %7B%0A                    out.write(buf, 0, len);%0A                %7D%0A%0A                in.close();%0A                out.close(
 );%0A 

replace copyInputStreamToFile