2001-10-06 14:13:28 +00:00
|
|
|
#include "CBufferedOutputStream.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "XIO.h"
|
2001-10-06 14:13:28 +00:00
|
|
|
#include "CLock.h"
|
|
|
|
#include "CMutex.h"
|
|
|
|
#include "CThread.h"
|
|
|
|
#include "IJob.h"
|
|
|
|
|
|
|
|
//
|
|
|
|
// CBufferedOutputStream
|
|
|
|
//
|
|
|
|
|
2002-07-28 17:25:13 +00:00
|
|
|
CBufferedOutputStream::CBufferedOutputStream(
|
|
|
|
CMutex* mutex, IJob* adoptedCloseCB) :
|
2002-06-10 22:06:45 +00:00
|
|
|
m_mutex(mutex),
|
2002-07-28 17:25:13 +00:00
|
|
|
m_closeCB(adoptedCloseCB),
|
2002-06-10 22:06:45 +00:00
|
|
|
m_empty(mutex, true),
|
|
|
|
m_closed(false)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
assert(m_mutex != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
CBufferedOutputStream::~CBufferedOutputStream()
|
|
|
|
{
|
|
|
|
delete m_closeCB;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
const void*
|
2002-06-17 13:31:21 +00:00
|
|
|
CBufferedOutputStream::peek(UInt32 n)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
return m_buffer.peek(n);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
CBufferedOutputStream::pop(UInt32 n)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
m_buffer.pop(n);
|
2002-06-02 22:57:50 +00:00
|
|
|
if (m_buffer.getSize() == 0) {
|
|
|
|
m_empty.broadcast();
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
UInt32
|
|
|
|
CBufferedOutputStream::getSize() const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
return m_buffer.getSize();
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CBufferedOutputStream::close()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
CLock lock(m_mutex);
|
|
|
|
if (m_closed) {
|
|
|
|
throw XIOClosed();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_closed = true;
|
2001-10-21 00:21:02 +00:00
|
|
|
m_buffer.pop(m_buffer.getSize());
|
2002-07-28 17:25:13 +00:00
|
|
|
if (m_closeCB != NULL) {
|
2001-10-06 14:13:28 +00:00
|
|
|
m_closeCB->run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
UInt32
|
2002-07-28 17:25:13 +00:00
|
|
|
CBufferedOutputStream::write(const void* buffer, UInt32 n)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
CLock lock(m_mutex);
|
|
|
|
if (m_closed) {
|
|
|
|
throw XIOClosed();
|
|
|
|
}
|
|
|
|
|
2002-07-28 17:25:13 +00:00
|
|
|
m_buffer.write(buffer, n);
|
2001-10-06 14:13:28 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CBufferedOutputStream::flush()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
// wait until all data is written
|
|
|
|
CLock lock(m_mutex);
|
2002-06-02 22:57:50 +00:00
|
|
|
while (m_buffer.getSize() > 0) {
|
|
|
|
m_empty.wait();
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|