2011-10-15 23:40:56 +00:00
|
|
|
#pragma once
|
2010-06-01 13:35:09 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
2010-06-01 13:35:09 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteBuffer.h>
|
|
|
|
#include <IO/BufferWithOwnMemory.h>
|
2010-06-01 13:35:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int CANNOT_WRITE_TO_OSTREAM;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2011-06-27 18:22:14 +00:00
|
|
|
class WriteBufferFromOStream : public BufferWithOwnMemory<WriteBuffer>
|
2010-06-01 13:35:09 +00:00
|
|
|
{
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
std::ostream & ostr;
|
2010-06-01 13:35:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void nextImpl() override
|
|
|
|
{
|
|
|
|
if (!offset())
|
|
|
|
return;
|
2016-08-19 01:54:23 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ostr.write(working_buffer.begin(), offset());
|
|
|
|
ostr.flush();
|
2010-06-01 13:35:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!ostr.good())
|
|
|
|
throw Exception("Cannot write to ostream", ErrorCodes::CANNOT_WRITE_TO_OSTREAM);
|
|
|
|
}
|
2010-06-01 13:35:09 +00:00
|
|
|
|
2011-06-26 21:30:59 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
WriteBufferFromOStream(
|
|
|
|
std::ostream & ostr_,
|
|
|
|
size_t size = DBMS_DEFAULT_BUFFER_SIZE,
|
|
|
|
char * existing_memory = nullptr,
|
|
|
|
size_t alignment = 0)
|
|
|
|
: BufferWithOwnMemory<WriteBuffer>(size, existing_memory, alignment), ostr(ostr_) {}
|
|
|
|
|
|
|
|
~WriteBufferFromOStream() override
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
|
|
|
}
|
2010-06-01 13:35:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|