Jamie Reece Wilson
076e255432
[+] EStreamError::eErrorByteBuffer [+] EStreamError::eErrorOutOfMemory
77 lines
1.8 KiB
C++
77 lines
1.8 KiB
C++
/***
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: IStreamReader.inl
|
|
Date: 2022-2-1
|
|
Author: Reece
|
|
***/
|
|
#pragma once
|
|
|
|
namespace Aurora::IO
|
|
{
|
|
inline EStreamError ReadAll(IStreamReader *that, Memory::ByteBuffer &buffer, AuOptional<bool> bBreakOnFirstMismatchLength)
|
|
{
|
|
static const int kBufferSize = 8192;
|
|
|
|
EStreamError ret;
|
|
ret = EStreamError::eErrorEndOfStream;
|
|
|
|
if (buffer.HasStreamError())
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
|
|
if (buffer.HasStreamError())
|
|
{
|
|
return EStreamError::eErrorByteBuffer;
|
|
}
|
|
|
|
if (ret == EStreamError::eErrorEndOfStream)
|
|
{
|
|
return EStreamError::eErrorNone;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
} |