java.net.URLClassLoader#getResourceAsStream ( )源码实例Demo

下面列出了java.net.URLClassLoader#getResourceAsStream ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: camel-idea-plugin   文件: CamelService.java
private static String loadComponentJSonSchema(URLClassLoader classLoader, String scheme) {
    String answer = null;

    String path = null;
    String javaType = extractComponentJavaType(classLoader, scheme);
    if (javaType != null) {
        int pos = javaType.lastIndexOf(".");
        path = javaType.substring(0, pos);
        path = path.replace('.', '/');
        path = path + "/" + scheme + ".json";
    }

    if (path != null) {
        try {
            InputStream is = classLoader.getResourceAsStream(path);
            if (is != null) {
                answer = loadText(is);
            }
        } catch (Throwable e) {
            LOG.warn("Error loading " + path + " file", e);
        }
    }

    return answer;
}
 
private static void assertResource(URLClassLoader classLoader, String file,
        String expectedContent) throws IOException {
    InputStream fileStream = classLoader.getResourceAsStream(file);

    if (fileStream == null && expectedContent == null) {
        return;
    }
    if (fileStream == null && expectedContent != null) {
        throw new RuntimeException(
                buildMessage(file, expectedContent, null));
    }
    try {
        String actualContent = readAsString(fileStream);

        if (fileStream != null && expectedContent == null) {
            throw new RuntimeException(buildMessage(file, null,
                    actualContent));
        }
        if (!expectedContent.equals(actualContent)) {
            throw new RuntimeException(buildMessage(file, expectedContent,
                    actualContent));
        }
    } finally {
        fileStream.close();
    }
}
 
private static void assertResource(URLClassLoader classLoader, String file,
        String expectedContent) throws IOException {
    InputStream fileStream = classLoader.getResourceAsStream(file);

    if (fileStream == null && expectedContent == null) {
        return;
    }
    if (fileStream == null && expectedContent != null) {
        throw new RuntimeException(
                buildMessage(file, expectedContent, null));
    }
    try {
        String actualContent = readAsString(fileStream);

        if (fileStream != null && expectedContent == null) {
            throw new RuntimeException(buildMessage(file, null,
                    actualContent));
        }
        if (!expectedContent.equals(actualContent)) {
            throw new RuntimeException(buildMessage(file, expectedContent,
                    actualContent));
        }
    } finally {
        fileStream.close();
    }
}
 
源代码4 项目: jdk8u60   文件: JarIndexMergeForClassLoaderTest.java
private static void assertResource(URLClassLoader classLoader, String file,
        String expectedContent) throws IOException {
    InputStream fileStream = classLoader.getResourceAsStream(file);

    if (fileStream == null && expectedContent == null) {
        return;
    }
    if (fileStream == null && expectedContent != null) {
        throw new RuntimeException(
                buildMessage(file, expectedContent, null));
    }
    try {
        String actualContent = readAsString(fileStream);

        if (fileStream != null && expectedContent == null) {
            throw new RuntimeException(buildMessage(file, null,
                    actualContent));
        }
        if (!expectedContent.equals(actualContent)) {
            throw new RuntimeException(buildMessage(file, expectedContent,
                    actualContent));
        }
    } finally {
        fileStream.close();
    }
}
 
private static void assertResource(URLClassLoader classLoader, String file,
        String expectedContent) throws IOException {
    InputStream fileStream = classLoader.getResourceAsStream(file);

    if (fileStream == null && expectedContent == null) {
        return;
    }
    if (fileStream == null && expectedContent != null) {
        throw new RuntimeException(
                buildMessage(file, expectedContent, null));
    }
    try {
        String actualContent = readAsString(fileStream);

        if (fileStream != null && expectedContent == null) {
            throw new RuntimeException(buildMessage(file, null,
                    actualContent));
        }
        if (!expectedContent.equals(actualContent)) {
            throw new RuntimeException(buildMessage(file, expectedContent,
                    actualContent));
        }
    } finally {
        fileStream.close();
    }
}
 
private static void assertResource(URLClassLoader classLoader, String file,
        String expectedContent) throws IOException {
    InputStream fileStream = classLoader.getResourceAsStream(file);

    if (fileStream == null && expectedContent == null) {
        return;
    }
    if (fileStream == null && expectedContent != null) {
        throw new RuntimeException(
                buildMessage(file, expectedContent, null));
    }
    try {
        String actualContent = readAsString(fileStream);

        if (fileStream != null && expectedContent == null) {
            throw new RuntimeException(buildMessage(file, null,
                    actualContent));
        }
        if (!expectedContent.equals(actualContent)) {
            throw new RuntimeException(buildMessage(file, expectedContent,
                    actualContent));
        }
    } finally {
        fileStream.close();
    }
}
 
private static void assertResource(URLClassLoader classLoader, String file,
        String expectedContent) throws IOException {
    InputStream fileStream = classLoader.getResourceAsStream(file);

    if (fileStream == null && expectedContent == null) {
        return;
    }
    if (fileStream == null && expectedContent != null) {
        throw new RuntimeException(
                buildMessage(file, expectedContent, null));
    }
    try {
        String actualContent = readAsString(fileStream);

        if (fileStream != null && expectedContent == null) {
            throw new RuntimeException(buildMessage(file, null,
                    actualContent));
        }
        if (!expectedContent.equals(actualContent)) {
            throw new RuntimeException(buildMessage(file, expectedContent,
                    actualContent));
        }
    } finally {
        fileStream.close();
    }
}
 
private static void assertResource(URLClassLoader classLoader, String file,
        String expectedContent) throws IOException {
    InputStream fileStream = classLoader.getResourceAsStream(file);

    if (fileStream == null && expectedContent == null) {
        return;
    }
    if (fileStream == null && expectedContent != null) {
        throw new RuntimeException(
                buildMessage(file, expectedContent, null));
    }
    try {
        String actualContent = readAsString(fileStream);

        if (fileStream != null && expectedContent == null) {
            throw new RuntimeException(buildMessage(file, null,
                    actualContent));
        }
        if (!expectedContent.equals(actualContent)) {
            throw new RuntimeException(buildMessage(file, expectedContent,
                    actualContent));
        }
    } finally {
        fileStream.close();
    }
}
 
源代码9 项目: uavstack   文件: JarHelper.java
/**
 * 提取资源为InputStream
 * 
 * @param jarFilePath
 * @param resourcePath
 * @return
 */
public static InputStream getResourceAsStream(String jarFilePath, String resourcePath) {

    File file = new File(jarFilePath);

    if (!file.exists()) {
        return null;
    }

    try {
        @SuppressWarnings("resource")
        URLClassLoader cl = new URLClassLoader(new URL[] { file.toURI().toURL() });

        InputStream is = cl.getResourceAsStream(resourcePath);

        return is;

    }
    catch (Exception e) {
        // ignore
    }

    return null;
}
 
源代码10 项目: uavstack   文件: JarUtil.java
/**
 * 提取资源为InputStream
 * 
 * @param jarFilePath
 * @param resourcePath
 * @return
 */
public static InputStream getResourceAsStream(String jarFilePath, String resourcePath) {

    File file = new File(jarFilePath);

    if (!file.exists()) {
        return null;
    }

    try {
        @SuppressWarnings("resource")
        URLClassLoader cl = new URLClassLoader(new URL[] { file.toURI().toURL() });

        InputStream is = cl.getResourceAsStream(resourcePath);

        return is;

    }
    catch (Exception e) {
        // ignore
    }

    return null;
}
 
源代码11 项目: doctorkafka   文件: DoctorKafkaWebServer.java
@Override
protected void doGet(HttpServletRequest request,
                     HttpServletResponse response) throws ServletException,
                                                          IOException {
  response.setContentType("text/html");
  try {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    URL resource = classLoader.getResource(indexPagePath);
    URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{resource});

    InputStream input = urlClassLoader.getResourceAsStream(indexPagePath);
    String result = CharStreams.toString(new InputStreamReader(input, Charsets.UTF_8));
    response.setStatus(HttpServletResponse.SC_OK);
    response.getWriter().print(result);
  } catch (Exception e) {
    response.getWriter().print(e);
    LOG.error("error : ", e);
  }
}
 
private static void assertResource(URLClassLoader classLoader, String file,
        String expectedContent) throws IOException {
    InputStream fileStream = classLoader.getResourceAsStream(file);

    if (fileStream == null && expectedContent == null) {
        return;
    }
    if (fileStream == null && expectedContent != null) {
        throw new RuntimeException(
                buildMessage(file, expectedContent, null));
    }
    try {
        String actualContent = readAsString(fileStream);

        if (fileStream != null && expectedContent == null) {
            throw new RuntimeException(buildMessage(file, null,
                    actualContent));
        }
        if (!expectedContent.equals(actualContent)) {
            throw new RuntimeException(buildMessage(file, expectedContent,
                    actualContent));
        }
    } finally {
        fileStream.close();
    }
}
 
源代码13 项目: fabric8-forge   文件: CamelProjectHelper.java
public static boolean isCamelComponentArtifact(Dependency dependency) {
    try {
        // is it a JAR file
        File file = dependency.getArtifact().getUnderlyingResourceObject();
        if (file != null && file.getName().toLowerCase().endsWith(".jar")) {
            URL url = new URL("file:" + file.getAbsolutePath());
            URLClassLoader child = new URLClassLoader(new URL[]{url});

            // custom component
            InputStream is = child.getResourceAsStream("META-INF/services/org/apache/camel/component.properties");
            if (is != null) {
                IOHelpers.close(is);
                return true;
            }
        }
    } catch (Throwable e) {
        // ignore
    }

    return false;
}
 
public static Properties loadComponentProperties(Dependency dependency) {
    Properties answer = new Properties();

    try {
        // is it a JAR file
        File file = dependency.getArtifact().getUnderlyingResourceObject();
        if (file != null && file.getName().toLowerCase().endsWith(".jar")) {
            URL url = new URL("file:" + file.getAbsolutePath());
            URLClassLoader child = new URLClassLoader(new URL[]{url});

            InputStream is = child.getResourceAsStream("META-INF/services/org/apache/camel/component.properties");
            if (is != null) {
                answer.load(is);
            }
        }
    } catch (Throwable e) {
        // ignore
    }

    return answer;
}
 
源代码15 项目: camel-idea-plugin   文件: CamelService.java
private static Properties loadComponentProperties(URLClassLoader classLoader, boolean legacyScan) {
    Properties answer = new Properties();
    try {
        // load the component files using the recommended way by a component.properties file
        InputStream is = classLoader.getResourceAsStream("META-INF/services/org/apache/camel/component.properties");
        if (is != null) {
            answer.load(is);
        } else if (legacyScan) {
            // okay then try to load using a fallback using legacy classpath scanning
            loadComponentPropertiesClasspathScan(classLoader, answer);
        }
    } catch (Throwable e) {
        LOG.warn("Error loading META-INF/services/org/apache/camel/component.properties file", e);
    }
    return answer;
}
 
源代码16 项目: usergrid   文件: CoordinatorUtils.java
/**
 *
 * @param runnerJar
 * @param resource
 * @return
 */
public static InputStream getResourceAsStreamFromRunnerJar( File runnerJar, String resource ) {
    try {
        // Access the jar file resources after adding it to a new ClassLoader
        URLClassLoader classLoader = new URLClassLoader( new URL[] { runnerJar.toURL() },
                Thread.currentThread().getContextClassLoader() );

        return classLoader.getResourceAsStream( resource );
    }
    catch ( Exception e ) {
        LOG.warn( "Error while reading {} from runner.jar resources", resource, e );
        return null;
    }
}
 
@Override
public ArchetypeCatalog getArchetypeCatalog() {
    if (cachedArchetypes == null) {
        String version = VersionHelper.fabric8ArchetypesVersion();

        Coordinate coordinate = CoordinateBuilder.create()
                .setGroupId("io.fabric8.archetypes")
                .setArtifactId("archetypes-catalog")
                .setVersion(version)
                .setPackaging("jar");

        // load the archetype-catalog.xml from inside the JAR
        Dependency dependency = resolver.get().resolveArtifact(DependencyQueryBuilder.create(coordinate));
        if (dependency != null) {
            try {
                String name = dependency.getArtifact().getFullyQualifiedName();
                URL url = new URL("file", null, name);
                URLClassLoader loader = new URLClassLoader(new URL[]{url});
                InputStream is = loader.getResourceAsStream("archetype-catalog.xml");
                if (is != null) {
                    cachedArchetypes = new ArchetypeCatalogXpp3Reader().read(is);
                }
            } catch (Exception e) {
                LOG.log(Level.WARNING, "Error while retrieving archetypes due " + e.getMessage(), e);
            }
        }
    }
    return cachedArchetypes;
}
 
public static String loadComponentJSonSchema(Dependency dependency, String scheme) {
    String answer = null;

    String path = null;
    String javaType = extractComponentJavaType(dependency, scheme);
    if (javaType != null) {
        int pos = javaType.lastIndexOf(".");
        path = javaType.substring(0, pos);
        path = path.replace('.', '/');
        path = path + "/" + scheme + ".json";
    }

    if (path != null) {
        try {
            // is it a JAR file
            File file = dependency.getArtifact().getUnderlyingResourceObject();
            if (file != null && file.getName().toLowerCase().endsWith(".jar")) {
                URL url = new URL("file:" + file.getAbsolutePath());
                URLClassLoader child = new URLClassLoader(new URL[]{url});

                InputStream is = child.getResourceAsStream(path);
                if (is != null) {
                    answer = loadText(is);
                }
            }
        } catch (Throwable e) {
            // ignore
        }
    }

    return answer;
}
 
源代码19 项目: Bytecoder   文件: JARSoundbankReader.java
@Override
@SuppressWarnings("deprecation")
public Soundbank getSoundbank(URL url)
        throws InvalidMidiDataException, IOException {
    if (!isZIP(url))
        return null;
    ArrayList<Soundbank> soundbanks = new ArrayList<>();
    URLClassLoader ucl = URLClassLoader.newInstance(new URL[]{url});
    InputStream stream = ucl.getResourceAsStream(
            "META-INF/services/javax.sound.midi.Soundbank");
    if (stream == null)
        return null;
    try
    {
        BufferedReader r = new BufferedReader(new InputStreamReader(stream));
        String line = r.readLine();
        while (line != null) {
            if (!line.startsWith("#")) {
                try {
                    Class<?> c = Class.forName(line.trim(), false, ucl);
                    if (Soundbank.class.isAssignableFrom(c)) {
                        ReflectUtil.checkPackageAccess(c);
                        Object o = c.newInstance();
                        soundbanks.add((Soundbank) o);
                    }
                } catch (ReflectiveOperationException ignored) {
                }
            }
            line = r.readLine();
        }
    }
    finally
    {
        stream.close();
    }
    if (soundbanks.size() == 0)
        return null;
    if (soundbanks.size() == 1)
        return soundbanks.get(0);
    SimpleSoundbank sbk = new SimpleSoundbank();
    for (Soundbank soundbank : soundbanks)
        sbk.addAllInstruments(soundbank);
    return sbk;
}
 
源代码20 项目: openjdk-jdk9   文件: JARSoundbankReader.java
@Override
@SuppressWarnings("deprecation")
public Soundbank getSoundbank(URL url)
        throws InvalidMidiDataException, IOException {
    if (!isZIP(url))
        return null;
    ArrayList<Soundbank> soundbanks = new ArrayList<>();
    URLClassLoader ucl = URLClassLoader.newInstance(new URL[]{url});
    InputStream stream = ucl.getResourceAsStream(
            "META-INF/services/javax.sound.midi.Soundbank");
    if (stream == null)
        return null;
    try
    {
        BufferedReader r = new BufferedReader(new InputStreamReader(stream));
        String line = r.readLine();
        while (line != null) {
            if (!line.startsWith("#")) {
                try {
                    Class<?> c = Class.forName(line.trim(), false, ucl);
                    if (Soundbank.class.isAssignableFrom(c)) {
                        ReflectUtil.checkPackageAccess(c);
                        Object o = c.newInstance();
                        soundbanks.add((Soundbank) o);
                    }
                } catch (ReflectiveOperationException ignored) {
                }
            }
            line = r.readLine();
        }
    }
    finally
    {
        stream.close();
    }
    if (soundbanks.size() == 0)
        return null;
    if (soundbanks.size() == 1)
        return soundbanks.get(0);
    SimpleSoundbank sbk = new SimpleSoundbank();
    for (Soundbank soundbank : soundbanks)
        sbk.addAllInstruments(soundbank);
    return sbk;
}