summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-05-02 17:04:24 +0200
committerPeter Wu <peter@lekensteyn.nl>2014-05-02 17:04:24 +0200
commitc112dcf584048d7f94c9cf6136105b9f6dc83215 (patch)
tree82fbdb124f62cae0bc4ed7168fdab83d1010c154
parent125d8d251de88d2522a849e02b92071177ba00ae (diff)
downloadTwitterDataAnalytics-c112dcf584048d7f94c9cf6136105b9f6dc83215.tar.gz
Don't ignore IOException stupid!
Files may not be accessible if they are read-only, or if the directory is read-only, or ... whatever. So, let the caller handle IOException in DataWriter. Return a set if it can be read. Do some misc cleanups (remove unused imports, fix docs, remove obsolete TODOs).
-rw-r--r--src/io/DataWriter.java57
-rw-r--r--src/main/TweetShell.java15
2 files changed, 33 insertions, 39 deletions
diff --git a/src/io/DataWriter.java b/src/io/DataWriter.java
index c26dbf3..42d69f8 100644
--- a/src/io/DataWriter.java
+++ b/src/io/DataWriter.java
@@ -39,28 +39,26 @@ public class DataWriter implements ResultListener {
* the buffer of profile ids that already exist.
*/
private final Set<Long> m_profileIdSet;
-
+
public final static String CFG_PROFILE_FILENAME = "profiles-filename";
- public final static String CFG_TWEETS_FILENAME = "tweets-filename";
+ public final static String CFG_TWEETS_FILENAME = "tweets-filename";
/**
* Opens a stream to every single file that data will be streamed to.
+ *
* @param profilesName The file to write the profiles to.
* @param tweetsName The file to write the tweets to.
+ * @throws java.io.IOException if the files cannot be read or written.
*/
- public DataWriter(final String profilesName, final String tweetsName) {
- try {
- m_profileIdSet = readIds(profilesName);
- m_profileWriter = new FileWriter(profilesName, true);
+ public DataWriter(final String profilesName, final String tweetsName)
+ throws IOException {
+ m_profileIdSet = readIds(profilesName);
+ m_profileWriter = new FileWriter(profilesName, true);
- m_tweetIdSet = readIds(tweetsName);
- m_tweetWriter = new FileWriter(tweetsName, true);
- } catch (IOException ex) {
- // This should not happen.
- throw new RuntimeException(ex.getMessage());
- }
+ m_tweetIdSet = readIds(tweetsName);
+ m_tweetWriter = new FileWriter(tweetsName, true);
}
-
+
public void close() {
try {
m_tweetWriter.close();
@@ -82,18 +80,15 @@ public class DataWriter implements ResultListener {
/**
* Read the current existing tweetName and profileName filenames and fill
- * the existing id set, it will create the file when it doesn't exist.
+ * the existing id set.
*
- * @param filename The file to parse
- * @return The set of ids
+ * @param filename The file to parse.
+ * @return The set of ids, may be empty if the fill does not exist.
*/
private Set readIds(String filename) throws IOException {
+ Set<Long> idSet = new HashSet<>();
try {
Scanner reader = new Scanner(new File(filename));
-
- // TODO: Read the file JSON objects and parse the ids.
- Set idSet = new HashSet();
-
// parse each line into a JSONObject, read the id and add it to
// the set of ids.
while (reader.hasNext()) {
@@ -101,29 +96,23 @@ public class DataWriter implements ResultListener {
long id = obj.getLong("id");
idSet.add(id);
}
-
- return idSet;
} catch (FileNotFoundException ex) {
- // File does not exist, so create one.
- File file = new File(filename);
- // Return value should always be true.
- file.createNewFile();
+ // ignore, file will be created if necessary.
} catch (JSONException ex) {
- getLogger().log(Level.SEVERE, null, ex);
+ getLogger().log(Level.WARNING, filename
+ + ": File is only partially processed", ex);
}
-
- // return the empty set.
- return new HashSet();
+ return idSet;
}
/**
* Writes the JSONObject to a writer and update the idSet.
- *
+ *
* @param obj The object to write.
* @param writer The writer object to append the object to.
* @param idSet The id set to add the obj id to.
*/
- private void writeObject(JSONObject obj, FileWriter writer, Set idSet) {
+ private void writeObject(JSONObject obj, FileWriter writer, Set<Long> idSet) {
try {
long id = obj.getLong("id");
@@ -133,11 +122,11 @@ public class DataWriter implements ResultListener {
writer.write(obj.toString() + "\n");
idSet.add(id);
} catch (IOException ex) {
- getLogger().log(Level.SEVERE, null, ex);
+ getLogger().log(Level.WARNING, "Cannot write to file", ex);
}
}
} catch (JSONException ex) {
- getLogger().log(Level.SEVERE, null, ex);
+ getLogger().log(Level.WARNING, "ID not found?!", ex);
}
}
diff --git a/src/main/TweetShell.java b/src/main/TweetShell.java
index 3ab1397..a5114e2 100644
--- a/src/main/TweetShell.java
+++ b/src/main/TweetShell.java
@@ -377,11 +377,16 @@ public class TweetShell implements TwitterApi.PinSupplier {
String profilesFilename = config.getProperty(DataWriter.CFG_PROFILE_FILENAME);
String tweetsFilename = config.getProperty(DataWriter.CFG_TWEETS_FILENAME);
-
- resultListeners.register(new DataWriter(profilesFilename, tweetsFilename));
-
- // save the changes to the config.
- config.save();
+ try {
+ DataWriter dw = new DataWriter(profilesFilename, tweetsFilename);
+ resultListeners.register(dw);
+ // save the changes to the config.
+ config.save();
+ } catch (IOException ex) {
+ System.err.println("Could not open file for storing tweets:");
+ System.err.println(ex.getMessage());
+ return false;
+ }
} else if (rlCls == StreamHandler.class) {
resultListeners.register(new StreamHandler());
}