2022-06-11 23:01:27 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
File: AuByteBufferStreamPair.cpp
|
2022-06-11 23:01:27 +00:00
|
|
|
Date: 2022-6-6
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
|
|
|
#include <Aurora/IO/IOExperimental.hpp>
|
2022-12-17 20:14:19 +00:00
|
|
|
#include "AuByteBufferStreamPair.hpp"
|
2022-06-11 23:01:27 +00:00
|
|
|
|
|
|
|
namespace Aurora::IO
|
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
ByteBufferStreamPair::ByteBufferStreamPair(const AuSPtr<Memory::ByteBuffer> &pBuffer) :
|
|
|
|
pBuffer(pBuffer),
|
|
|
|
reader(pBuffer),
|
|
|
|
writer(pBuffer),
|
|
|
|
seeker(pBuffer)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IStreamReader> ByteBufferStreamPair::ToStreamReader()
|
|
|
|
{
|
|
|
|
return AuSPtr<IStreamReader>(AuSharedFromThis(), AuStaticCast<IStreamReader>(&this->reader));
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<ISeekingReader> ByteBufferStreamPair::ToSeekingReader()
|
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
if (this->pBuffer->flagCircular)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
|
|
|
SysPushErrorIO("ring buffer must not be seekable");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return AuSPtr<ISeekingReader>(AuSharedFromThis(), AuStaticCast<ISeekingReader>(&this->seeker));
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IStreamWriter> ByteBufferStreamPair::ToStreamWriter()
|
|
|
|
{
|
|
|
|
return AuSPtr<IStreamWriter>(AuSharedFromThis(), AuStaticCast<IStreamWriter>(&this->writer));
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<Memory::ByteBuffer> ByteBufferStreamPair::ToByteBuffer()
|
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
return this->pBuffer;
|
2022-06-11 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<IByteBufferStreamPair> NewByteBufferPair()
|
|
|
|
{
|
|
|
|
auto scalable = AuMakeShared<AuByteBuffer>();
|
|
|
|
if (!scalable)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return AuMakeShared<ByteBufferStreamPair>(scalable);
|
|
|
|
}
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
AUKN_SYM AuSPtr<IByteBufferStreamPair> NewByteBufferPairEx(AuUInt uLength, bool bPermitResize)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
auto buffer = AuMakeShared<AuByteBuffer>(uLength, false, bPermitResize);
|
2022-06-11 23:01:27 +00:00
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return AuMakeShared<ByteBufferStreamPair>(buffer);
|
|
|
|
}
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
AUKN_SYM AuSPtr<IByteBufferStreamPair> NewRingByteBuffer(AuUInt uLength)
|
2022-06-11 23:01:27 +00:00
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
auto buffer = AuMakeShared<AuByteBuffer>(uLength, false, false);
|
2022-06-11 23:01:27 +00:00
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return AuMakeShared<ByteBufferStreamPair>(buffer);
|
|
|
|
}
|
|
|
|
}
|