IoSupport { public static <A, B extends Closeable> A withResource(B b, Function<B, A> f) { try { return f.apply(b); } finally { IoSupport.closeQuietly(b); } } private  IoSupport(); static String getSystemTmpDir(); static boolean closeQuietly(final Closeable s); static boolean closeQuietly(final StreamHelper s); static boolean closeQuietly(final Process process); static void writeUTF8File(URL file, String contents); static void writeUTF8File(File file, String contents); static void writeUTF8File(String filename, String contents); @Deprecated static String readFileFromURL(URL url); @Deprecated static String readFileFromURL(URL url, TrustedHttpClient trustedClient); static Properties loadPropertiesFromUrl(final URL url); static Properties loadPropertiesFromStream(final InputStream stream); static Option<String> loadTxtFromClassPath(final String resource, final Class<?> clazz); @Deprecated static A withStream(InputStream s, Function<InputStream, A> f); static A withResource(B b, Function<B, A> f); static A withResource(B b, Fn<? super B, ? extends A> f); static Option<InputStream> openClassPathResource(String resource, Class<?> clazz); static Option<InputStream> openClassPathResource(String resource); static Option<File> classPathResourceAsFile(String resource); static Option<String> loadFileFromClassPathAsString(String resource, Class<?> clazz); static Option<String> loadFileFromClassPathAsString(String resource); static Option<A> withFile(File file, Function2<InputStream, File, A> f); @Deprecated static Either<Err, A> withStream(Function0<InputStream> s, Function<Exception, Err> toErr,
          Function<InputStream, A> f); static Either<Err, A> withResource(Function0<B> r,
          Function<Exception, Err> toErr, Function<B, A> f); @Deprecated static A withStream(OutputStream s, Function<OutputStream, A> f); static Function0<InputStream> fileInputStream(final File a); static File file(String... pathElems); static synchronized A locked(File file, Function<File, A> f); static A serializeDeserialize(final A a); static final Function<InputStream, String> readToString; }
@Test(expected = IOException.class) public void testWithResource() throws Exception { final InputStream r = this.getClass().getResourceAsStream("/cover.png"); final String s = withResource(r, new Function<InputStream, String>() { @Override public String apply(InputStream in) { return "read"; } }); assertEquals("read", s); r.read(); }