blob: 15ddceaf67f22fed55f99da19ed2297ea1cdc94f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
package io;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import data.Tweet;
import data.TwitterJsonDeserializer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* Reads tweets from an InputStream.
*
* @author Peter Wu
*/
public class TweetReader implements ITweetReader {
private final InputStream is;
private final BufferedReader reader;
private final Gson gson;
public TweetReader(InputStream is) {
if (is == null) {
throw new NullPointerException();
}
this.is = is;
reader = new BufferedReader(new InputStreamReader(is));
gson = TwitterJsonDeserializer.getGsonBuilder().create();
}
@Override
public Tweet getTweet() throws IOException {
String line = reader.readLine();
Tweet tweet = null;
if (line != null) {
try {
tweet = gson.fromJson(line, Tweet.class);
} catch (JsonSyntaxException ex) {
debugTweet(line, ex);
throw ex;
}
}
return tweet;
}
@Override
public void close() {
try {
is.close();
} catch (IOException ex) {
}
}
private void debugTweet(String line, JsonSyntaxException ex) {
System.err.println("Faulty line: " + line);
ex.printStackTrace();
}
}
|