001package org.openimaj.picslurper; 002 003import java.io.BufferedReader; 004import java.io.IOException; 005import java.io.InputStream; 006import java.io.InputStreamReader; 007 008import org.apache.log4j.Logger; 009import org.openimaj.tools.FileToolsUtil; 010 011import twitter4j.internal.json.z_T4JInternalJSONImplFactory; 012import twitter4j.internal.org.json.JSONObject; 013 014/** 015 * Single threaded read an input stream and hand to a consumer 016 * 017 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 018 * 019 */ 020public class InputStreamFeeder implements StatusFeeder { 021 private static final Logger logger = Logger 022 .getLogger(InputStreamFeeder.class); 023 private z_T4JInternalJSONImplFactory factory; 024 025 /** 026 * Initialise a feeder on a slurper 027 * 028 * @param slurper 029 * @throws IOException 030 */ 031 public InputStreamFeeder(PicSlurper slurper) throws IOException { 032 factory = new z_T4JInternalJSONImplFactory(null); 033 if (FileToolsUtil.isStdin(slurper)) { 034 slurper.stdin = true; 035 } else { 036 slurper.inputFiles = FileToolsUtil.validateLocalInput(slurper); 037 slurper.fileIterator = slurper.inputFiles.iterator(); 038 } 039 } 040 041 @Override 042 public void feedStatus(final PicSlurper slurper) throws IOException { 043 for (final InputStream inStream : slurper) { 044 final BufferedReader reader = new BufferedReader(new InputStreamReader( 045 inStream)); 046 String line = null; 047 while ((line = reader.readLine()) != null) { 048 try { 049 slurper.handleStatus(factory.createStatus(new JSONObject(line))); 050 } catch (final Exception e) { 051 logger.error("Failed transforming status"); 052 } 053 } 054 } 055 } 056 057}