barrier/io/CBufferedOutputStream.cpp

83 lines
1.2 KiB
C++
Raw Normal View History

2001-10-06 18:13:28 +04:00
#include "CBufferedOutputStream.h"
#include "XIO.h"
2001-10-06 18:13:28 +04:00
#include "CLock.h"
#include "CMutex.h"
#include "CThread.h"
#include "IJob.h"
//
// CBufferedOutputStream
//
2002-06-17 17:31:21 +04:00
CBufferedOutputStream::CBufferedOutputStream(CMutex* mutex, IJob* closeCB) :
m_mutex(mutex),
m_closeCB(closeCB),
m_empty(mutex, true),
m_closed(false)
2001-10-06 18:13:28 +04:00
{
assert(m_mutex != NULL);
}
CBufferedOutputStream::~CBufferedOutputStream()
{
delete m_closeCB;
}
const void*
2002-06-17 17:31:21 +04:00
CBufferedOutputStream::peek(UInt32 n)
2001-10-06 18:13:28 +04:00
{
return m_buffer.peek(n);
}
void
2002-06-17 17:31:21 +04:00
CBufferedOutputStream::pop(UInt32 n)
2001-10-06 18:13:28 +04:00
{
m_buffer.pop(n);
if (m_buffer.getSize() == 0) {
m_empty.broadcast();
}
2001-10-06 18:13:28 +04:00
}
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;
2001-10-21 04:21:02 +04:00
m_buffer.pop(m_buffer.getSize());
2001-10-06 18:13:28 +04:00
if (m_closeCB) {
m_closeCB->run();
}
}
UInt32
2002-06-17 17:31:21 +04:00
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
CLock lock(m_mutex);
while (m_buffer.getSize() > 0) {
m_empty.wait();
}
2001-10-06 18:13:28 +04:00
}