/*** Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved. File: BaseStream.cpp Date: 2022-2-14 Author: Reece ***/ #include #include "Compression.hpp" #include "IngestableReadBase.hpp" #include "BaseStream.hpp" namespace Aurora::Compression { AuStreamReadWrittenPair_t BaseStream::ReadEx(const Memory::MemoryViewWrite & /*opt*/ destination, bool ingestUntilEOS) { AU_LOCK_GUARD(this->_spinlock); AuUInt32 read {}, len {}; if (!destination.length && !destination.ptr) { return {0, this->_outbuffer.RemainingBytes()}; } if (ingestUntilEOS) { while (this->_outbuffer.RemainingBytes() < destination.length) { auto toRead = destination.length ? AuUInt32(destination.length - this->_outbuffer.RemainingBytes()) : 10 * 1024; if (Ingest_s(toRead).second == 0) { if (!this->_outbuffer.RemainingBytes(true)) { return {}; } break; } read += toRead; } } len = this->_outbuffer.Read(destination.ptr, destination.length, destination.ptr == nullptr); return {read, len}; } AuUInt32 BaseStream::GetAvailableProcessedBytes() { AU_LOCK_GUARD(this->_spinlock); return this->_outbuffer.RemainingBytes(); } AuUInt32 BaseStream::Read(const Memory::MemoryViewWrite & /*opt*/ destination) { AU_LOCK_GUARD(this->_spinlock); if (!destination.length && !destination.ptr) { return this->_outbuffer.RemainingBytes(); } return this->_outbuffer.Read(destination.ptr, destination.length, destination.ptr == nullptr); } bool BaseStream::GoBackByProcessedN(AuUInt32 offset) { AU_LOCK_GUARD(this->_spinlock); return this->_outbuffer.ReaderTryGoBack(offset); } bool BaseStream::GoForwardByProcessedN(AuUInt32 offset) { AU_LOCK_GUARD(this->_spinlock); if (!offset) { return true; } return this->_outbuffer.ReaderTryGoForward(offset); } AuStreamReadWrittenPair_t BaseStream::Ingest(AuUInt32 bytesFromUnprocessedInputSource) { AU_LOCK_GUARD(this->_spinlock); if (!bytesFromUnprocessedInputSource) { return {}; } return Ingest_s(bytesFromUnprocessedInputSource); } bool BaseStream::Write(const void *a, AuUInt32 length) { auto written = this->_outbuffer.Write(reinterpret_cast(a), length); return written == length; } AuUInt32 BaseStream::GetInternalBufferSize() { return this->_outbuffer.allocSize; } }