本文整理汇总了Java中java.net.URLClassLoader.getResourceAsStream方法的典型用法代码示例。如果您正苦于以下问题:Java URLClassLoader.getResourceAsStream方法的具体用法?Java URLClassLoader.getResourceAsStream怎么用?Java URLClassLoader.getResourceAsStream使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.URLClassLoader
的用法示例。
在下文中一共展示了URLClassLoader.getResourceAsStream方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: assertResource
import java.net.URLClassLoader; //导入方法依赖的package包/类
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();
}
}
示例2: getResourceAsStream
import java.net.URLClassLoader; //导入方法依赖的package包/类
/**
* 提取资源为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;
}
示例3: doGet
import java.net.URLClassLoader; //导入方法依赖的package包/类
@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);
}
}
示例4: test
import java.net.URLClassLoader; //导入方法依赖的package包/类
static void test (File file, boolean loadclass, boolean readall)
throws Exception
{
URL[] urls = new URL[] {file.toURI().toURL()};
System.out.println ("Doing tests with URL: " + urls[0]);
URLClassLoader loader = new URLClassLoader (urls);
if (loadclass) {
Class testclass = loadClass ("com.foo.TestClass", loader, true);
}
InputStream s = loader.getResourceAsStream ("hello.txt");
s.read();
if (readall) {
while (s.read() != -1) ;
s.close();
}
loader.close ();
// should not find bye.txt now
InputStream s1 = loader.getResourceAsStream("bye.txt");
if (s1 != null) {
throw new RuntimeException ("closed loader returned resource");
}
// now check we can delete the path
rm_minus_rf (file);
System.out.println (" ... OK");
}
示例5: PluginBubble
import java.net.URLClassLoader; //导入方法依赖的package包/类
public PluginBubble(File pluginFile) throws Exception {
URLClassLoader loader = new URLClassLoader(new URL[]{pluginFile.toURL()}, ClassLoader.getSystemClassLoader());
InputStream stream = loader.getResourceAsStream("plugin.json");
JSONObject json = new JSONObject(IOUtils.toString(stream, "utf-8"));
stream.close();
pluginName = json.getString("name");
pluginInstance = (SagiriPlugin) loader.loadClass(json.getString("main")).newInstance();
}
示例6: getSoundbank
import java.net.URLClassLoader; //导入方法依赖的package包/类
@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;
}
示例7: testShaderWithRelocatedClassname
import java.net.URLClassLoader; //导入方法依赖的package包/类
public void testShaderWithRelocatedClassname()
throws Exception
{
DefaultShader s = newShader();
Set<File> set = new LinkedHashSet<File>();
set.add( new File( "src/test/jars/test-project-1.0-SNAPSHOT.jar" ) );
set.add( new File( "src/test/jars/plexus-utils-1.4.1.jar" ) );
List<Relocator> relocators = new ArrayList<Relocator>();
relocators.add( new SimpleRelocator( "org/codehaus/plexus/util/", "_plexus/util/__", null,
Arrays.<String> asList() ) );
List<ResourceTransformer> resourceTransformers = new ArrayList<ResourceTransformer>();
resourceTransformers.add( new ComponentsXmlResourceTransformer() );
List<Filter> filters = new ArrayList<Filter>();
File file = new File( "target/foo-relocate-class.jar" );
ShadeRequest shadeRequest = new ShadeRequest();
shadeRequest.setJars( set );
shadeRequest.setUberJar( file );
shadeRequest.setFilters( filters );
shadeRequest.setRelocators( relocators );
shadeRequest.setResourceTransformers( resourceTransformers );
s.shade( shadeRequest );
URLClassLoader cl = new URLClassLoader( new URL[] { file.toURI().toURL() } );
Class<?> c = cl.loadClass( "_plexus.util.__StringUtils" );
// first, ensure it works:
Object o = c.newInstance();
assertEquals( "", c.getMethod( "clean", String.class ).invoke( o, (String) null ) );
// now, check that its source file was rewritten:
final String[] source = { null };
final ClassReader classReader = new ClassReader( cl.getResourceAsStream( "_plexus/util/__StringUtils.class" ) );
classReader.accept( new ClassVisitor( Opcodes.ASM4 )
{
@Override
public void visitSource( String arg0, String arg1 )
{
super.visitSource( arg0, arg1 );
source[0] = arg0;
}
}, ClassReader.SKIP_CODE );
assertEquals( "__StringUtils.java", source[0] );
}