DataReader.loadAsync terminates even if unconsumedBufferLength is 0

I am consuming a JSON stream on UWP WinRT using this code:

async function connect() { let stream: MSStream; return new CancellableContext<void>( async (context) => { // this will be called immediately stream = await context.queue(() => getStreamByXHR()); // returns ms-stream object await consumeStream(stream); }, { revert: () => { // this will be called when user cancels the task stream.msClose(); } } ).feed(); } async function consumeStream(stream: MSStream) { return new CancellableContext<void>(async (context) => { const input = stream.msDetachStream() as Windows.Storage.Streams.IInputStream; const reader = new Windows.Storage.Streams.DataReader(input); reader.inputStreamOptions = Windows.Storage.Streams.InputStreamOptions.partial; while (!context.canceled) { const content = await consumeString(1000); // ... some more codes } async function consumeString(count: number) { await reader.loadAsync(count); // will throw when the stream gets closed return reader.readString(reader.unconsumedBufferLength); } }).feed(); } 

Here's a document on InputStreamOptions.partial saying:

An asynchronous read operation completes when one or more bytes are available.

However, reader.loadAsync terminates even when reader.unconsumedBufferLength is 0, and this causes the processor to load. Is this an API error or can I prevent this behavior so that loadAsync can loadAsync only when unconsumedBufferLength greater than 0?

PS: Here is a reproduction with pure JS: https://github.com/SaschaNaz/InputStreamOptionsBugRepro

+6
source share
1 answer

Is this an API error or can I prevent this behavior so that loadAsync can only exit when unconsumedBufferLength is greater than 0

In most cases, it also ends at the end of the stream. Thus, in this case, unconsumedBufferLength will be null and must be maintained.

In fact, the example https://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.streams.datareader.aspx shows something like this (admittedly not using this option):

  // Once we have written the contents successfully we load the stream. await dataReader.LoadAsync((uint)stream.Size); var receivedStrings = ""; // Keep reading until we consume the complete stream. while (dataReader.UnconsumedBufferLength > 0) 

🌹

+1
source

All Articles