Coverage Summary for Class: Flushables (com.google.common.io)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| Flushables | 0% (0/1) | 0% (0/4) | 0% (0/12) |
1 /* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.io; 16 17 import com.google.common.annotations.Beta; 18 import com.google.common.annotations.GwtIncompatible; 19 import java.io.Flushable; 20 import java.io.IOException; 21 import java.util.logging.Level; 22 import java.util.logging.Logger; 23 24 /** 25 * Utility methods for working with {@link Flushable} objects. 26 * 27 * @author Michael Lancaster 28 * @since 1.0 29 */ 30 @Beta 31 @GwtIncompatible 32 @ElementTypesAreNonnullByDefault 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 public static void flush(Flushable flushable, boolean swallowIOException) throws IOException { 52 try { 53 flushable.flush(); 54 } catch (IOException e) { 55 if (swallowIOException) { 56 logger.log(Level.WARNING, "IOException thrown while flushing Flushable.", e); 57 } else { 58 throw e; 59 } 60 } 61 } 62 63 /** 64 * Equivalent to calling {@code flush(flushable, true)}, but with no {@code IOException} in the 65 * signature. 66 * 67 * @param flushable the {@code Flushable} object to be flushed. 68 */ 69 public static void flushQuietly(Flushable flushable) { 70 try { 71 flush(flushable, true); 72 } catch (IOException e) { 73 logger.log(Level.SEVERE, "IOException should not have been thrown.", e); 74 } 75 } 76 }