2010-06-01 13:35:09 +00:00
|
|
|
#ifndef DBMS_COMMON_WRITEBUFFER_FROM_OSTREAM_H
|
|
|
|
#define DBMS_COMMON_WRITEBUFFER_FROM_OSTREAM_H
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <DB/Core/Exception.h>
|
|
|
|
#include <DB/Core/ErrorCodes.h>
|
|
|
|
|
2010-06-04 18:25:25 +00:00
|
|
|
#include <DB/IO/WriteBuffer.h>
|
2011-06-27 18:22:14 +00:00
|
|
|
#include <DB/IO/BufferWithOwnMemory.h>
|
2010-06-01 13:35:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2011-06-27 18:22:14 +00:00
|
|
|
class WriteBufferFromOStream : public BufferWithOwnMemory<WriteBuffer>
|
2010-06-01 13:35:09 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::ostream & ostr;
|
|
|
|
|
2011-06-24 21:08:26 +00:00
|
|
|
void nextImpl()
|
2010-06-01 13:35:09 +00:00
|
|
|
{
|
2011-06-27 19:34:03 +00:00
|
|
|
if (!offset())
|
|
|
|
return;
|
|
|
|
|
2011-06-27 18:22:14 +00:00
|
|
|
ostr.write(working_buffer.begin(), offset());
|
2010-06-04 18:25:25 +00:00
|
|
|
ostr.flush();
|
2010-06-01 13:35:09 +00:00
|
|
|
|
|
|
|
if (!ostr.good())
|
|
|
|
throw Exception("Cannot write to ostream", ErrorCodes::CANNOT_WRITE_TO_OSTREAM);
|
|
|
|
}
|
|
|
|
|
2011-06-26 21:30:59 +00:00
|
|
|
public:
|
|
|
|
WriteBufferFromOStream(std::ostream & ostr_) : ostr(ostr_) {}
|
|
|
|
|
2010-06-01 13:35:09 +00:00
|
|
|
~WriteBufferFromOStream()
|
|
|
|
{
|
2011-06-27 16:41:02 +00:00
|
|
|
nextImpl();
|
2010-06-01 13:35:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|