ktl::Awaitable<KBuffer::SPtr> GetFileBytesAsync(__in KStringView const & name) { KBlockFile::SPtr fileSPtr = nullptr; ktl::io::KFileStream::SPtr streamSPtr = nullptr; ktl::io::KFileStream::Create(streamSPtr, GetAllocator()); co_await OpenFileAsync(name, KBlockFile::CreateDisposition::eOpenExisting, *streamSPtr, fileSPtr); KBuffer::SPtr fileBytes = co_await GetFileBytesAsync(*streamSPtr); co_await streamSPtr->CloseAsync(); fileSPtr->Close(); co_return fileBytes; }
ktl::Awaitable<void> WriteFileBytesAsync( __in KStringView const & name, __in KBuffer & bytes) { KBlockFile::SPtr fileSPtr = nullptr; ktl::io::KFileStream::SPtr streamSPtr = nullptr; ktl::io::KFileStream::Create(streamSPtr, GetAllocator()); co_await OpenFileAsync(name, KBlockFile::CreateDisposition::eCreateAlways, *streamSPtr, fileSPtr); co_await WriteFileBytesAsync(*streamSPtr, bytes); co_await streamSPtr->CloseAsync(); fileSPtr->Close(); }
void DesktopFileHandle::ReadTextAsync(const std::function<void(std::string)>& callback) { std::function<void(void)> openCallback = [&]() { std::lock_guard<std::recursive_mutex> recLock(mMutex); std::string str((std::istreambuf_iterator<char>(mFile)), (std::istreambuf_iterator<char>())); callback(str); }; bool open = false; { std::lock_guard<std::recursive_mutex> recLock(mMutex); open = bIsOpen; } if (!open) OpenFileAsync(openCallback, FileMode::READ_ONLY); else std::future<void> fut = std::async(openCallback); }