barrier/io/CBufferedOutputStream.cpp

80 lines
1.2 KiB
C++
Raw Normal View History

2001-10-06 18:13:28 +04:00
#include "CBufferedOutputStream.h"
#include "CLock.h"
#include "CMutex.h"
#include "CThread.h"
#include "IJob.h"
#include "XIO.h"
#include <assert.h>
//
// CBufferedOutputStream
//
CBufferedOutputStream::CBufferedOutputStream(CMutex* mutex, IJob* closeCB) :
m_mutex(mutex),
m_closeCB(closeCB),
m_closed(false)
{
assert(m_mutex != NULL);
}
CBufferedOutputStream::~CBufferedOutputStream()
{
delete m_closeCB;
}
const void* CBufferedOutputStream::peek(UInt32 n)
2001-10-06 18:13:28 +04:00
{
return m_buffer.peek(n);
}
void CBufferedOutputStream::pop(UInt32 n)
2001-10-06 18:13:28 +04:00
{
m_buffer.pop(n);
}
UInt32 CBufferedOutputStream::getSize() const
2001-10-06 18:13:28 +04:00
{
return m_buffer.getSize();
}
void CBufferedOutputStream::close()
2001-10-06 18:13:28 +04:00
{
CLock lock(m_mutex);
if (m_closed) {
throw XIOClosed();
}
m_closed = true;
if (m_closeCB) {
m_closeCB->run();
}
}
UInt32 CBufferedOutputStream::write(
const void* data, UInt32 n)
2001-10-06 18:13:28 +04:00
{
CLock lock(m_mutex);
if (m_closed) {
throw XIOClosed();
}
m_buffer.write(data, n);
return n;
}
void CBufferedOutputStream::flush()
2001-10-06 18:13:28 +04:00
{
// wait until all data is written
while (getSizeWithLock() > 0) {
CThread::sleep(0.05);
}
}
UInt32 CBufferedOutputStream::getSizeWithLock() const
2001-10-06 18:13:28 +04:00
{
CLock lock(m_mutex);
return m_buffer.getSize();
}