Created
February 21, 2012 18:10
-
-
Save pkulak/1877906 to your computer and use it in GitHub Desktop.
InputStream parser for the Twitter streaming API.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.showyou.importer.twitter; | |
import java.io.IOException; | |
import java.io.InputStream; | |
/** | |
* Created by Phil Kulak | |
* Date: 1/1/12 | |
* | |
* Parses an InputStream from the Twitter site streams API. The stream needs to be length-delimited (?delimited=length) | |
*/ | |
public class StreamParser { | |
private InputStream inputStream; | |
public StreamParser(InputStream stream) { | |
inputStream = stream; | |
} | |
public String read() throws IOException, InterruptedException { | |
if (Thread.interrupted()) { | |
inputStream.close(); | |
throw new InterruptedException(); | |
} | |
int currentByte = inputStream.read(); | |
StringBuilder byteCountString = new StringBuilder(); | |
// Slip ahead to the next integer (to skip keep alives) | |
while (currentByte >= 58 || currentByte <= 47) { | |
currentByte = inputStream.read(); | |
} | |
// Grab the byte count. | |
while (currentByte < 58 && currentByte > 47) { | |
byteCountString.append(new String(new byte[] {(byte) currentByte})); | |
currentByte = inputStream.read(); | |
} | |
int byteCount = Integer.parseInt(byteCountString.toString()); | |
// Swallow the \n. | |
inputStream.skip(1); | |
// Adjust the byte count to avoid the \r\n\r\n at the end | |
byteCount -= 4; | |
// Get the string. | |
byte[] buffer = new byte[byteCount]; | |
inputStream.read(buffer); | |
String json = new String(buffer, "UTF-8"); | |
// Swallow the \r\n\r\n | |
inputStream.skip(4); | |
return json; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment