### Question:
ResourceUtil { public static InputStream asInputStream(String resource) throws IOException { return asInputStream(resource, inStream -> inStream); }  static URI fromShapefileDir(String shapeFile); static URI fromShapefileDir(URI shapeFile); static InputStream asInputStream(String resource); static InputStream asInputStream(String resource, InputStreamFactory factory); static InputStream asInputStream(URI resource, InputStreamFactory factory); static boolean isFileURI(URI resource); static URI getAbsoluteResourceURI(URI context, URI resourceName); static final String SHAPEFILES_DIR; }### Answer:
@Test public void localJarResource() throws IOException { URL resource = getClass().getResource("/java/lang/String.class"); InputStream inputStream = ResourceUtil.asInputStream(resource.toString()); assertNotNull(inputStream); inputStream.close(); assertThat(inputStream.available(), is(0)); }
@Test public void localJarResourceWithInputStreamFactory() throws URISyntaxException, IOException { URL resource = getClass().getResource("/java/lang/String.class"); AtomicLong counter = new AtomicLong(0); InputStream inputStream = ResourceUtil.asInputStream(resource.toString(), new InputStreamFactory() { @Override public InputStream create(InputStream inStream) throws IOException { return new ProxyInputStream(inStream) { @Override protected void afterRead(int n) throws IOException { counter.addAndGet(n); } }; } }); assertNotNull(inputStream); IOUtils.copy(inputStream, new NullOutputStream()); inputStream.close(); assertThat(counter.get(), greaterThan(100L)); }
@Test public void kaboomWithInputStreamFactory() throws URISyntaxException { URL resource = getClass().getResource("/java/lang/String.class"); try { ResourceUtil.asInputStream(resource.toString(), new InputStreamFactory() { @Override public InputStream create(InputStream inStream) throws IOException { throw new IOException("kaboom!"); } }); fail("expected IOException"); } catch (IOException ex) { assertThat(ex.getCause().getMessage(), is("kaboom!")); } }