Java Stream Close close(final Closeable resource, final String resourceId)

Here you can find the source of close(final Closeable resource, final String resourceId)

Description

Close the passed resource and care about possible problems.

License

Open Source License

Parameter

Parameter Description
resource the resource to close (or null)
resourceId a string to identify the resource, will used in case of error for the error message only.

Declaration

public static void close(final Closeable resource, final String resourceId) 

Method Source Code


//package com.java2s;
/* scenarioo-server//from   w w  w  . j av a2  s  . c  o m
 * Copyright (C) 2014, scenarioo.org Development Team
 * 
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

import java.io.Closeable;
import java.io.IOException;

public class Main {
    /**
     * Close the passed resource and care about possible problems.
     * 
     * Is null pointer safe, by handling passed null parameter. Will throw a runtime exception if resource is not
     * closeable.
     * 
     * @param resource
     *            the resource to close (or null)
     * @param resourceId
     *            a string to identify the resource, will used in case of error for the error message only.
     */
    public static void close(final Closeable resource, final String resourceId) {
        if (resource != null) {
            try {
                resource.close();
            } catch (IOException e) {
                throw new RuntimeException(
                        "Could not close resource properly for unknown reason (unexpected exception): "
                                + resourceId,
                        e);
            }
        }
    }
}

Related

  1. close(Collection inCloseables)
  2. close(DataOutputStream out)
  3. close(File file)
  4. close(FileReader fr)
  5. close(FileReader reader, BufferedReader br)
  6. close(final Exception e, final Closeable... objects)
  7. close(final InputStream anInputStream)
  8. close(final InputStream is)
  9. close(final InputStream stream)