Skip to content

Commit 1ae6218

Browse files
committed
Dont use an InputStreamReader - was causing problems.
1 parent 795ab97 commit 1ae6218

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

src/com/codebutler/android_websockets/HybiParser.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
import java.io.*;
3636
import java.math.BigInteger;
37-
import java.net.Socket;
3837
import java.util.Arrays;
3938
import java.util.List;
4039

@@ -106,12 +105,7 @@ private static byte[] mask(byte[] payload, byte[] mask, int offset) {
106105
return payload;
107106
}
108107

109-
public void start(Socket socket) throws IOException {
110-
start(socket.getInputStream());
111-
}
112-
113-
public void start(InputStream inputStream) throws IOException {
114-
HappyDataInputStream stream = new HappyDataInputStream(inputStream);
108+
public void start(HappyDataInputStream stream) throws IOException {
115109
while (true) {
116110
if (stream.available() == -1) break;
117111
switch (mStage) {
@@ -350,7 +344,7 @@ public ProtocolError(String detailMessage) {
350344
}
351345
}
352346

353-
private static class HappyDataInputStream extends DataInputStream {
347+
public static class HappyDataInputStream extends DataInputStream {
354348
public HappyDataInputStream(InputStream in) {
355349
super(in);
356350
}

src/com/codebutler/android_websockets/WebSocketClient.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
import android.text.TextUtils;
44
import android.util.Base64;
5+
import android.util.Log;
56
import org.apache.http.*;
67
import org.apache.http.message.BasicLineParser;
78
import org.apache.http.message.BasicNameValuePair;
89

910
import javax.net.SocketFactory;
1011
import javax.net.ssl.SSLSocketFactory;
12+
import java.io.EOFException;
1113
import java.io.IOException;
12-
import java.io.InputStreamReader;
1314
import java.io.OutputStream;
1415
import java.io.PrintWriter;
1516
import java.net.Socket;
@@ -74,17 +75,17 @@ public void run() {
7475
out.print("\r\n");
7576
out.flush();
7677

77-
InputStreamReader reader = new InputStreamReader(mSocket.getInputStream());
78+
HybiParser.HappyDataInputStream stream = new HybiParser.HappyDataInputStream(mSocket.getInputStream());
7879

7980
// Read HTTP response status line.
80-
StatusLine statusLine = parseStatusLine(readLine(reader));
81+
StatusLine statusLine = parseStatusLine(readLine(stream));
8182
if (statusLine.getStatusCode() != HttpStatus.SC_SWITCHING_PROTOCOLS) {
8283
throw new ProtocolException("Bad HTTP response: " + statusLine);
8384
}
8485

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

9697
// Now decode websocket frames.
97-
mParser.start(mSocket);
98+
mParser.start(stream);
99+
100+
} catch (EOFException ex) {
101+
Log.d(TAG, "WebSocket EOF!", ex);
102+
mHandler.onDisconnect(0, "EOF");
98103

99104
} catch (Exception ex) {
100105
mHandler.onError(ex);
@@ -125,17 +130,21 @@ private Header parseHeader(String line) {
125130
}
126131

127132
// Can't use BufferedReader because it buffers past the HTTP data.
128-
private String readLine(InputStreamReader reader) throws IOException {
133+
private String readLine(HybiParser.HappyDataInputStream reader) throws IOException {
129134
int readChar = reader.read();
130135
if (readChar == -1) {
131136
return null;
132137
}
133138
StringBuilder string = new StringBuilder("");
134-
while (readChar != -1 && readChar != '\n') {
139+
while (readChar != '\n') {
135140
if (readChar != '\r') {
136141
string.append((char) readChar);
137142
}
143+
138144
readChar = reader.read();
145+
if (readChar == -1) {
146+
return null;
147+
}
139148
}
140149
return string.toString();
141150
}

0 commit comments

Comments
 (0)