From 2e418ff2cb2d4452812886a0f29b8652494fc077 Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Tue, 10 Sep 2013 01:26:53 +0000 Subject: [PATCH] add closeQuietly util method git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@796 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../jackcess/impl/ByteUtil.java | 12 ++++++++++++ .../jackcess/impl/DatabaseImpl.java | 12 ++---------- .../jackcess/impl/GeneralLegacyIndexCodes.java | 8 +------- .../impl/complex/AttachmentColumnInfoImpl.java | 18 +++--------------- .../jackcess/util/ExportUtil.java | 8 +------- .../jackcess/util/ImportUtil.java | 9 ++------- 6 files changed, 21 insertions(+), 46 deletions(-) diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/ByteUtil.java b/src/main/java/com/healthmarketscience/jackcess/impl/ByteUtil.java index f851788..c988ca0 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/ByteUtil.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/ByteUtil.java @@ -27,6 +27,7 @@ King of Prussia, PA 19406 package com.healthmarketscience.jackcess.impl; +import java.io.Closeable; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStream; @@ -607,6 +608,17 @@ public final class ByteUtil { return newArr; } + /** + * Closes the given Closeable if non-null, swallows any IOExceptions. + */ + public static void closeQuietly(Closeable c) { + if(c != null) { + try { + c.close(); + } catch(IOException ignored) {} + } + } + /** * Utility byte stream similar to ByteArrayOutputStream but with extended * accessibility to the bytes. diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java index 5c28d36..99b938a 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java @@ -391,11 +391,7 @@ public class DatabaseImpl implements Database } finally { if(!success && closeChannel) { // something blew up, shutdown the channel (quietly) - try { - channel.close(); - } catch(Exception ignored) { - // we don't care - } + ByteUtil.closeQuietly(channel); } } } @@ -449,11 +445,7 @@ public class DatabaseImpl implements Database } finally { if(!success && closeChannel) { // something blew up, shutdown the channel (quietly) - try { - channel.close(); - } catch(Exception ignored) { - // we don't care - } + ByteUtil.closeQuietly(channel); } } } diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/GeneralLegacyIndexCodes.java b/src/main/java/com/healthmarketscience/jackcess/impl/GeneralLegacyIndexCodes.java index 4bdfeeb..e755068 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/GeneralLegacyIndexCodes.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/GeneralLegacyIndexCodes.java @@ -347,13 +347,7 @@ public class GeneralLegacyIndexCodes { throw new RuntimeException("failed loading index codes file " + codesFilePath, e); } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException ex) { - // ignored - } - } + ByteUtil.closeQuietly(reader); } return values; diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/complex/AttachmentColumnInfoImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/complex/AttachmentColumnInfoImpl.java index 69c43df..ae5df69 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/complex/AttachmentColumnInfoImpl.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/complex/AttachmentColumnInfoImpl.java @@ -404,13 +404,7 @@ public class AttachmentColumnInfoImpl extends ComplexColumnInfoImpl return tmpBytes; } finally { - if(contentStream != null) { - try { - contentStream.close(); - } catch(IOException e) { - // ignored - } - } + ByteUtil.closeQuietly(contentStream); } } @@ -465,15 +459,9 @@ public class AttachmentColumnInfoImpl extends ComplexColumnInfoImpl return dataStream.toByteArray(); } finally { - if(contentStream != null) { - try { - contentStream.close(); - } catch(IOException e) { - // ignored - } - } + ByteUtil.closeQuietly(contentStream); if(deflater != null) { - deflater.end(); + deflater.end(); } } } diff --git a/src/main/java/com/healthmarketscience/jackcess/util/ExportUtil.java b/src/main/java/com/healthmarketscience/jackcess/util/ExportUtil.java index 125c8df..dc1fff4 100644 --- a/src/main/java/com/healthmarketscience/jackcess/util/ExportUtil.java +++ b/src/main/java/com/healthmarketscience/jackcess/util/ExportUtil.java @@ -220,13 +220,7 @@ public class ExportUtil { exportWriter(db, tableName, out, header, delim, quote, filter); out.close(); } finally { - if (out != null) { - try { - out.close(); - } catch (Exception ex) { - LOG.warn("Could not close file " + f.getAbsolutePath(), ex); - } - } + ByteUtil.closeQuietly(out); } } diff --git a/src/main/java/com/healthmarketscience/jackcess/util/ImportUtil.java b/src/main/java/com/healthmarketscience/jackcess/util/ImportUtil.java index c34e7ff..ebeb480 100644 --- a/src/main/java/com/healthmarketscience/jackcess/util/ImportUtil.java +++ b/src/main/java/com/healthmarketscience/jackcess/util/ImportUtil.java @@ -46,6 +46,7 @@ import com.healthmarketscience.jackcess.DataType; import com.healthmarketscience.jackcess.Database; import com.healthmarketscience.jackcess.Table; import com.healthmarketscience.jackcess.TableBuilder; +import com.healthmarketscience.jackcess.impl.ByteUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -315,13 +316,7 @@ public class ImportUtil return importReader(in, db, name, delim, quote, filter, useExistingTable, header); } finally { - if (in != null) { - try { - in.close(); - } catch (IOException ex) { - LOG.warn("Could not close file " + f.getAbsolutePath(), ex); - } - } + ByteUtil.closeQuietly(in); } } -- 2.39.5