package edu.cmu.pocketsphinx;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.content.Context;

import android.content.res.AssetManager;

import android.util.Log;


public class SphinxUtil {

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

    public static File syncAssets(Context context, String path) 
        throws IOException
    {
        File root = new File(context.getExternalFilesDir(null), path);
        copyAsset(context.getAssets(), path, root);

        return root;
    }

    private static void copyAsset(AssetManager assets, String src, File dst)
        throws IOException
    {
        String[] content = assets.list(src);

        if (content.length > 0) {
            for (String path : content) {
                String childAsset = new File(src, path).getPath();
                copyAsset(assets, childAsset, new File(dst, path));
            }
        } else {
            if (!dst.exists()) {
                dst.getParentFile().mkdirs();
                Log.d(TAG, "copy " + src + " to " + dst);
                copyStream(assets.open(src), new FileOutputStream(dst));
            } else {
                Log.d(TAG, "skip " + src + " - file exists");
            }
        }
    }

    /**
     * Copies raw asset resources to external storage of the device.
     *
     * Implementation is borrowed from Apache Commons.
     */
    private static void copyStream(InputStream source, OutputStream dest)
        throws IOException
    {
        byte[] buffer = new byte[1024];
        int nread;

        while ((nread = source.read(buffer)) != -1) {
            if (nread == 0) {
                nread = source.read();
                if (nread < 0)
                    break;

                dest.write(nread);
                continue;
            }

            dest.write(buffer, 0, nread);
        }
    }
}

@@ -196,17 +196,16 @@
 ontext;%0A
-%0A
 import a
@@ -237,17 +237,16 @@
 anager;%0A
-%0A
 import a
@@ -263,16 +263,79 @@
 l.Log;%0A%0A
+import static android.os.Environment.getExternalStorageState;%0A%0A
 %0Apublic 
@@ -542,24 +542,14 @@
 ile 
-root = new File(
+dir = 
 cont
@@ -577,16 +577,201 @@
 ir(null)
+;%0A        if (null == dir)%0A            throw new IOException(%22external storage state: %22 +%0A                                  getExternalStorageState());%0A%0A        File root = new File(dir
 , path);
@@ -1345,25 +1345,25 @@
         Log.
-d
+i
 (TAG, %22copy 
@@ -1501,17 +1501,17 @@
     Log.
-d
+i
 (TAG, %22s

Check application directory path for null