Skip to content

Commit 29dc46f

Browse files
authored
Merge pull request #255 from LaoSparrow/streamext-readstring-eos
fix(StreamExt/ReadBytes): now throws EndOfStreamException when the en…
2 parents 2c82f67 + 87792be commit 29dc46f

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

TerrariaServerAPI/TerrariaApi.Server/StreamExt.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -211,23 +211,16 @@ public static byte[] ReadBytes(this Stream s, int count)
211211
throw new ArgumentOutOfRangeException("count");
212212
}
213213
byte[] array = new byte[count];
214-
int num = 0;
215-
do
214+
int offset = 0;
215+
while (count > 0)
216216
{
217-
int num2 = s.Read(array, num, count);
218-
if (num2 == 0)
217+
int numBytesRead = s.Read(array, offset, count);
218+
if (numBytesRead == 0)
219219
{
220-
break;
220+
throw new EndOfStreamException("End of stream");
221221
}
222-
num += num2;
223-
count -= num2;
224-
}
225-
while (count > 0);
226-
if (num != array.Length)
227-
{
228-
byte[] array2 = new byte[num];
229-
Buffer.BlockCopy(array, 0, array2, 0, num);
230-
array = array2;
222+
offset += numBytesRead;
223+
count -= numBytesRead;
231224
}
232225
return array;
233226
}

0 commit comments

Comments
 (0)