ClickHouse/dbms/include/DB/IO/WriteBufferFromOStream.h

58 lines
956 B
C++
Raw Normal View History

2011-10-15 23:40:56 +00:00
#pragma once
2010-06-01 13:35:09 +00:00
#include <iostream>
2015-10-05 01:35:28 +00:00
#include <DB/Common/Exception.h>
2010-06-01 13:35:09 +00:00
2010-06-04 18:25:25 +00:00
#include <DB/IO/WriteBuffer.h>
#include <DB/IO/BufferWithOwnMemory.h>
2010-06-01 13:35:09 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int CANNOT_WRITE_TO_OSTREAM;
}
class WriteBufferFromOStream : public BufferWithOwnMemory<WriteBuffer>
2010-06-01 13:35:09 +00:00
{
private:
std::ostream & ostr;
void nextImpl() override
2010-06-01 13:35:09 +00:00
{
2011-06-27 19:34:03 +00:00
if (!offset())
return;
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_,
size_t size = DBMS_DEFAULT_BUFFER_SIZE,
char * existing_memory = nullptr,
size_t alignment = 0)
: BufferWithOwnMemory<WriteBuffer>(size, existing_memory, alignment), ostr(ostr_) {}
2011-06-26 21:30:59 +00:00
~WriteBufferFromOStream() override
2010-06-01 13:35:09 +00:00
{
2013-11-18 17:17:45 +00:00
try
{
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
}
};
}