AuroraRuntime/Include/Aurora/IO/Buffered/BlobSeekableReader.hpp

63 lines
1.8 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: BlobSeekableReader.hpp
Date: 2021-6-10
Author: Reece
***/
#pragma once
namespace Aurora::IO::Buffered
{
struct BlobSeekableReader : ISeekingReader
{
AU_NO_COPY_NO_MOVE(BlobSeekableReader)
inline BlobSeekableReader(const AuSPtr<Memory::ByteBuffer> &buffer) : buffer_(buffer) {}
inline BlobSeekableReader(Memory::ByteBuffer &&buffer) : buffer_(AuMakeShared<Memory::ByteBuffer>(AuMove(buffer))) {}
inline BlobSeekableReader(const Memory::ByteBuffer &buffer) : buffer_(AuMakeShared<Memory::ByteBuffer>(buffer)) {}
inline BlobSeekableReader() {}
inline ~BlobSeekableReader(){}
inline virtual EStreamError IsOpen() override
{
if (!this->buffer_)
{
return EStreamError::eErrorStreamNotOpen;
}
return EStreamError::eErrorNone;
}
inline virtual EStreamError ArbitraryRead(AuUInt offset, const Memory::MemoryViewStreamWrite &parameters) override
{
if (!this->buffer_)
{
return EStreamError::eErrorStreamNotOpen;
}
if (this->buffer_->empty())
{
return EStreamError::eErrorEndOfStream;
}
this->buffer_->readPtr = this->buffer_->base + offset;
parameters.outVariable = this->buffer_->Read(parameters.ptr, parameters.length);
return parameters.outVariable == 0 ? EStreamError::eErrorEndOfStream : EStreamError::eErrorNone;
}
inline virtual void Close() override
{
if (!this->buffer_)
{
return;
}
this->buffer_->clear();
}
private:
AuSPtr<Memory::ByteBuffer> buffer_;
};
}