@Test public void testWithFile() throws Exception { final File f1 = new File(this.getClass().getResource("/dublincore.xml").toURI()); final Option<String> r1 = withFile(f1, new Function2.X<InputStream, File, String>() { @Override public String xapply(InputStream in, File file) throws IOException { return IOUtils.readLines(in).get(0); } }); assertEquals(some("<?xml version=\"1.0\"?>"), r1); final File f2 = new File("i-do-not-exist"); final Option<String> r2 = withFile(f2, new Function2.X<InputStream, File, String>() { @Override public String xapply(InputStream in, File file) throws IOException { return IOUtils.readLines(in).get(0); } }); assertEquals(none(String.class), r2); }
public static <A> Option<A> withFile(File file, Function2<InputStream, File, A> f) { InputStream s = null; try { s = new FileInputStream(file); return some(f.apply(s, file)); } catch (FileNotFoundException ignore) { return none(); } finally { IoSupport.closeQuietly(s); } }
IoSupport { public static <A> Option<A> withFile(File file, Function2<InputStream, File, A> f) { InputStream s = null; try { s = new FileInputStream(file); return some(f.apply(s, file)); } catch (FileNotFoundException ignore) { return none(); } finally { IoSupport.closeQuietly(s); } } }
IoSupport { public static <A> Option<A> withFile(File file, Function2<InputStream, File, A> f) { InputStream s = null; try { s = new FileInputStream(file); return some(f.apply(s, file)); } catch (FileNotFoundException ignore) { return none(); } finally { IoSupport.closeQuietly(s); } } private  IoSupport(); }
IoSupport { public static <A> Option<A> withFile(File file, Function2<InputStream, File, A> f) { InputStream s = null; try { s = new FileInputStream(file); return some(f.apply(s, file)); } catch (FileNotFoundException ignore) { return none(); } finally { IoSupport.closeQuietly(s); } } 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); }
IoSupport { public static <A> Option<A> withFile(File file, Function2<InputStream, File, A> f) { InputStream s = null; try { s = new FileInputStream(file); return some(f.apply(s, file)); } catch (FileNotFoundException ignore) { return none(); } finally { IoSupport.closeQuietly(s); } } 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; }