X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=inline;f=src%2Fmain%2Fjava%2Fde%2Ftodesbaum%2Futil%2Fio%2FCloser.java;fp=src%2Fmain%2Fjava%2Fde%2Ftodesbaum%2Futil%2Fio%2FCloser.java;h=67f7c57654b93fecf9e7c9922f46bc8fb6e780be;hb=0e88169c3e8decfcd99f39f5ecf3a85df50c3fca;hp=0000000000000000000000000000000000000000;hpb=953de352675a4ad91fe307d816a4ea7780c94274;p=jSite.git diff --git a/src/main/java/de/todesbaum/util/io/Closer.java b/src/main/java/de/todesbaum/util/io/Closer.java new file mode 100644 index 0000000..67f7c57 --- /dev/null +++ b/src/main/java/de/todesbaum/util/io/Closer.java @@ -0,0 +1,189 @@ +/* + * jSite - Closer.java - Copyright © 2006–2012 David Roden + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 2 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package de.todesbaum.util.io; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.Writer; +import java.net.ServerSocket; +import java.net.Socket; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +/** + * Helper class that can close all kinds of resources without throwing exception + * so that clean-up code can be written with less code. All methods check that + * the given resource is not null before invoking the close() + * method of the respective type. + * + * @author David ‘Bombe&squo; + * Roden + * @version $Id$ + */ +public class Closer { + + /** + * Closes the given result set. + * + * @param resultSet + * The result set to close + * @see ResultSet#close() + */ + public static void close(ResultSet resultSet) { + if (resultSet != null) { + try { + resultSet.close(); + } catch (SQLException ioe1) { + } + } + } + + /** + * Closes the given statement. + * + * @param statement + * The statement to close + * @see Statement#close() + */ + public static void close(Statement statement) { + if (statement != null) { + try { + statement.close(); + } catch (SQLException ioe1) { + } + } + } + + /** + * Closes the given connection. + * + * @param connection + * The connection to close + * @see Connection#close() + */ + public static void close(Connection connection) { + if (connection != null) { + try { + connection.close(); + } catch (SQLException ioe1) { + } + } + } + + /** + * Closes the given server socket. + * + * @param serverSocket + * The server socket to close + * @see ServerSocket#close() + */ + public static void close(ServerSocket serverSocket) { + if (serverSocket != null) { + try { + serverSocket.close(); + } catch (IOException ioe1) { + } + } + } + + /** + * Closes the given socket. + * + * @param socket + * The socket to close + * @see Socket#close() + */ + public static void close(Socket socket) { + if (socket != null) { + try { + socket.close(); + } catch (IOException ioe1) { + } + } + } + + /** + * Closes the given input stream. + * + * @param inputStream + * The input stream to close + * @see InputStream#close() + */ + public static void close(InputStream inputStream) { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException ioe1) { + } + } + } + + /** + * Closes the given output stream. + * + * @param outputStream + * The output stream to close + * @see OutputStream#close() + */ + public static void close(OutputStream outputStream) { + if (outputStream != null) { + try { + outputStream.close(); + } catch (IOException ioe1) { + } + } + } + + /** + * Closes the given reader. + * + * @param reader + * The reader to close + * @see Reader#close() + */ + public static void close(Reader reader) { + if (reader != null) { + try { + reader.close(); + } catch (IOException ioe1) { + } + } + } + + /** + * Closes the given writer. + * + * @param writer + * The write to close + * @see Writer#close() + */ + public static void close(Writer writer) { + if (writer != null) { + try { + writer.close(); + } catch (IOException ioe1) { + } + } + } + +}