From 87792befce39714c06baa65fae3aa79efa033ec9 Mon Sep 17 00:00:00 2001 From: LaoSparrow Date: Sat, 1 Mar 2025 11:40:15 +0800 Subject: [PATCH] fix(StreamExt/ReadBytes): now throws EndOfStreamException when the end of the stream is reached --- .../TerrariaApi.Server/StreamExt.cs | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/TerrariaServerAPI/TerrariaApi.Server/StreamExt.cs b/TerrariaServerAPI/TerrariaApi.Server/StreamExt.cs index 6b7e7a7be..4d058978f 100644 --- a/TerrariaServerAPI/TerrariaApi.Server/StreamExt.cs +++ b/TerrariaServerAPI/TerrariaApi.Server/StreamExt.cs @@ -211,23 +211,16 @@ public static byte[] ReadBytes(this Stream s, int count) throw new ArgumentOutOfRangeException("count"); } byte[] array = new byte[count]; - int num = 0; - do + int offset = 0; + while (count > 0) { - int num2 = s.Read(array, num, count); - if (num2 == 0) + int numBytesRead = s.Read(array, offset, count); + if (numBytesRead == 0) { - break; + throw new EndOfStreamException("End of stream"); } - num += num2; - count -= num2; - } - while (count > 0); - if (num != array.Length) - { - byte[] array2 = new byte[num]; - Buffer.BlockCopy(array, 0, array2, 0, num); - array = array2; + offset += numBytesRead; + count -= numBytesRead; } return array; }