From c9ef24035ac3d7ad53881c35c12a2fe17ad0c415 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Fri, 2 May 2014 19:49:01 +0200 Subject: Abort if file cannot be read, don't leak on error --- src/io/DataWriter.java | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/io/DataWriter.java b/src/io/DataWriter.java index 2e90097..6a5e3fe 100644 --- a/src/io/DataWriter.java +++ b/src/io/DataWriter.java @@ -12,6 +12,7 @@ import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.io.Charsets; +import org.apache.commons.io.IOUtils; import org.json.JSONException; import org.json.JSONObject; import provider.ResultListener; @@ -56,10 +57,20 @@ public class DataWriter implements ResultListener { public DataWriter(final String profilesName, final String tweetsName) throws IOException { m_profile = getStore(profilesName); - m_profileIdSet = readIds(m_profile); - + try { + m_profileIdSet = readIds(m_profile); + } catch (IOException ex) { + m_profile.close(); + throw ex; + } m_tweet = getStore(profilesName); - m_tweetIdSet = readIds(m_tweet); + try { + m_tweetIdSet = readIds(m_tweet); + } catch (IOException ex) { + m_profile.close(); + m_tweet.close(); + throw ex; + } } /** @@ -112,15 +123,10 @@ public class DataWriter implements ResultListener { // ignore, file will be created if necessary. } catch (JSONException ex) { getLogger().log(Level.WARNING, store.getFileName() - + ": File is only partially processed", ex); + + ": Corrupt file?", ex); + throw new IOException(ex); } finally { - try { - if (is != null) { - is.close(); - } - } catch (IOException ex) { - getLogger().log(Level.WARNING, "Cannot close input file", ex); - } + IOUtils.closeQuietly(is); } return idSet; } -- cgit v1.2.1