@@ -128,8 +128,15 @@ private ValueTask<int> ReadBytesAsync(Memory<byte> buffer, ProtocolErrorBehavior
128
128
var uncompressedData = new byte [ uncompressedLength ] ;
129
129
using var compressedStream = new MemoryStream ( payloadReadBytes . Array ! , payloadReadBytes . Offset , payloadReadBytes . Count ) ;
130
130
using var decompressingStream = new ZLibStream ( compressedStream , CompressionMode . Decompress ) ;
131
- var bytesRead = decompressingStream . Read ( uncompressedData , 0 , uncompressedLength ) ;
132
- m_remainingData = new ( uncompressedData , 0 , bytesRead ) ;
131
+ int bytesRead , totalBytesRead = 0 ;
132
+ do
133
+ {
134
+ bytesRead = decompressingStream . Read ( uncompressedData , totalBytesRead , uncompressedLength - totalBytesRead ) ;
135
+ totalBytesRead += bytesRead ;
136
+ } while ( bytesRead > 0 ) ;
137
+ if ( totalBytesRead != uncompressedLength && protocolErrorBehavior == ProtocolErrorBehavior . Throw )
138
+ return ValueTaskExtensions . FromException < int > ( new InvalidOperationException ( "Expected to read {0:n0} uncompressed bytes but only read {1:n0}" . FormatInvariant ( uncompressedLength , totalBytesRead ) ) ) ;
139
+ m_remainingData = new ( uncompressedData , 0 , totalBytesRead ) ;
133
140
#else
134
141
// check CMF (Compression Method and Flags) and FLG (Flags) bytes for expected values
135
142
var cmf = payloadReadBytes . Array ! [ payloadReadBytes . Offset ] ;
@@ -151,10 +158,17 @@ private ValueTask<int> ReadBytesAsync(Memory<byte> buffer, ProtocolErrorBehavior
151
158
var uncompressedData = new byte [ uncompressedLength ] ;
152
159
using var compressedStream = new MemoryStream ( payloadReadBytes . Array , payloadReadBytes . Offset + headerSize , payloadReadBytes . Count - headerSize - checksumSize ) ;
153
160
using var decompressingStream = new DeflateStream ( compressedStream , CompressionMode . Decompress ) ;
154
- var bytesRead = decompressingStream . Read ( uncompressedData , 0 , uncompressedLength ) ;
155
- m_remainingData = new ( uncompressedData , 0 , bytesRead ) ;
156
-
157
- var checksum = Adler32 . Calculate ( uncompressedData , 0 , ( uint ) bytesRead ) ;
161
+ int bytesRead , totalBytesRead = 0 ;
162
+ do
163
+ {
164
+ bytesRead = decompressingStream . Read ( uncompressedData , totalBytesRead , uncompressedLength - totalBytesRead ) ;
165
+ totalBytesRead += bytesRead ;
166
+ } while ( bytesRead > 0 ) ;
167
+ if ( totalBytesRead != uncompressedLength && protocolErrorBehavior == ProtocolErrorBehavior . Throw )
168
+ return ValueTaskExtensions . FromException < int > ( new InvalidOperationException ( "Expected to read {0:n0} uncompressed bytes but only read {1:n0}" . FormatInvariant ( uncompressedLength , totalBytesRead ) ) ) ;
169
+ m_remainingData = new ( uncompressedData , 0 , totalBytesRead ) ;
170
+
171
+ var checksum = Adler32 . Calculate ( uncompressedData , 0 , ( uint ) totalBytesRead ) ;
158
172
159
173
var adlerStartOffset = payloadReadBytes . Offset + payloadReadBytes . Count - 4 ;
160
174
if ( payloadReadBytes . Array [ adlerStartOffset + 0 ] != ( ( checksum >> 24 ) & 0xFF ) ||
0 commit comments