[*] Redo AuIO::ReadAll(IStreamReader *that, Memory::ByteBuffer &buffer, ...)

[+] EStreamError::eErrorByteBuffer
[+] EStreamError::eErrorOutOfMemory
This commit is contained in:
Reece Wilson 2023-10-24 11:27:20 +01:00
parent 4b38f205d1
commit 076e255432
3 changed files with 50 additions and 18 deletions

View File

@ -14,6 +14,8 @@ namespace Aurora::IO
eErrorEndOfStream,
eErrorStreamNotOpen,
eErrorStreamInterrupted,
eErrorHandleClosed
eErrorHandleClosed,
eErrorOutOfMemory,
eErrorByteBuffer
);
}

View File

@ -15,7 +15,7 @@ namespace Aurora::IO
AUI_METHOD(void, Close, ())
);
inline EStreamError ReadAll(IStreamReader* that, Memory::ByteBuffer& buffer);
inline EStreamError ReadAll(IStreamReader* that, Memory::ByteBuffer& buffer, AuOptional<bool> bBreakOnFirstMismatchLength = true);
}
#include "IStreamReader.inl" // ReadAll utility

View File

@ -9,32 +9,62 @@
namespace Aurora::IO
{
inline EStreamError ReadAll(IStreamReader *that, Memory::ByteBuffer &buffer)
inline EStreamError ReadAll(IStreamReader *that, Memory::ByteBuffer &buffer, AuOptional<bool> bBreakOnFirstMismatchLength)
{
static const int kBufferSize = 2048;
static const int kBufferSize = 8192;
AuUInt len;
EStreamError ret;
AuUInt8 temp[kBufferSize];
len = kBufferSize;
ret = EStreamError::eErrorEndOfStream;
buffer = Memory::NewResizableBuffer();
while ((ret = that->Read(Memory::MemoryViewStreamWrite(temp, len))) == EStreamError::eErrorNone)
if (buffer.HasStreamError())
{
if (len == 0)
return EStreamError::eErrorByteBuffer;
}
while (true)
{
AuUInt uBytesRead {};
bool bDoublebreak {};
auto next = buffer.GetNextLinearWrite();
if (!next)
{
next = buffer.GetOrAllocateLinearWriteable(kBufferSize);
}
if (!next)
{
return EStreamError::eErrorOutOfMemory;
}
while ((ret = that->Read(Memory::MemoryViewStreamWrite(next, uBytesRead))) == EStreamError::eErrorNone)
{
if (uBytesRead == 0)
{
bDoublebreak = true;
break;
}
buffer.Write(next.ptr, uBytesRead);
if (bBreakOnFirstMismatchLength.value_or(true) &&
uBytesRead != next.length)
{
bDoublebreak = true;
break;
}
}
if (uBytesRead == 0 ||
bDoublebreak)
{
break;
}
}
buffer.Write(temp, len);
if (len != kBufferSize)
{
break;
}
if (buffer.HasStreamError())
{
return EStreamError::eErrorByteBuffer;
}
if (ret == EStreamError::eErrorEndOfStream)