Assuming you are using a custom classloader and you want to store / cache bytes of content in a hash map (and not in form in byte []). Then you have the same question that brought me here. But thatβs how I was able to 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))); } } }
source share