Skip to content

Commit

Permalink
Dont use an InputStreamReader - was causing problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
codebutler committed Apr 2, 2012
1 parent 795ab97 commit 1ae6218
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
10 changes: 2 additions & 8 deletions src/com/codebutler/android_websockets/HybiParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

import java.io.*;
import java.math.BigInteger;
import java.net.Socket;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -106,12 +105,7 @@ private static byte[] mask(byte[] payload, byte[] mask, int offset) {
return payload;
}

public void start(Socket socket) throws IOException {
start(socket.getInputStream());
}

public void start(InputStream inputStream) throws IOException {
HappyDataInputStream stream = new HappyDataInputStream(inputStream);
public void start(HappyDataInputStream stream) throws IOException {
while (true) {
if (stream.available() == -1) break;
switch (mStage) {
Expand Down Expand Up @@ -350,7 +344,7 @@ public ProtocolError(String detailMessage) {
}
}

private static class HappyDataInputStream extends DataInputStream {
public static class HappyDataInputStream extends DataInputStream {
public HappyDataInputStream(InputStream in) {
super(in);
}
Expand Down
23 changes: 16 additions & 7 deletions src/com/codebutler/android_websockets/WebSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import org.apache.http.*;
import org.apache.http.message.BasicLineParser;
import org.apache.http.message.BasicNameValuePair;

import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
Expand Down Expand Up @@ -74,17 +75,17 @@ public void run() {
out.print("\r\n");
out.flush();

InputStreamReader reader = new InputStreamReader(mSocket.getInputStream());
HybiParser.HappyDataInputStream stream = new HybiParser.HappyDataInputStream(mSocket.getInputStream());

// Read HTTP response status line.
StatusLine statusLine = parseStatusLine(readLine(reader));
StatusLine statusLine = parseStatusLine(readLine(stream));
if (statusLine.getStatusCode() != HttpStatus.SC_SWITCHING_PROTOCOLS) {
throw new ProtocolException("Bad HTTP response: " + statusLine);
}

// Read HTTP response headers.
String line;
while (!TextUtils.isEmpty(line = readLine(reader))) {
while (!TextUtils.isEmpty(line = readLine(stream))) {
Header header = parseHeader(line);
if (header.getName().equals("Sec-WebSocket-Accept")) {
// FIXME: Verify the response...
Expand All @@ -94,7 +95,11 @@ public void run() {
mHandler.onConnect();

// Now decode websocket frames.
mParser.start(mSocket);
mParser.start(stream);

} catch (EOFException ex) {
Log.d(TAG, "WebSocket EOF!", ex);
mHandler.onDisconnect(0, "EOF");

} catch (Exception ex) {
mHandler.onError(ex);
Expand Down Expand Up @@ -125,17 +130,21 @@ private Header parseHeader(String line) {
}

// Can't use BufferedReader because it buffers past the HTTP data.
private String readLine(InputStreamReader reader) throws IOException {
private String readLine(HybiParser.HappyDataInputStream reader) throws IOException {
int readChar = reader.read();
if (readChar == -1) {
return null;
}
StringBuilder string = new StringBuilder("");
while (readChar != -1 && readChar != '\n') {
while (readChar != '\n') {
if (readChar != '\r') {
string.append((char) readChar);
}

readChar = reader.read();
if (readChar == -1) {
return null;
}
}
return string.toString();
}
Expand Down

0 comments on commit 1ae6218

Please sign in to comment.