Utility methods for working with {@link Flushable} objects. @author Michael Lancaster @since 1.0
| 29 | * @since 1.0 |
| 30 | */ |
| 31 | @J2ktIncompatible |
| 32 | @GwtIncompatible |
| 33 | public final class Flushables { |
| 34 | private static final Logger logger = Logger.getLogger(Flushables.class.getName()); |
| 35 | |
| 36 | private Flushables() {} |
| 37 | |
| 38 | /** |
| 39 | * Flush a {@link Flushable}, with control over whether an {@code IOException} may be thrown. |
| 40 | * |
| 41 | * <p>If {@code swallowIOException} is true, then we don't rethrow {@code IOException}, but merely |
| 42 | * log it. |
| 43 | * |
| 44 | * @param flushable the {@code Flushable} object to be flushed. |
| 45 | * @param swallowIOException if true, don't propagate IO exceptions thrown by the {@code flush} |
| 46 | * method |
| 47 | * @throws IOException if {@code swallowIOException} is false and {@link Flushable#flush} throws |
| 48 | * an {@code IOException}. |
| 49 | * @see Closeables#close |
| 50 | */ |
| 51 | @SuppressWarnings("IdentifierName") // See Closeables.close |
| 52 | public static void flush(Flushable flushable, boolean swallowIOException) throws IOException { |
| 53 | try { |
| 54 | flushable.flush(); |
| 55 | } catch (IOException e) { |
| 56 | if (swallowIOException) { |
| 57 | logger.log(Level.WARNING, "IOException thrown while flushing Flushable.", e); |
| 58 | } else { |
| 59 | throw e; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Equivalent to calling {@code flush(flushable, true)}, but with no {@code IOException} in the |
| 66 | * signature. |
| 67 | * |
| 68 | * @param flushable the {@code Flushable} object to be flushed. |
| 69 | */ |
| 70 | @Beta |
| 71 | public static void flushQuietly(Flushable flushable) { |
| 72 | try { |
| 73 | flush(flushable, true); |
| 74 | } catch (IOException e) { |
| 75 | logger.log(Level.SEVERE, "IOException should not have been thrown.", e); |
| 76 | } |
| 77 | } |
| 78 | } |