]> source.dussan.org Git - jackcess.git/commitdiff
add closeQuietly util method
authorJames Ahlborn <jtahlborn@yahoo.com>
Tue, 10 Sep 2013 01:26:53 +0000 (01:26 +0000)
committerJames Ahlborn <jtahlborn@yahoo.com>
Tue, 10 Sep 2013 01:26:53 +0000 (01:26 +0000)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@796 f203690c-595d-4dc9-a70b-905162fa7fd2

src/main/java/com/healthmarketscience/jackcess/impl/ByteUtil.java
src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java
src/main/java/com/healthmarketscience/jackcess/impl/GeneralLegacyIndexCodes.java
src/main/java/com/healthmarketscience/jackcess/impl/complex/AttachmentColumnInfoImpl.java
src/main/java/com/healthmarketscience/jackcess/util/ExportUtil.java
src/main/java/com/healthmarketscience/jackcess/util/ImportUtil.java

index f851788e4e4c5e29ae21a16c8ec2d2292b70b450..c988ca089ba7a6df9853617823a0ac0c43a6b6a7 100644 (file)
@@ -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.
index 5c28d3693838a47d951b65f1348456fdeb2f8b59..99b938a38446538b48cee11196ccbfe0883893e3 100644 (file)
@@ -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);
       }
     }
   }
index 4bdfeeb65f952414657e6994b0b101e5723666e6..e755068206b808f680f184f1b883dbbc9500f4e4 100644 (file)
@@ -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;
index 69c43dfb6d904c23907c86f1fea13d923ab34c81..ae5df69417420577acd667e7ba8a6a5a0e80b697 100644 (file)
@@ -404,13 +404,7 @@ public class AttachmentColumnInfoImpl extends ComplexColumnInfoImpl<Attachment>
         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<Attachment>
         return dataStream.toByteArray();
 
       } finally {
-        if(contentStream != null) {
-          try {
-            contentStream.close();
-          } catch(IOException e) {
-            // ignored
-          }
-        }
+        ByteUtil.closeQuietly(contentStream);
         if(deflater != null) {
-            deflater.end();
+          deflater.end();
         }
       }
     }
index 125c8df7a9e8874acac14ceb9b3e241205d831ad..dc1fff4f497c7cd974143ead98f692a2f1ce5c75 100644 (file)
@@ -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);
     }
   }
 
index c34e7ff055d636eb0b6ddd7d28c41bbfaf1b6d57..ebeb4807344166fe15b60cb93a39b5941180d461 100644 (file)
@@ -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);
     }
   }