List of usage examples for com.google.common.io Closer close
@Override public void close() throws IOException
From source file:org.glowroot.common.util.Version.java
private static @Nullable Manifest getManifest(@Nullable URL url) throws IOException { if (url == null) { return null; }/* w w w. ja v a 2s . c o m*/ // Closer is used to simulate Java 7 try-with-resources Closer closer = Closer.create(); try { InputStream manifestIn = closer.register(url.openStream()); return new Manifest(manifestIn); } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } }
From source file:com.skcraft.launcher.persistence.Persistence.java
/** * Save an object to file./*from w w w . j av a 2s .c o m*/ * * @param object the object * @throws java.io.IOException on save error */ public static void commit(@NonNull Object object) throws IOException { ByteSink sink; synchronized (bound) { sink = bound.get(object); if (sink == null) { throw new IOException("Cannot persist unbound object: " + object); } } Closer closer = Closer.create(); try { OutputStream os = closer.register(sink.openBufferedStream()); mapper.writeValue(os, object); } finally { closer.close(); } }
From source file:org.impressivecode.depress.mr.astcompare.utils.Utils.java
private static void saveFile(InputStream is, OutputStream os) throws IOException { Closer closer = Closer.create(); try {/*from w w w . java 2s . co m*/ InputStream in = closer.register(is); OutputStream out = closer.register(os); ByteStreams.copy(in, out); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
From source file:alluxio.util.CommonUtils.java
/** * Closes a closer and ignores the IOException if it throws one. * * @param closer the closer//from w w w . j a v a 2s. com */ public static void closeQuietly(Closer closer) { try { closer.close(); } catch (IOException e) { // Ignore. } }
From source file:com.github.fge.jackson.JsonLoader.java
/** * Read a {@link JsonNode} from a resource path. * * <p>This method first tries and loads the resource using {@link * Class#getResource(String)}; if not found, is tries and uses the context * classloader and if this is not found, this class's classloader.</p> * * <p>This method throws an {@link IOException} if the resource does not * exist.</p>//from w w w .ja v a2 s . com * * @param resource the path to the resource (<strong>must</strong> begin * with a {@code /}) * @return the JSON document at the resource * @throws IllegalArgumentException resource path does not begin with a * {@code /} * @throws IOException there was a problem loading the resource, or the JSON * document is invalid */ public static JsonNode fromResource(@Nonnull final String resource) throws IOException { Preconditions.checkNotNull(resource); Preconditions.checkArgument(resource.startsWith("/"), "resource path does not start with a '/'"); URL url; url = JsonLoader.class.getResource(resource); if (url == null) { final ClassLoader classLoader = Objects.firstNonNull(Thread.currentThread().getContextClassLoader(), JsonLoader.class.getClassLoader()); final String s = INITIAL_SLASH.matcher(resource).replaceFirst(""); url = classLoader.getResource(s); } if (url == null) throw new IOException("resource " + resource + " not found"); final Closer closer = Closer.create(); final JsonNode ret; final InputStream in; try { in = closer.register(url.openStream()); ret = READER.fromInputStream(in); } finally { closer.close(); } return ret; }
From source file:com.github.fge.jackson.JsonLoader.java
/** * Read a {@link JsonNode} from a file on the local filesystem. * * @param path the path (relative or absolute) to the file * @return the document in the file/*www . j ava 2 s . c om*/ * @throws IOException if this is not a file, if it cannot be read, etc. */ public static JsonNode fromPath(final String path) throws IOException { final Closer closer = Closer.create(); final JsonNode ret; final FileInputStream in; try { in = closer.register(new FileInputStream(path)); ret = READER.fromInputStream(in); } finally { closer.close(); } return ret; }
From source file:com.github.fge.jackson.JsonLoader.java
/** * Same as {@link #fromPath(String)}, but this time the user supplies the * {@link File} object instead/*from w w w . j ava 2 s . c om*/ * * @param file the File object * @return The document * @throws IOException in many cases! */ public static JsonNode fromFile(final File file) throws IOException { final Closer closer = Closer.create(); final JsonNode ret; final FileInputStream in; try { in = closer.register(new FileInputStream(file)); ret = READER.fromInputStream(in); } finally { closer.close(); } return ret; }
From source file:org.glowroot.agent.live.JvmTool.java
private static <T> T processAndClose(InputStream in, InputStreamProcessor<T> processor) throws IOException { Closer closer = Closer.create(); try {//from www. j av a 2 s .com closer.register(in); return processor.process(in); } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } }
From source file:com.sk89q.worldguard.six2five.util.SwingHelper.java
public static BufferedImage readIconImage(Class<?> clazz, String path) { Closer closer = Closer.create(); try {/*w ww. jav a 2 s. c o m*/ try { InputStream in = closer.register(clazz.getResourceAsStream(path)); if (in != null) { return ImageIO.read(in); } } finally { closer.close(); } } catch (IOException ignored) { } return null; }
From source file:org.apache.jackrabbit.oak.upgrade.cli.OakUpgrade.java
public static void migrate(MigrationCliArguments argumentParser) throws IOException { MigrationOptions options = argumentParser.getOptions(); StoreArguments stores = argumentParser.getStoreArguments(); Closer closer = Closer.create(); CliUtils.handleSigInt(closer);/*w ww . ja v a 2 s . c om*/ MigrationFactory factory = new MigrationFactory(options, stores, closer); try { if (stores.getSrcStore().isJcr2()) { upgrade(factory); } else { sidegrade(factory); } } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } }