summaryrefslogtreecommitdiff
path: root/src/Chapter4/util/TweetFileProcessor.java
blob: 9b6b99c89ddc1667a08b6d96e4bc31731031f399 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package Chapter4.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;

public class TweetFileProcessor implements Iterator<JSONObject>{
	
	protected BufferedReader fileBuffer;
	protected boolean endOfFile;
	protected String nextLine;
	
	public TweetFileProcessor(File f){
		
		endOfFile = false;
		
		InputStreamReader isr;
		BufferedReader br = null;
		try {
			isr = new InputStreamReader(new FileInputStream(f), "UTF-8");
			br = new BufferedReader(isr);
			nextLine = br.readLine();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			endOfFile = true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			endOfFile = true;
		} catch (IOException e) {
			e.printStackTrace();
			endOfFile = true;
		}
		finally{
			fileBuffer = br;
		}
	}

	@Override
	public boolean hasNext() {
		return !endOfFile;
	}

	@Override
	public JSONObject next() {
		JSONObject obj = null;
                try {
                    obj = new JSONObject(nextLine);
                } catch (JSONException ex) {
                    Logger.getLogger(TweetFileProcessor.class.getName()).log(Level.SEVERE, null, ex);
                }
		try {
			nextLine = fileBuffer.readLine();
			if(nextLine == null){
				endOfFile = true;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
		return obj;
	}

	@Override
	public void remove() throws UnsupportedOperationException{
		throw new UnsupportedOperationException();
	}
}