2001-10-06 14:13:28 +00: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;
|
|
|
|
}
|
|
|
|
|
2001-10-14 16:58:01 +00:00
|
|
|
const void* CBufferedOutputStream::peek(UInt32 n)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
return m_buffer.peek(n);
|
|
|
|
}
|
|
|
|
|
2001-10-14 16:58:01 +00:00
|
|
|
void CBufferedOutputStream::pop(UInt32 n)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
m_buffer.pop(n);
|
|
|
|
}
|
|
|
|
|
2001-10-14 16:58:01 +00:00
|
|
|
UInt32 CBufferedOutputStream::getSize() const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
return m_buffer.getSize();
|
|
|
|
}
|
|
|
|
|
2001-10-14 16:58:01 +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());
|
2001-10-06 14:13:28 +00:00
|
|
|
if (m_closeCB) {
|
|
|
|
m_closeCB->run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UInt32 CBufferedOutputStream::write(
|
2001-10-14 16:58:01 +00:00
|
|
|
const void* data, UInt32 n)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
CLock lock(m_mutex);
|
|
|
|
if (m_closed) {
|
|
|
|
throw XIOClosed();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_buffer.write(data, n);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2001-10-14 16:58:01 +00:00
|
|
|
void CBufferedOutputStream::flush()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
// wait until all data is written
|
|
|
|
while (getSizeWithLock() > 0) {
|
|
|
|
CThread::sleep(0.05);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-14 16:58:01 +00:00
|
|
|
UInt32 CBufferedOutputStream::getSizeWithLock() const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
CLock lock(m_mutex);
|
|
|
|
return m_buffer.getSize();
|
|
|
|
}
|
|
|
|
|