/** Load properties from a stream. Close the stream after reading. */ public static Properties loadPropertiesFromStream(final InputStream stream) { return withResource(stream, new Function.X<InputStream, Properties>() { @Override public Properties xapply(InputStream in) throws Exception { final Properties p = new Properties(); p.load(in); return p; } }); }
/** To be used in unit tests. */ public static MediaPackage loadFromClassPath(String path) { return withResource(MediaPackageSupport.class.getResourceAsStream(path), new Function.X<InputStream, MediaPackage>() { @Override public MediaPackage xapply(InputStream is) throws MediaPackageException { return MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().loadFromXml(is); } }); }
/** * Read a SMIL document from a string. * * @throws java.io.IOException * in case of any IO error * @throws org.xml.sax.SAXException * in case of a SAX related error */ public static SMILDocument readSmil(String smil) throws IOException, SAXException { return withResource(IOUtils.toInputStream(smil, "UTF-8"), parseSmilFn); }
/** Load a text file from the class path using the class loader of the given class. */ public static Option<String> loadTxtFromClassPath(final String resource, final Class<?> clazz) { return withResource(clazz.getResourceAsStream(resource), new Function<InputStream, Option<String>>() { @Override public Option<String> apply(InputStream is) { try { return some(IOUtils.toString(is)); } catch (Exception e) { logger.warn("Cannot load resource " + resource + " from classpath"); return none(); } } }); }
@Override public void run() { try { running.countDown(); ready.await(); // also save a reference to the reader to able to close it in stopReading // otherwise the read loop may continue reading from the buffer reader = new BufferedReader(new InputStreamReader(stream)); IoSupport.withResource(reader, consumeBuffered); finished.countDown(); } catch (InterruptedException e) { chuck(e); } }
/** * Serialize and deserialize an object. To test serializability. */ public static <A extends Serializable> A serializeDeserialize(final A a) { final ByteArrayOutputStream out = new ByteArrayOutputStream(); try { withResource( new ObjectOutputStream(out), new FnX<ObjectOutputStream, Unit>() { @Override public Unit applyX(ObjectOutputStream out) throws Exception { out.writeObject(a); return Unit.unit; } }); return withResource( new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())), new FnX<ObjectInputStream, A>() { @Override public A applyX(ObjectInputStream in) throws Exception { return (A) in.readObject(); } }); } catch (IOException e) { return Prelude.chuck(e); } } }
/** Load a properties file from the classpath using the class loader of the given class. */ public static Properties loadPropertiesFromClassPath(final String resource, final Class<?> clazz) { for (InputStream in : openClassPathResource(resource, clazz)) { return withResource(in, new Function<InputStream, Properties>() { @Override public Properties apply(InputStream is) { final Properties p = new Properties(); try { p.load(is); } catch (Exception e) { throw new Error("Cannot load resource " + resource + "@" + clazz); } return p; } }); } return chuck(new FileNotFoundException(resource + " does not exist")); }