/// <summary> /// Reads a string value from the input stream. /// </summary> /// <param name="codeUnitCount">The length of the string.</param> /// <returns>The value.</returns> public string ReadString(uint codeUnitCount) { // although docs says that input parameter is "codeUnitCount", sample // https://docs.microsoft.com/en-us/uwp/api/Windows.Storage.Streams.DataReader?view=winrt-19041 // shows that it is BYTE count, not CODEUNIT count. // codepoint in UTF-8 can be encoded in anything from 1 to 6 bytes. int length = (int)codeUnitCount; VerifyRead(length); var data = _buffer.GetSegment(); string result; switch (UnicodeEncoding) { case UnicodeEncoding.Utf8: result = Encoding.UTF8.GetString(data.Array !, data.Offset + _bufferPosition, length); break; case UnicodeEncoding.Utf16LE: result = Encoding.Unicode.GetString(data.Array !, data.Offset + _bufferPosition, length * 2); break; case UnicodeEncoding.Utf16BE: result = Encoding.BigEndianUnicode.GetString(data.Array !, data.Offset + _bufferPosition, length * 2); break; default: throw new InvalidOperationException("Unsupported UnicodeEncoding value."); } _bufferPosition += length; return(result); }