@@ -38,23 +38,34 @@ private static StreamType streamType(byte streamType) {
3838 */
3939 public Frame readFrame () throws IOException {
4040 byte [] header = new byte [HEADER_SIZE ];
41- int headerSize = inputStream .read (header );
4241
43- if (headerSize == -1 ) {
44- return null ;
45- }
42+ int actualHeaderSize = 0 ;
4643
47- if (headerSize != HEADER_SIZE ) {
48- throw new IOException (String .format ("header must be %d bytes long, but was %d" , HEADER_SIZE , headerSize ));
49- }
44+ do {
45+ int headerCount = inputStream .read (header , actualHeaderSize , HEADER_SIZE - actualHeaderSize );
46+
47+ if (headerCount == -1 ) {
48+ return null ;
49+ }
50+ actualHeaderSize += headerCount ;
51+ } while (actualHeaderSize < HEADER_SIZE );
5052
5153 int payloadSize = ((header [4 ] & 0xff ) << 24 ) + ((header [5 ] & 0xff ) << 16 ) + ((header [6 ] & 0xff ) << 8 ) + (header [7 ] & 0xff );
5254
5355 byte [] payload = new byte [payloadSize ];
54- int actualPayloadSize = inputStream .read (payload );
55- if (actualPayloadSize != payloadSize ) {
56- throw new IOException (String .format ("payload must be %d bytes long, but was %d" , payloadSize , actualPayloadSize ));
57- }
56+ int actualPayloadSize = 0 ;
57+
58+ do {
59+ int count = inputStream .read (payload , actualPayloadSize , payloadSize - actualPayloadSize );
60+
61+ if (count == -1 ) {
62+ if (actualPayloadSize != payloadSize ) {
63+ throw new IOException (String .format ("payload must be %d bytes long, but was %d" , payloadSize , actualPayloadSize ));
64+ }
65+ break ;
66+ }
67+ actualPayloadSize += count ;
68+ } while (actualPayloadSize < payloadSize );
5869
5970 return new Frame (streamType (header [0 ]), payload );
6071 }
0 commit comments