@Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { Closeables.close(tarArchiveOutputStream, true); throw exc; } }
public static void closeQuietly(@Nullable Closeable closeable) { try { close(closeable, true); } catch (IOException e) {} }
public static void close(Iterator<?> iterator) { if (iterator instanceof Closeable) { try { Closeables.close((Closeable) iterator, false); } catch (IOException e) { LOGGER.log(Level.FINE, "Ignoring exception on CloseableIteratorAdapter.close()", e); } } }
/** * Close a closeable. * * @param closeable * closeable to close */ public static void close(@Nullable Closeable closeable) { if (null == closeable) { return; } try { Closeables.close(closeable, true); } catch (IOException e) { // no-op. the exception is swallowed. } }
/** * Closes the given {@link InputStream}, logging any {@code IOException} that's thrown rather than * propagating it. * * <p>While it's not safe in the general case to ignore exceptions that are thrown when closing an * I/O resource, it should generally be safe in the case of a resource that's being used only for * reading, such as an {@code InputStream}. Unlike with writable resources, there's no chance that * a failure that occurs when closing the stream indicates a meaningful problem such as a failure * to flush all bytes to the underlying resource. * * @param inputStream the input stream to be closed, or {@code null} in which case this method * does nothing * @since 17.0 */ public static void closeQuietly(@Nullable InputStream inputStream) { try { close(inputStream, true); } catch (IOException impossible) { throw new AssertionError(impossible); } }
/** * Closes the given {@link Reader}, logging any {@code IOException} that's thrown rather than * propagating it. * * <p>While it's not safe in the general case to ignore exceptions that are thrown when closing an * I/O resource, it should generally be safe in the case of a resource that's being used only for * reading, such as a {@code Reader}. Unlike with writable resources, there's no chance that a * failure that occurs when closing the reader indicates a meaningful problem such as a failure to * flush all bytes to the underlying resource. * * @param reader the reader to be closed, or {@code null} in which case this method does nothing * @since 17.0 */ public static void closeQuietly(@Nullable Reader reader) { try { close(reader, true); } catch (IOException impossible) { throw new AssertionError(impossible); } } }
@Override public void close() throws IOException { for (Closeable closeable : closeables) { Closeables.close(closeable, true); } }
@Override public void close() throws IOException { log.info("Stopping kafka 0.8 simple firehose"); stopped = true; for (PartitionConsumerWorker t : consumerWorkers) { Closeables.close(t, true); } } };
/** * Closes the given {@link Reader}, logging any {@code IOException} that's thrown rather than * propagating it. * * <p>While it's not safe in the general case to ignore exceptions that are thrown when closing * an I/O resource, it should generally be safe in the case of a resource that's being used only * for reading, such as a {@code Reader}. Unlike with writable resources, there's no chance that * a failure that occurs when closing the reader indicates a meaningful problem such as a failure * to flush all bytes to the underlying resource. * * @param reader the reader to be closed, or {@code null} in which case this method does nothing * @since 17.0 */ public static void closeQuietly(@Nullable Reader reader) { try { close(reader, true); } catch (IOException impossible) { throw new AssertionError(impossible); } } }
/** * Flushes and closes output stream. * @param stream the output stream * @throws IOException when there is a problems with file flush and close */ private static void flushAndCloseOutStream(OutputStream stream) throws IOException { if (stream != null) { Flushables.flush(stream, false); } Closeables.close(stream, false); }
@Override public void close() throws IOException { repeatingActivity.close(); Closeables.close(provider, true); }
/** * Closes the given {@link Reader}, logging any {@code IOException} that's thrown rather than * propagating it. * * <p>While it's not safe in the general case to ignore exceptions that are thrown when closing an * I/O resource, it should generally be safe in the case of a resource that's being used only for * reading, such as a {@code Reader}. Unlike with writable resources, there's no chance that a * failure that occurs when closing the reader indicates a meaningful problem such as a failure to * flush all bytes to the underlying resource. * * @param reader the reader to be closed, or {@code null} in which case this method does nothing * @since 17.0 */ public static void closeQuietly(@NullableDecl Reader reader) { try { close(reader, true); } catch (IOException impossible) { throw new AssertionError(impossible); } } }
/** * Closes the given {@link InputStream}, logging any {@code IOException} that's thrown rather than * propagating it. * * <p>While it's not safe in the general case to ignore exceptions that are thrown when closing an * I/O resource, it should generally be safe in the case of a resource that's being used only for * reading, such as an {@code InputStream}. Unlike with writable resources, there's no chance that * a failure that occurs when closing the stream indicates a meaningful problem such as a failure * to flush all bytes to the underlying resource. * * @param inputStream the input stream to be closed, or {@code null} in which case this method * does nothing * @since 17.0 */ public static void closeQuietly(@NullableDecl InputStream inputStream) { try { close(inputStream, true); } catch (IOException impossible) { throw new AssertionError(impossible); } }
private void doClose(Closeable closeable, boolean swallowException, boolean expectThrown) throws IOException { try { Closeables.close(closeable, swallowException); if (expectThrown) { fail("Didn't throw exception."); } } catch (IOException e) { if (!expectThrown) { fail("Threw exception"); } } verify(closeable).close(); } }
@Override public void run(Context context) throws IOException, InterruptedException { super.run(context); if (determineIntervals) { final Path outPath = config.makeIntervalInfoPath(); final OutputStream out = Utils.makePathAndOutputStream(context, outPath, config.isOverwriteFiles()); try { HadoopDruidIndexerConfig.JSON_MAPPER.writerWithType( new TypeReference<List<Interval>>() { } ).writeValue( out, intervals ); } finally { Closeables.close(out, false); } } } }
@Test public void testCloseAndFlushOutputStreamAfterCreatingHashCode() throws IOException { mockStatic(Closeables.class); doNothing().when(Closeables.class); Closeables.close(any(ObjectOutputStream.class), ArgumentMatchers.eq(false)); mockStatic(Flushables.class); doNothing().when(Flushables.class); Flushables.flush(any(ObjectOutputStream.class), ArgumentMatchers.eq(false)); final Configuration config = new DefaultConfiguration("myName"); final PropertyCacheFile cache = new PropertyCacheFile(config, "fileDoesNotExist.txt"); cache.load(); verifyStatic(Closeables.class, times(1)); Closeables.close(any(ObjectOutputStream.class), ArgumentMatchers.eq(false)); verifyStatic(Flushables.class, times(1)); Flushables.flush(any(ObjectOutputStream.class), ArgumentMatchers.eq(false)); }
@Test public void testFlushAndCloseCacheFileOutputStream() throws IOException { mockStatic(Closeables.class); doNothing().when(Closeables.class); Closeables.close(any(OutputStream.class), ArgumentMatchers.eq(false)); mockStatic(Flushables.class); doNothing().when(Flushables.class); Flushables.flush(any(OutputStream.class), ArgumentMatchers.eq(false)); final Configuration config = new DefaultConfiguration("myName"); final PropertyCacheFile cache = new PropertyCacheFile(config, temporaryFolder.newFile().getPath()); cache.put("CheckedFileName.java", System.currentTimeMillis()); cache.persist(); verifyStatic(Closeables.class, times(1)); Closeables.close(any(OutputStream.class), ArgumentMatchers.eq(false)); verifyStatic(Flushables.class, times(1)); Flushables.flush(any(OutputStream.class), ArgumentMatchers.eq(false)); }