@@ -159,7 +159,15 @@ public List<char[]> DetachBlocks ()
159159
160160 // Rent a fresh block and carry over any unscanned data (partial line in progress)
161161 var tailLength = _readBlockLength - _scanOffset ;
162- var newBlock = ArrayPool < char > . Shared . Rent ( BLOCK_SIZE ) ;
162+
163+ // The tail may exceed BLOCK_SIZE after reading a long line (buffer was grown).
164+ var newBlockSize = BLOCK_SIZE ;
165+ while ( tailLength > newBlockSize )
166+ {
167+ newBlockSize *= 2 ;
168+ }
169+
170+ var newBlock = ArrayPool < char > . Shared . Rent ( newBlockSize ) ;
163171
164172 if ( tailLength > 0 )
165173 {
@@ -183,8 +191,17 @@ private void RefillBlock (StreamReader reader)
183191 {
184192 var tailLength = _readBlockLength - _scanOffset ;
185193
186- // Rent a new block
187- var newBlock = ArrayPool < char > . Shared . Rent ( BLOCK_SIZE ) ;
194+ // Determine new block size: if the tail already fills a standard block,
195+ // grow the buffer so there's room to read more data. This handles lines
196+ // longer than BLOCK_SIZE (e.g. huge XML payloads).
197+ var newBlockSize = BLOCK_SIZE ;
198+ while ( tailLength >= newBlockSize )
199+ {
200+ newBlockSize *= 2 ;
201+ }
202+
203+ // Rent a new block (may be larger than BLOCK_SIZE for very long lines)
204+ var newBlock = ArrayPool < char > . Shared . Rent ( newBlockSize ) ;
188205
189206 // Copy the tail (partial line) to the start of the new block
190207 if ( tailLength > 0 )
@@ -199,7 +216,8 @@ private void RefillBlock (StreamReader reader)
199216 _scanOffset = 0 ;
200217
201218 // Fill the rest of the block from the stream
202- var charsRead = reader . Read ( newBlock , tailLength , BLOCK_SIZE - tailLength ) ;
219+ var available = newBlock . Length - tailLength ;
220+ var charsRead = reader . Read ( newBlock , tailLength , available ) ;
203221
204222 _readBlockLength = tailLength + charsRead ;
205223
0 commit comments