Permalink
Browse files

Better handling of TCP fragmentation.

  • Loading branch information...
1 parent 78667c5 commit bf066e019ca3aea7e84c429a900bddfa4f38fac7 @codebutler codebutler committed Apr 5, 2012
Showing with 14 additions and 2 deletions.
  1. +14 −2 src/com/codebutler/android_websockets/HybiParser.java
@@ -351,9 +351,21 @@ public HappyDataInputStream(InputStream in) {
public byte[] readBytes(int length) throws IOException {
byte[] buffer = new byte[length];
- if (read(buffer) != length) {
- throw new IOException("Read wrong number of bytes.");
+
+ int total = 0;
+
+ while (total < length) {
+ int count = read(buffer, total, length - total);
+ if (count == -1) {
+ break;
+ }
+ total += count;
}
+
+ if (total != length) {
+ throw new IOException(String.format("Read wrong number of bytes. Got: %s, Expected: %s.", total, length));
+ }
+
return buffer;
}
}

0 comments on commit bf066e0

Please sign in to comment.