diff options
author | Shigeru Chiba <chibash@users.noreply.github.com> | 2018-07-19 06:05:45 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-19 06:05:45 +0900 |
commit | 87ea3c526f1f80b75c1387032755a2626ec0d328 (patch) | |
tree | cbde8799dfd3c2c43fb6309f1ced63cbae3c8e64 | |
parent | a59658da9986ec2a9952a6539000c57ff1f02026 (diff) | |
parent | 1aa06e37c8f629276998606a99d64c51fa0858aa (diff) | |
download | javassist-87ea3c526f1f80b75c1387032755a2626ec0d328.tar.gz javassist-87ea3c526f1f80b75c1387032755a2626ec0d328.zip |
Merge pull request #207 from andresluuk/patch-2
Custom URL stream handler in ByteArrayClassPath
-rw-r--r-- | src/main/javassist/ByteArrayClassPath.java | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/src/main/javassist/ByteArrayClassPath.java b/src/main/javassist/ByteArrayClassPath.java index b93bc892..95ea418c 100644 --- a/src/main/javassist/ByteArrayClassPath.java +++ b/src/main/javassist/ByteArrayClassPath.java @@ -17,9 +17,12 @@ package javassist; import java.io.ByteArrayInputStream; +import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; /** * A <code>ByteArrayClassPath</code> contains bytes that is served as @@ -86,12 +89,34 @@ public class ByteArrayClassPath implements ClassPath { if(this.classname.equals(classname)) { String cname = classname.replace('.', '/') + ".class"; try { - // return new File(cname).toURL(); - return new URL("file:/ByteArrayClassPath/" + cname); + return new URL(null, "file:/ByteArrayClassPath/" + cname, new BytecodeURLStreamHandler()); } catch (MalformedURLException e) {} } return null; } + + private class BytecodeURLStreamHandler extends URLStreamHandler { + protected URLConnection openConnection(final URL u) { + return new BytecodeURLConnection(u); + } + } + + private class BytecodeURLConnection extends URLConnection { + protected BytecodeURLConnection(URL url) { + super(url); + } + + public void connect() throws IOException { + } + + public InputStream getInputStream() throws IOException { + return new ByteArrayInputStream(classfile); + } + + public int getContentLength() { + return classfile.length; + } + } } |