Coder Social home page Coder Social logo

Comments (1)

anthony0982 avatar anthony0982 commented on June 9, 2024

原因:异步复制,但在主线程关闭了文件。问题语句:zip.close();

修改:

/**
 * copy so lib to specify directory(/data/data/host_pack_name/pluginlib)
 * 
 * @param dexPath
 *            plugin path
 * @param cpuName
 *            cpuName CPU_X86,CPU_MIPS,CPU_ARMEABI
 */
public void copyPluginSoLib(Context context, String dexPath, String nativeLibDir) {
    String cpuName = getCpuName();
    String cpuArchitect = getCpuArch(cpuName);

    sNativeLibDir = nativeLibDir;
    Log.d(TAG, "cpuArchitect: " + cpuArchitect);
    long start = System.currentTimeMillis();
    try {
        ZipFile zipFile = new ZipFile(dexPath);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry zipEntry = (ZipEntry) entries.nextElement();
            if (zipEntry.isDirectory()) {
                continue;
            }
            String zipEntryName = zipEntry.getName();
            if (zipEntryName.endsWith(".so") && zipEntryName.contains(cpuArchitect)) {
                final long lastModify = zipEntry.getTime();
                if (lastModify == DLConfigs.getSoLastModifiedTime(context, zipEntryName)) {
                    // exist and no change
                    Log.d(TAG, "skip copying, the so lib is exist and not change: " + zipEntryName);
                    continue;
                }
                mSoExecutor.execute(new CopySoTask(context, zipFile, zipEntry, lastModify));
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    long end = System.currentTimeMillis();
    Log.d(TAG, "### copy so time : " + (end - start) + " ms");
}

/**
 * @author mrsimple
 */
private class CopySoTask implements Runnable {

    private String mSoFileName;
    private ZipFile mZipFile;
    private ZipEntry mZipEntry;
    private Context mContext;
    private long mLastModityTime;

    CopySoTask(Context context, ZipFile zipFile, ZipEntry zipEntry, long lastModify) {
        mZipFile = zipFile;
        mContext = context;
        mZipEntry = zipEntry;
        mSoFileName = parseSoFileName(zipEntry.getName());
        mLastModityTime = lastModify;
    }

    private final String parseSoFileName(String zipEntryName) {
        return zipEntryName.substring(zipEntryName.lastIndexOf("/") + 1);
    }

    private void writeSoFile2LibDir() {
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            is = mZipFile.getInputStream(mZipEntry);
            fos = new FileOutputStream(new File(sNativeLibDir, mSoFileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        copy(is, fos);
        try {
            mZipFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 输入输出流拷贝
     * 
     * @param is
     * @param os
     */
    public void copy(InputStream is, OutputStream os) {
        if (is == null || os == null)
            return;
        BufferedInputStream bis = new BufferedInputStream(is);
        BufferedOutputStream bos = new BufferedOutputStream(os);
        byte[] buf = null;
        try {
            buf = new byte[getAvailableSize(bis)];
        } catch (IOException e) {
            e.printStackTrace();
        }
        int i = 0;
        try {
            while ((i = bis.read(buf)) != -1) {
                bos.write(buf, 0, i);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            bos.flush();
            bos.close();
            bis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private int getAvailableSize(InputStream is) throws IOException {
        if (is == null)
            return 0;
        int available = is.available();
        return available <= 0 ? 1024 : available;
    }

    @Override
    public void run() {
        writeSoFile2LibDir();
        DLConfigs.setSoLastModifiedTime(mContext, mZipEntry.getName(), mLastModityTime);
        Log.d(TAG, "copy so lib success: " + mZipEntry.getName());
    }

}

from dynamic-load-apk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.