/** * Closes the specified streams, suppressing any IOExceptions for inputstreams and readers. * Even if an I/O exception is thrown, all streams can be considered closed. * * @param streams the streams to close * * @throws IOException if an i/o exception occurs while closing any outputstream or writer */ public static void close( Closeable... streams ) throws IOException { close( streams, 0 ); }
private static void close( Closeable[] streams, int idx ) throws IOException { if( idx >= streams.length ) { return; // done } Closeable stream = streams[idx]; try { if( stream != null ) { if( stream instanceof Flushable ) { ((Flushable)stream).flush(); } stream.close(); } } catch( IOException ex ) { if( !(stream instanceof InputStream || stream instanceof Reader) ) { throw ex; // ignore io exceptions for input streams and readers } } finally { close( streams, idx + 1 ); } }