use Closeable interface in IOUtils

This commit is contained in:
Hannes Janetzek 2013-09-20 13:50:11 +02:00
parent eab0f5e87a
commit 43de62f4bc
2 changed files with 66 additions and 18 deletions

View File

@ -0,0 +1,59 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.oscim.utils;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import org.oscim.backend.Log;
/**
* A utility class with IO-specific helper methods.
*/
public final class IOUtils {
/**
* Invokes the {@link Closeable#close()} method on the given object. If an
* {@link IOException} occurs during the
* method call, it will be caught and logged on level {@link Level#WARNING}.
*
* @param closeable
* the data source which should be closed (may be null).
*/
public static void closeQuietly(OutputStream closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (IOException e) {
Log.d(IOUtils.class.getName(), e.getMessage() + " " + e);
}
}
public static void closeQuietly(InputStream closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (IOException e) {
Log.d(IOUtils.class.getName(), e.getMessage() + " " + e);
}
}
private IOUtils() {
}
}

View File

@ -16,44 +16,33 @@ package org.oscim.utils;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.oscim.backend.Log;
/**
* A utility class with IO-specific helper methods.
*/
public final class IOUtils {
private static final Logger LOGGER = Logger.getLogger(IOUtils.class.getName());
/**
* Invokes the {@link Closeable#close()} method on the given object. If an {@link IOException} occurs during the
* Invokes the {@link Closeable#close()} method on the given object. If an
* {@link IOException} occurs during the
* method call, it will be caught and logged on level {@link Level#WARNING}.
*
*
* @param closeable
* the data source which should be closed (may be null).
*/
public static void closeQuietly(InputStream closeable) {
public static void closeQuietly(Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (IOException e) {
LOGGER.log(Level.FINE, e.getMessage(), e);
Log.d(IOUtils.class.getName(), e.getMessage() + " " + e);
}
}
public static void closeQuietly(OutputStream closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (IOException e) {
LOGGER.log(Level.FINE, e.getMessage(), e);
}
}
private IOUtils() {
throw new IllegalStateException();
}
}