Search in sources :

Example 11 with FileFilter

use of java.io.FileFilter in project ansj_seg by NLPchina.

the class File2Stream method multiple.

private InputStream multiple(String path) throws FileNotFoundException {
    File[] libs = new File[0];
    File file = new File(path);
    if (file.exists() && file.canRead()) {
        if (file.isFile()) {
            libs = new File[1];
            libs[0] = file;
        } else if (file.isDirectory()) {
            File[] files = file.listFiles(new FileFilter() {

                public boolean accept(File file) {
                    return file.canRead() && !file.isHidden() && !file.isDirectory();
                }
            });
            if (files != null && files.length > 0) {
                libs = files;
            }
        }
    }
    if (libs.length == 0) {
        throw new LibraryException("not find any file in path : " + path);
    }
    if (libs.length == 1) {
        return new FileInputStream(libs[0]);
    }
    Vector<InputStream> vector = new Vector<>(libs.length);
    for (int i = 0; i < libs.length; i++) {
        vector.add(new FileInputStream(libs[i]));
    }
    return new SequenceInputStream(vector.elements());
}
Also used : SequenceInputStream(java.io.SequenceInputStream) SequenceInputStream(java.io.SequenceInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) LibraryException(org.ansj.exception.LibraryException) FileFilter(java.io.FileFilter) File(java.io.File) Vector(java.util.Vector) FileInputStream(java.io.FileInputStream)

Example 12 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 13 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 14 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 15 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)232 File (java.io.File)218 ArrayList (java.util.ArrayList)50 IOException (java.io.IOException)40 Test (org.junit.Test)13 FilenameFilter (java.io.FilenameFilter)11 URL (java.net.URL)10 HashMap (java.util.HashMap)10 FileInputStream (java.io.FileInputStream)8 FileNotFoundException (java.io.FileNotFoundException)8 HashSet (java.util.HashSet)8 JarFile (java.util.jar.JarFile)8 Pattern (java.util.regex.Pattern)8 Map (java.util.Map)7 WildcardFileFilter (org.apache.commons.io.filefilter.WildcardFileFilter)7 NotNull (org.jetbrains.annotations.NotNull)7 Treebank (edu.stanford.nlp.trees.Treebank)6 Pair (edu.stanford.nlp.util.Pair)6 ArtifactStub (org.apache.maven.plugin.testing.stubs.ArtifactStub)6 EvaluateTreebank (edu.stanford.nlp.parser.lexparser.EvaluateTreebank)5