// DWBS ------------------------------------------------------------------ DWBS
//
// PLATFORM		: ALL
// PRODUCT		: BIT_DEMON
// VISIBILITY	: PUBLIC
//
// DWBS ------------------------------------------------------------------ DWBS

#include <bdCore/bdContainers/bdByteBuffer.h>

bdByteBuffer::~bdByteBuffer()
{
	if(m_data)
	{
		bdDeallocate<bdUByte8>(m_data);
	}
	m_data = BD_NULL;
	m_readPtr = BD_NULL;
	m_writePtr = BD_NULL;
}


bdBool bdByteBuffer::write(const void *data, bdUInt size)
{
	bdBool ok = false;
	bdUInt maxWriteSize = m_size - (m_writePtr - m_data);
	if(size <= maxWriteSize)
	{
		bdUInt temp;
		ok = bdBytePacker::appendBuffer(m_writePtr, maxWriteSize, 0, temp, data, size);
		if(ok)
		{
			m_writePtr += temp;
		}
	}
	else
	{
		BD_ASSERT(g_assertFalse, "Could not write data to buffer. Insufficient space.\n");
	}
	return ok;
}

bdBool bdByteBuffer::read(void *data, bdUInt size)
{
	bdBool ok = false;
	bdUInt maxReadSize = m_size - (m_readPtr - m_data);
	if(size <= maxReadSize)
	{
		bdUInt temp;
		ok = bdBytePacker::removeBuffer(m_readPtr, maxReadSize, 0, temp, data, size);
		m_readPtr += size;
	}
	else
	{
		BD_WARN("warn", "Could not read data from buffer. Insufficient data available.\n");
	}
	return ok;
}
