pollymc/launcher/net/ByteArraySink.h

62 lines
1.4 KiB
C
Raw Normal View History

#pragma once
#include "Sink.h"
namespace Net {
/*
* Sink object for downloads that uses an external QByteArray it doesn't own as a target.
*/
class ByteArraySink : public Sink {
public:
ByteArraySink(QByteArray* output) : m_output(output){};
virtual ~ByteArraySink() = default;
public:
auto init(QNetworkRequest& request) -> Task::State override
2018-07-15 18:21:05 +05:30
{
if(!m_output)
return Task::State::Failed;
2018-07-15 18:21:05 +05:30
m_output->clear();
if (initAllValidators(request))
return Task::State::Running;
return Task::State::Failed;
2018-07-15 18:21:05 +05:30
};
auto write(QByteArray& data) -> Task::State override
2018-07-15 18:21:05 +05:30
{
if(!m_output)
return Task::State::Failed;
2018-07-15 18:21:05 +05:30
m_output->append(data);
if (writeAllValidators(data))
return Task::State::Running;
return Task::State::Failed;
2018-07-15 18:21:05 +05:30
}
auto abort() -> Task::State override
2018-07-15 18:21:05 +05:30
{
if(!m_output)
return Task::State::Failed;
2018-07-15 18:21:05 +05:30
m_output->clear();
failAllValidators();
return Task::State::Failed;
2018-07-15 18:21:05 +05:30
}
auto finalize(QNetworkReply& reply) -> Task::State override
2018-07-15 18:21:05 +05:30
{
if (finalizeAllValidators(reply))
return Task::State::Succeeded;
return Task::State::Failed;
2018-07-15 18:21:05 +05:30
}
auto hasLocalData() -> bool override { return false; }
private:
QByteArray* m_output;
};
} // namespace Net