classpathreader.java.svn-base

来自「cqME :java framework for TCK test.」· SVN-BASE 代码 · 共 211 行

SVN-BASE
211
字号
/* * $Id$ * * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package com.sun.tck.cldc.javatest.util;import java.io.ByteArrayOutputStream;import java.io.FileFilter;import java.io.InputStream;import java.io.FileInputStream;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.Enumeration;import java.util.Hashtable;import java.util.Vector;import java.util.jar.JarEntry;import java.util.jar.JarFile;public class ClassPathReader {    private File[] classpath = null;    private byte[] b = new byte[1024];    private ByteArrayOutputStream baos = new ByteArrayOutputStream(32768);    public ClassPathReader(String classpath) {        setClassPath(classpath);    }    public void setClassPath(String classpath) {        if (classpath != null) {            this.classpath = split(classpath);        }    }    /**     * Searches in the paths of classpath and returns an array of abstract     * pathnames denoting the files in the specified directory relative to the     * paths of the classpath that satisfy the specified filter.     *      * @param parent - A directory relative to the paths of the classpath.     * @param filter - A file filter.     */    public String[] listFiles(String parent, FileFilter filter) {        ArrayList<String> files = new ArrayList();        for (File path : classpath) {            if (path.isDirectory()) {                File sp = new File(path, parent.replace('/', File.separatorChar));                if (sp.exists()) {                    for (File f : sp.listFiles(filter)) {                        files.add(f.getPath().                                substring(path.getPath().length() + 1).                                replace(File.separatorChar, '/'));                    }                }            } else {                JarFile z = getJarFile(path);                if (z != null) {                    Enumeration<JarEntry> entries = z.entries();                    while (entries.hasMoreElements()) {                        String entryName = entries.nextElement().getName();                        if (entryName.startsWith(parent) &&                             (entryName.indexOf('/', parent.length() + 1) == -1) &&                               filter.accept(new File(entryName))) {                            files.add(entryName);                        }                    }                }            }        }        return files.toArray(new String[]{});    }        public byte[] getFileData(String name) {        InputStream in = getFileDataAsStream(name);        if (in == null) {            throw new RuntimeException("Class or resource not found: " + name);        }        try {            // reset baos's count, discard accumulated output            baos.reset();            int nb;            while ((nb = in.read(b)) != -1) {                baos.write(b, 0, nb);            }            in.close();            return baos.toByteArray();        } catch (Exception e) {            throw new RuntimeException(e.toString());        }    }    public InputStream getFileDataAsStream(String name) {        if (classpath == null) {             return getClass().getResourceAsStream("/" + name);        }        for (int i = 0; i < classpath.length; i++) {            InputStream data;            if (classpath[i].isDirectory()) {                data = getFileDataInDir(name, classpath[i]);            } else {                data = getFileDataInJar(name, classpath[i]);            }            if (data != null) {                return data;            }        }        return null;    }    private InputStream getFileDataInDir(String cname, File dir) {        // System.err.println("locateClassInDir: " + cname + " " + dir);        try {            File file = new File(dir, cname.replace('/', File.separatorChar));            return new FileInputStream(file);        } catch (IOException e) {            // System.err.println("locateClassInDir: " + e);            return null;        }    }    private InputStream getFileDataInJar(String cname, File jarFile) {        // System.err.println("locateClassInJar: " + cname + " " + jarFile);        JarFile z = getJarFile(jarFile);        if (z != null) {            JarEntry ze = (JarEntry) z.getEntry(cname);            if (ze != null) {                try {                    return z.getInputStream(ze);                } catch (IOException e) {                    // System.err.println("locateClassInJar: " + e);                }            }        }        return null;    }    private File[] split(String s) {        char pathCh = File.pathSeparatorChar;        Vector v = new Vector();        int start = 0;        for (int i = s.indexOf(pathCh); i != -1;                i = s.indexOf(pathCh, start)) {            add(s.substring(start, i), v);            start = i + 1;        }        if (start != s.length()) {            add(s.substring(start), v);        }        File[] path = new File[v.size()];        v.copyInto(path);        return path;    }    private void add(String s, Vector v) {        if (s.length() != 0) {            File f = new File(s);            if (! f.exists()) {                throw new RuntimeException(s + " not found");            }            v.addElement(f);        }    }        private JarFile getJarFile(File jar) {        JarFile z = (JarFile) zips.get(jar);        if (z == null) {            try {                z = new JarFile(jar);                zips.put(jar, z);            } catch (IOException e) {            }        }        return z;    }        private Hashtable zips = new Hashtable();}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?