java - Any way to create a URL from a byte array? -
is there way create url byte array? have custom class loader stores entries jarinputstream in hashmap storing entry names bytes. reason i'm looking create url byte array satisfy getresource(string name) method found in classloaders. i've accomplished getresourceasstream(string name) using bytearrayinputstream.
assuming use custom classloader , want store/cache bytes of content in hashmap (not location in byte[] form). have same question brought me here. how able solve this:
class somelassloader { private final map<string, byte[]> entries = new hashmap<>(); public url getresource(string name) { try { return new url(null, "bytes:///" + name, new byteshandler()); } catch (malformedurlexception e) { throw new runtimeexception(e); } } class byteshandler extends urlstreamhandler { @override protected urlconnection openconnection(url u) throws ioexception { return new byteurlconnection(u); } } class byteurlconnection extends urlconnection { public byteurlconnection(url url) { super(url); } @override public void connect() throws ioexception { } @override public inputstream getinputstream() throws ioexception { system.out.println(this.geturl().getpath().substring(1)); return new bytearrayinputstream(entries.get(this.geturl().getpath().substring(1))); } } }
Comments
Post a Comment