001/**
002 * Copyright (c) 2012, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.twitter.collection;
031
032import java.io.BufferedInputStream;
033import java.io.DataInput;
034import java.io.DataOutput;
035import java.io.IOException;
036import java.io.InputStream;
037import java.io.PrintWriter;
038import java.util.HashMap;
039import java.util.Scanner;
040
041import org.openimaj.io.ReadWriteable;
042import org.openimaj.twitter.collection.StreamJSONStatusList.ReadableWritableJSON;
043import org.openimaj.util.list.AbstractStreamBackedList;
044
045
046import com.google.gson.Gson;
047
048
049/**
050 * A list of json maps
051 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
052 *
053 */
054public class StreamJSONStatusList extends AbstractStreamBackedList<ReadableWritableJSON> {
055
056        /**
057         * a readable json hashmap
058         * @author Sina Samangooei (ss@ecs.soton.ac.uk)
059         *
060         */
061        public static class ReadableWritableJSON extends HashMap<String,Object> implements ReadWriteable{
062                /**
063                 *
064                 */
065                private static final long serialVersionUID = 6001988896541110142L;
066                /**
067                 *
068                 */
069                public ReadableWritableJSON() {
070                }
071                private transient Gson gson = new Gson();
072                @Override
073                public void readASCII(Scanner in) throws IOException {
074                        if(in == null) return;
075                        ReadableWritableJSON inner = gson.fromJson(in.nextLine(), ReadableWritableJSON.class);
076                        for (java.util.Map.Entry<String, Object> entry: inner.entrySet()) {
077                                this.put(entry.getKey(), entry.getValue());
078                        }
079                }
080
081                @Override
082                public String asciiHeader() {
083                        return "";
084                }
085
086                @Override
087                public void readBinary(DataInput in) throws IOException {
088                        ReadableWritableJSON inner = gson.fromJson(in.readUTF(), ReadableWritableJSON.class);
089                        for (java.util.Map.Entry<String, Object> entry: inner.entrySet()) {
090                                this.put(entry.getKey(), entry.getValue());
091                        }
092
093                }
094
095                @Override
096                public byte[] binaryHeader() {
097                        return "".getBytes();
098                }
099
100                @Override
101                public void writeASCII(PrintWriter out) throws IOException {
102                        gson.toJson(this, out);
103                }
104
105                @Override
106                public void writeBinary(DataOutput out) throws IOException {
107                        out.writeUTF(gson.toJson(this));
108                }
109        }
110
111        protected StreamJSONStatusList(InputStream stream, int size,boolean isBinary, int headerLength, int recordLength,String charset) throws IOException{
112                super(stream, size, isBinary, headerLength, recordLength,ReadableWritableJSON.class,charset);
113        }
114
115
116        /**
117         * Construct a new StreamTwitterStatusList from the given input stream.
118         *
119         * @param stream the input stream
120         * @param nTweets of tweets to read from this stream
121         *
122         * @return a new list
123         * @throws IOException if an error occurs reading from the stream
124         */
125        public static StreamJSONStatusList read(InputStream stream, int nTweets) throws IOException {
126                return read(new BufferedInputStream(stream), nTweets);
127        }
128
129        /**
130         * Construct a new StreamTwitterStatusList from the given input stream.
131         *
132         * @param stream the input stream
133         *
134         * @return a new list
135         * @throws IOException if an error occurs reading from the stream
136         */
137        public static StreamJSONStatusList read(InputStream stream) throws IOException {
138                return read(new BufferedInputStream(stream), -1);
139        }
140
141
142        /**
143         * Construct a new StreamTwitterStatusList from the given input stream.
144         *
145         * @param stream the input stream
146         * @param nTweets of tweets to read from this stream
147         * @param charset
148         *
149         * @return a new list
150         * @throws IOException if an error occurs reading from the stream
151         */
152        public static StreamJSONStatusList read(InputStream stream, int nTweets,String charset) throws IOException {
153                return read(new BufferedInputStream(stream), nTweets,charset);
154        }
155
156        /**
157         * Construct a new StreamTwitterStatusList from the given input stream.
158         *
159         * @param stream the input stream
160         * @param charset the charset of the underlying stream
161         *
162         * @return a new list
163         * @throws IOException if an error occurs reading from the stream
164         */
165        public static StreamJSONStatusList read(InputStream stream, String charset) throws IOException {
166                return read(new BufferedInputStream(stream), -1,charset);
167        }
168
169
170
171        /**
172         * Construct a new StreamTwitterStatusList from the given input stream.
173         *
174         * @param stream the input stream
175         * @param nTweets of tweets to read from this stream
176         * @return a new list
177         * @throws IOException if an error occurs reading from the stream
178         */
179        public static StreamJSONStatusList read(BufferedInputStream stream, int nTweets) throws IOException {
180                boolean isBinary = false;
181
182                //read header
183                int size = nTweets;
184                int headerLength = 0;
185                int recordLength = -1;
186
187                return new StreamJSONStatusList(stream, size, isBinary, headerLength, recordLength,"UTF-8");
188        }
189
190        /**
191         * Construct a new StreamTwitterStatusList from the given input stream.
192         *
193         * @param stream the input stream
194         * @param nTweets of tweets to read from this stream
195         * @param charset the charset to read the stream as
196         *
197         * @return a new list
198         * @throws IOException if an error occurs reading from the stream
199         */
200        public static StreamJSONStatusList read(BufferedInputStream stream, int nTweets, String charset) throws IOException {
201                boolean isBinary = false;
202
203                //read header
204                int size = nTweets;
205                int headerLength = 0;
206                int recordLength = -1;
207
208                return new StreamJSONStatusList(stream, size, isBinary, headerLength, recordLength,charset);
209        }
210}