diff options
-rw-r--r-- | src/io/DataWriter.java (renamed from src/io/OutputStream.java) | 35 |
1 files changed, 21 insertions, 14 deletions
diff --git a/src/io/OutputStream.java b/src/io/DataWriter.java index 5a92c59..900a467 100644 --- a/src/io/OutputStream.java +++ b/src/io/DataWriter.java @@ -19,7 +19,7 @@ import provider.TweetListener; * * @author Maurice Laveaux */ -public class OutputStream implements ProfileListener, TweetListener { +public class DataListener implements ProfileListener, TweetListener { /** * The writer for the tweet stream. @@ -42,16 +42,16 @@ public class OutputStream implements ProfileListener, TweetListener { private final Set<Long> m_profileIdSet; private static final String profilesName = "profiles.txt"; - private static final String tweetsName = "tweets.txt"; + private static final String tweetsName = "tweets.txt"; /** * Opens a stream to every single file that data will be streamed to. */ - public OutputStream() { + public DataListener() { try { m_profileIdSet = readIds(profilesName); m_profileWriter = new FileWriter(profilesName, true); - + m_tweetIdSet = readIds(tweetsName); m_tweetWriter = new FileWriter(tweetsName, true); } catch (IOException ex) { @@ -72,7 +72,7 @@ public class OutputStream implements ProfileListener, TweetListener { /** * Read the current existing tweetName and profileName filenames and fill - * the existing id set. + * the existing id set, it will create the file when it doesn't exist. * * @param filename The file to parse * @return The set of ids @@ -80,20 +80,20 @@ public class OutputStream implements ProfileListener, TweetListener { private Set readIds(String filename) throws IOException { 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()) { + while (reader.hasNext()) { JSONObject obj = new JSONObject(reader.nextLine()); long id = obj.getLong("id"); idSet.add(id); } - + return idSet; - } catch (FileNotFoundException ex) { + } catch (FileNotFoundException ex) { // File does not exist, so create one. File file = new File(filename); // Return value should always be true. @@ -101,11 +101,18 @@ public class OutputStream implements ProfileListener, TweetListener { } catch (JSONException ex) { getLogger().log(Level.SEVERE, null, ex); } - + // return the empty set. return new HashSet(); } - + + /** + * 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) { try { long id = obj.getLong("id"); @@ -121,10 +128,10 @@ public class OutputStream implements ProfileListener, TweetListener { } } catch (JSONException ex) { getLogger().log(Level.SEVERE, null, ex); - } + } } private Logger getLogger() { - return Logger.getLogger(OutputStream.class.getName()); + return Logger.getLogger(DataListener.class.getName()); } } |