Search in sources :

Example 36 with FileFilter

use of java.io.FileFilter in project MSEC by Tencent.

the class ClassUtils method findAndAddClassesInPackageByFile.

/**
	 * ���ļ�����ʽ����ȡ���µ�����Class
	 * 
	 * @param packageName
	 * @param packagePath
	 * @param recursive
	 * @param classes
	 */
public static void findAndAddClassesInPackageByFile(String packageName, String packagePath, final boolean recursive) {
    // ��ȡ�˰���Ŀ¼ ����һ��File
    File dir = new File(packagePath);
    // ��������ڻ��� Ҳ����Ŀ¼��ֱ�ӷ���
    if (!dir.exists() || !dir.isDirectory()) {
        // log.warn("�û�������� " + packageName + " ��û���κ��ļ�");
        return;
    }
    // ������� �ͻ�ȡ���µ������ļ� ����Ŀ¼
    File[] dirfiles = dir.listFiles(new FileFilter() {

        // �Զ�����˹��� �������ѭ��(������Ŀ¼) ��������.class��β���ļ�(����õ�java���ļ�)
        public boolean accept(File file) {
            return (recursive && file.isDirectory()) || (file.getName().endsWith(".class"));
        }
    });
    // ѭ�������ļ�
    for (File file : dirfiles) {
        // �����Ŀ¼ �����ɨ��
        if (file.isDirectory()) {
            findAndAddClassesInPackageByFile(packageName + "." + file.getName(), file.getAbsolutePath(), recursive);
        } else {
            // �����java���ļ� ȥ�������.class ֻ��������
            String className = file.getName().substring(0, file.getName().length() - 6);
            try {
                Class.forName(packageName + '.' + className);
            // classes.add(Thread.currentThread().getContextClassLoader().loadClass(packageName
            // + '.' + className));
            } catch (ClassNotFoundException e) {
                // log.error("����û��Զ�����ͼ����� �Ҳ��������.class�ļ�");
                e.printStackTrace();
            }
        }
    }
}
Also used : FileFilter(java.io.FileFilter) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 37 with FileFilter

use of java.io.FileFilter in project atlas by alibaba.

the class BundleReleaser method dexOptimization.

private void dexOptimization() {
    Log.e(TAG, "dexOptimization start");
    final File[] validDexes = reversionDir.listFiles(new FileFilter() {

        @Override
        public boolean accept(File pathname) {
            if (!DexReleaser.isArt()) {
                return pathname.getName().endsWith(DEX_SUFFIX);
            } else {
                return pathname.getName().endsWith(".zip");
            }
        }
    });
    dexFiles = new DexFile[validDexes.length];
    final CountDownLatch countDownLatch = new CountDownLatch(validDexes.length);
    for (int i = 0; i < validDexes.length; i++) {
        final int j = i;
        service.submit(new Runnable() {

            @Override
            public void run() {
                long startTime = System.currentTimeMillis();
                String optimizedPath = optimizedPathFor(validDexes[j], dexOptDir());
                try {
                    dexFiles[j] = DexFile.loadDex(validDexes[j].getPath(), optimizedPath, 0);
                    boolean result = verifyDexFile(dexFiles[j]);
                    if (!result) {
                        handler.sendMessage(handler.obtainMessage(MSG_ID_RELEASE_FAILED));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    handler.sendMessage(handler.obtainMessage(MSG_ID_RELEASE_FAILED));
                } finally {
                //后面需要loadclass,这里不能close
                //                        if (dexFile != null) {
                //                            try {
                //                                dexFile.close();
                //                            } catch (IOException e) {
                //                                e.printStackTrace();
                //                            }
                //                        }
                }
                Log.e(TAG, String.format("dex %s consume %d ms", validDexes[j].getAbsolutePath(), System.currentTimeMillis() - startTime));
                countDownLatch.countDown();
            }
        });
    }
    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    Log.e(TAG, "dex opt done");
    handler.sendMessage(handler.obtainMessage(MSG_ID_DEX_OPT_DONE));
}
Also used : IOException(java.io.IOException) FileFilter(java.io.FileFilter) CountDownLatch(java.util.concurrent.CountDownLatch) DexFile(dalvik.system.DexFile) File(java.io.File)

Example 38 with FileFilter

use of java.io.FileFilter in project atlas by alibaba.

the class MD5Util method getFileMd5.

public static String getFileMd5(Collection<File> inputFiles) {
    StringBuilder stringBuilder = new StringBuilder();
    FileFilter fileFilter = new FileFilter() {

        @Override
        public boolean accept(File pathname) {
            return pathname.isFile();
        }
    };
    for (File file : inputFiles) {
        if (null == file || !file.exists()) {
            continue;
        }
        if (file.isFile()) {
            stringBuilder.append(MD5Util.getFileMD5(file));
        } else {
            List<File> files = new ArrayList<File>();
            listFilesForFolder(file, files);
            for (File file1 : files) {
                stringBuilder.append(MD5Util.getFileMD5(file1));
            }
        }
    }
    try {
        return MD5Util.getMD5(stringBuilder.toString());
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }
}
Also used : ArrayList(java.util.ArrayList) FileFilter(java.io.FileFilter) File(java.io.File) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException)

Example 39 with FileFilter

use of java.io.FileFilter in project atlas by alibaba.

the class PatchUtils method getTpatchClassDef.

public static void getTpatchClassDef(File tpatchFile, Map<String, Map<String, ClassDef>> bundleMap) throws IOException {
    if (tpatchFile == null || !tpatchFile.exists()) {
        return;
    }
    File unZipFolder = new File(tpatchFile.getParentFile(), tpatchFile.getName().split("\\.")[0]);
    unZipFolder.mkdirs();
    ZipUtils.unzip(tpatchFile, unZipFolder.getAbsolutePath());
    File[] bundleFolder = unZipFolder.listFiles(new FileFilter() {

        @Override
        public boolean accept(File pathname) {
            return pathname.isDirectory() && pathname.getName().startsWith("lib");
        }
    });
    for (File file : bundleFolder) {
        Map<String, ClassDef> dexClasses = getBundleClassDef(file);
        bundleMap.put(file.getName(), dexClasses);
    }
    FileUtils.deleteDirectory(tpatchFile.getParentFile());
}
Also used : ClassDef(org.jf.dexlib2.iface.ClassDef) FileFilter(java.io.FileFilter) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File)

Example 40 with FileFilter

use of java.io.FileFilter in project canal by alibaba.

the class SpringInstanceConfigMonitor method scan.

private void scan() {
    File rootdir = new File(rootConf);
    if (!rootdir.exists()) {
        return;
    }
    File[] instanceDirs = rootdir.listFiles(new FileFilter() {

        public boolean accept(File pathname) {
            String filename = pathname.getName();
            return pathname.isDirectory() && !"spring".equalsIgnoreCase(filename);
        }
    });
    // 扫描目录的新增
    Set<String> currentInstanceNames = new HashSet<String>();
    // 判断目录内文件的变化
    for (File instanceDir : instanceDirs) {
        String destination = instanceDir.getName();
        currentInstanceNames.add(destination);
        File[] instanceConfigs = instanceDir.listFiles(new FilenameFilter() {

            public boolean accept(File dir, String name) {
                // 限制一下,只针对instance.properties文件,避免因为.svn或者其他生成的临时文件导致出现reload
                return StringUtils.equalsIgnoreCase(name, "instance.properties");
            }
        });
        if (!actions.containsKey(destination) && instanceConfigs.length > 0) {
            // 存在合法的instance.properties,并且第一次添加时,进行启动操作
            notifyStart(instanceDir, destination);
        } else if (actions.containsKey(destination)) {
            // 历史已经启动过
            if (instanceConfigs.length == 0) {
                // 如果不存在合法的instance.properties
                notifyStop(destination);
            } else {
                InstanceConfigFiles lastFile = lastFiles.get(destination);
                boolean hasChanged = judgeFileChanged(instanceConfigs, lastFile.getInstanceFiles());
                // 通知变化
                if (hasChanged) {
                    notifyReload(destination);
                }
                if (hasChanged || CollectionUtils.isEmpty(lastFile.getInstanceFiles())) {
                    // 更新内容
                    List<FileInfo> newFileInfo = new ArrayList<FileInfo>();
                    for (File instanceConfig : instanceConfigs) {
                        newFileInfo.add(new FileInfo(instanceConfig.getName(), instanceConfig.lastModified()));
                    }
                    lastFile.setInstanceFiles(newFileInfo);
                }
            }
        }
    }
    // 判断目录是否删除
    Set<String> deleteInstanceNames = new HashSet<String>();
    for (String destination : actions.keySet()) {
        if (!currentInstanceNames.contains(destination)) {
            deleteInstanceNames.add(destination);
        }
    }
    for (String deleteInstanceName : deleteInstanceNames) {
        notifyStop(deleteInstanceName);
    }
}
Also used : FilenameFilter(java.io.FilenameFilter) ArrayList(java.util.ArrayList) List(java.util.List) FileFilter(java.io.FileFilter) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

FileFilter (java.io.FileFilter)281 File (java.io.File)267 ArrayList (java.util.ArrayList)59 IOException (java.io.IOException)53 JarFile (java.util.jar.JarFile)22 URL (java.net.URL)17 Test (org.junit.Test)16 HashMap (java.util.HashMap)15 FileNotFoundException (java.io.FileNotFoundException)11 FilenameFilter (java.io.FilenameFilter)11 HashSet (java.util.HashSet)11 FileInputStream (java.io.FileInputStream)10 Pattern (java.util.regex.Pattern)10 WildcardFileFilter (org.apache.commons.io.filefilter.WildcardFileFilter)10 MalformedURLException (java.net.MalformedURLException)9 URI (java.net.URI)9 Map (java.util.Map)7 Path (org.apache.hadoop.fs.Path)7 NotNull (org.jetbrains.annotations.NotNull)7 Treebank (edu.stanford.nlp.trees.Treebank)6