Hi,
I am trying to convert an inputstream from a zip file holding an utf8 string into a Platform::String to load it Inside the xml parser, so I wrote the following code :
Platform::String^ Container::ConvertUTF8StreamToPString(IInputStream^ inputStream, size_t BUF_SIZE /*= 4096*/)
{
Platform::String^ str = nullptr;
std::wstring wstr;
using Converter = std::wstring_convert<std::codecvt_utf8<wchar_t>>;
Converter converter;
if (inputStream != nullptr)
{
Platform::WriteOnlyArray<unsigned char, 1U>^ buf = nullptr;
DataReader^ dataReader = ref new DataReader(inputStream);
dataReader->UnicodeEncoding = UnicodeEncoding::Utf8;
int numRead = 0;
do
{
auto opLoadData = create_task(dataReader->LoadAsync(BUF_SIZE));
opLoadData.then([dataReader, &converter, &wstr, &numRead](unsigned int numBytesLoaded) {
numRead = numBytesLoaded;
if (numRead > 0)
{
Platform::WriteOnlyArray<unsigned char, 1U>^ buf = ref new Platform::Array<unsigned char, 1U>(numRead);
dataReader->ReadBytes(buf);
wstr.append(converter.from_bytes(reinterpret_cast<char*>(buf->Data), reinterpret_cast<char*>(buf->Data + numRead)));
}
}).wait();
} while (numRead > 0);
}
str = ref new Platform::String(wstr.c_str());
return str;
}
The first loop works fine but the second one where there is nothing more to read throw an null pointer exception, how can I fix this code ?
Thanks