2011-10-15 23:40:56 +00:00
|
|
|
#pragma once
|
2010-06-01 13:35:09 +00:00
|
|
|
|
|
|
|
#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:
|
2011-11-29 17:23:30 +00:00
|
|
|
WriteBufferFromOStream(std::ostream & ostr_, size_t size = DBMS_DEFAULT_BUFFER_SIZE)
|
|
|
|
: BufferWithOwnMemory<WriteBuffer>(size), ostr(ostr_) {}
|
2011-06-26 21:30:59 +00:00
|
|
|
|
2010-06-01 13:35:09 +00:00
|
|
|
~WriteBufferFromOStream()
|
|
|
|
{
|
2013-11-18 17:17:45 +00:00
|
|
|
try
|
|
|
|
{
|
2011-12-12 01:06:13 +00:00
|
|
|
next();
|
2013-11-18 17:17:45 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2013-11-18 19:18:03 +00:00
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
2013-11-18 17:17:45 +00:00
|
|
|
}
|
2010-06-01 13:35:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|