Skip to content

Commit bf066e0

Browse files
committed
Better handling of TCP fragmentation.
1 parent 78667c5 commit bf066e0

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/com/codebutler/android_websockets/HybiParser.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,21 @@ public HappyDataInputStream(InputStream in) {
351351

352352
public byte[] readBytes(int length) throws IOException {
353353
byte[] buffer = new byte[length];
354-
if (read(buffer) != length) {
355-
throw new IOException("Read wrong number of bytes.");
354+
355+
int total = 0;
356+
357+
while (total < length) {
358+
int count = read(buffer, total, length - total);
359+
if (count == -1) {
360+
break;
361+
}
362+
total += count;
356363
}
364+
365+
if (total != length) {
366+
throw new IOException(String.format("Read wrong number of bytes. Got: %s, Expected: %s.", total, length));
367+
}
368+
357369
return buffer;
358370
}
359371
}

0 commit comments

Comments
 (0)