Skip to content

Commit 03343c4

Browse files
Simplify and optimize position checking
1 parent f96f8ca commit 03343c4

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

src/ImageSharp/IO/ChunkedMemoryStream.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -127,24 +127,26 @@ public override int Read(Span<byte> buffer)
127127

128128
int offset = 0;
129129
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)
133133
{
134-
bytesToRead = count;
134+
remaining = count;
135135
}
136136

137-
if (bytesToRead <= 0)
137+
if (remaining <= 0)
138138
{
139139
// Already at the end of the stream, nothing to read
140140
return 0;
141141
}
142142

143+
int bytesToRead = (int)remaining;
144+
int bytesRead = 0;
143145
while (bytesToRead != 0 && this.currentChunk != this.memoryChunkBuffer.Length)
144146
{
145147
bool moveToNextChunk = false;
146148
MemoryChunk chunk = this.memoryChunkBuffer[this.currentChunk];
147-
int n = (int)Math.Min(bytesToRead, int.MaxValue);
149+
int n = bytesToRead;
148150
int remainingBytesInCurrentChunk = chunk.Length - this.currentChunkIndex;
149151
if (n >= remainingBytesInCurrentChunk)
150152
{
@@ -193,26 +195,28 @@ public override void Write(ReadOnlySpan<byte> buffer)
193195

194196
int offset = 0;
195197
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;
198200

199201
// Ensure we have enough capacity to write the data.
200-
while (bytesToWrite < count)
202+
while (remaining < count)
201203
{
202204
this.memoryChunkBuffer.Expand();
203-
bytesToWrite = this.memoryChunkBuffer.Length - this.position;
205+
remaining = this.memoryChunkBuffer.Length - this.position;
204206
}
205207

206-
if (bytesToWrite > count)
208+
if (remaining > count)
207209
{
208-
bytesToWrite = count;
210+
remaining = count;
209211
}
210212

213+
int bytesToWrite = (int)remaining;
214+
int bytesWritten = 0;
211215
while (bytesToWrite != 0 && this.currentChunk != this.memoryChunkBuffer.Length)
212216
{
213217
bool moveToNextChunk = false;
214218
MemoryChunk chunk = this.memoryChunkBuffer[this.currentChunk];
215-
int n = (int)Math.Min(bytesToWrite, int.MaxValue);
219+
int n = bytesToWrite;
216220
int remainingBytesInCurrentChunk = chunk.Length - this.currentChunkIndex;
217221
if (n >= remainingBytesInCurrentChunk)
218222
{
@@ -254,19 +258,20 @@ public void WriteTo(Stream stream)
254258

255259
this.Position = 0;
256260

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)
260263
{
261264
// Already at the end of the stream, nothing to read
262265
return;
263266
}
264267

268+
int bytesToRead = (int)remaining;
269+
int bytesRead = 0;
265270
while (bytesToRead != 0 && this.currentChunk != this.memoryChunkBuffer.Length)
266271
{
267272
bool moveToNextChunk = false;
268273
MemoryChunk chunk = this.memoryChunkBuffer[this.currentChunk];
269-
int n = (int)Math.Min(bytesToRead, int.MaxValue);
274+
int n = bytesToRead;
270275
int remainingBytesInCurrentChunk = chunk.Length - this.currentChunkIndex;
271276
if (n >= remainingBytesInCurrentChunk)
272277
{

0 commit comments

Comments
 (0)