@@ -127,24 +127,26 @@ public override int Read(Span<byte> buffer)
127
127
128
128
int offset = 0 ;
129
129
int count = buffer . Length ;
130
- int bytesRead = 0 ;
131
- long bytesToRead = this . length - this . position ;
132
- if ( bytesToRead > count )
130
+
131
+ long remaining = this . length - this . position ;
132
+ if ( remaining > count )
133
133
{
134
- bytesToRead = count ;
134
+ remaining = count ;
135
135
}
136
136
137
- if ( bytesToRead <= 0 )
137
+ if ( remaining <= 0 )
138
138
{
139
139
// Already at the end of the stream, nothing to read
140
140
return 0 ;
141
141
}
142
142
143
+ int bytesToRead = ( int ) remaining ;
144
+ int bytesRead = 0 ;
143
145
while ( bytesToRead != 0 && this . currentChunk != this . memoryChunkBuffer . Length )
144
146
{
145
147
bool moveToNextChunk = false ;
146
148
MemoryChunk chunk = this . memoryChunkBuffer [ this . currentChunk ] ;
147
- int n = ( int ) Math . Min ( bytesToRead , int . MaxValue ) ;
149
+ int n = bytesToRead ;
148
150
int remainingBytesInCurrentChunk = chunk . Length - this . currentChunkIndex ;
149
151
if ( n >= remainingBytesInCurrentChunk )
150
152
{
@@ -193,26 +195,28 @@ public override void Write(ReadOnlySpan<byte> buffer)
193
195
194
196
int offset = 0 ;
195
197
int count = buffer . Length ;
196
- int bytesWritten = 0 ;
197
- long bytesToWrite = this . memoryChunkBuffer . Length - this . position ;
198
+
199
+ long remaining = this . memoryChunkBuffer . Length - this . position ;
198
200
199
201
// Ensure we have enough capacity to write the data.
200
- while ( bytesToWrite < count )
202
+ while ( remaining < count )
201
203
{
202
204
this . memoryChunkBuffer . Expand ( ) ;
203
- bytesToWrite = this . memoryChunkBuffer . Length - this . position ;
205
+ remaining = this . memoryChunkBuffer . Length - this . position ;
204
206
}
205
207
206
- if ( bytesToWrite > count )
208
+ if ( remaining > count )
207
209
{
208
- bytesToWrite = count ;
210
+ remaining = count ;
209
211
}
210
212
213
+ int bytesToWrite = ( int ) remaining ;
214
+ int bytesWritten = 0 ;
211
215
while ( bytesToWrite != 0 && this . currentChunk != this . memoryChunkBuffer . Length )
212
216
{
213
217
bool moveToNextChunk = false ;
214
218
MemoryChunk chunk = this . memoryChunkBuffer [ this . currentChunk ] ;
215
- int n = ( int ) Math . Min ( bytesToWrite , int . MaxValue ) ;
219
+ int n = bytesToWrite ;
216
220
int remainingBytesInCurrentChunk = chunk . Length - this . currentChunkIndex ;
217
221
if ( n >= remainingBytesInCurrentChunk )
218
222
{
@@ -254,19 +258,20 @@ public void WriteTo(Stream stream)
254
258
255
259
this . Position = 0 ;
256
260
257
- int bytesRead = 0 ;
258
- long bytesToRead = this . length - this . position ;
259
- if ( bytesToRead <= 0 )
261
+ long remaining = this . length - this . position ;
262
+ if ( remaining <= 0 )
260
263
{
261
264
// Already at the end of the stream, nothing to read
262
265
return ;
263
266
}
264
267
268
+ int bytesToRead = ( int ) remaining ;
269
+ int bytesRead = 0 ;
265
270
while ( bytesToRead != 0 && this . currentChunk != this . memoryChunkBuffer . Length )
266
271
{
267
272
bool moveToNextChunk = false ;
268
273
MemoryChunk chunk = this . memoryChunkBuffer [ this . currentChunk ] ;
269
- int n = ( int ) Math . Min ( bytesToRead , int . MaxValue ) ;
274
+ int n = bytesToRead ;
270
275
int remainingBytesInCurrentChunk = chunk . Length - this . currentChunkIndex ;
271
276
if ( n >= remainingBytesInCurrentChunk )
272
277
{
0 commit comments