From 97487f41ea0913a91e9249f2730959bf8a1e2fd3 Mon Sep 17 00:00:00 2001 From: janos-laszlo Date: Sat, 8 Nov 2025 00:39:16 +0200 Subject: [PATCH] Fix GetMoreBytesFromStream to handle final block correctly --- .../how-to/csharp/Utf8ReaderPartialRead.cs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/Utf8ReaderPartialRead.cs b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/Utf8ReaderPartialRead.cs index 39d90911940d0..244b402341b48 100644 --- a/docs/standard/serialization/system-text-json/snippets/how-to/csharp/Utf8ReaderPartialRead.cs +++ b/docs/standard/serialization/system-text-json/snippets/how-to/csharp/Utf8ReaderPartialRead.cs @@ -52,29 +52,38 @@ public static void Run() Console.WriteLine($"Got property value: {reader.GetString()}"); } - private static void GetMoreBytesFromStream( + private static bool GetMoreBytesFromStream( MemoryStream stream, ref byte[] buffer, ref Utf8JsonReader reader) { + // If we've reached the end of the JSON data, return false. + if (reader.IsFinalBlock) + return false; int bytesRead; if (reader.BytesConsumed < buffer.Length) { ReadOnlySpan leftover = buffer.AsSpan((int)reader.BytesConsumed); - + if (leftover.Length == buffer.Length) { Array.Resize(ref buffer, buffer.Length * 2); Console.WriteLine($"Increased buffer size to {buffer.Length}"); } - + + // Move any leftover bytes to the beginning of the buffer. leftover.CopyTo(buffer); + // Read more data from the stream to fill the rest of the buffer. bytesRead = stream.Read(buffer.AsSpan(leftover.Length)); } else { bytesRead = stream.Read(buffer); } + // If we've reached the end of the stream, set isFinalBlock to true. + if (stream.Position == stream.Length) + Array.Resize(ref buffer, buffer.Length - (int)reader.BytesConsumed + bytesRead); Console.WriteLine($"String in buffer is: {Encoding.UTF8.GetString(buffer)}"); - reader = new Utf8JsonReader(buffer, isFinalBlock: bytesRead == 0, reader.CurrentState); + reader = new Utf8JsonReader(buffer, isFinalBlock: stream.Position == stream.Length, reader.CurrentState); + return bytesRead > 0; } } }