1#include <IO/WriteBufferFromOStream.h>
2#include <Common/Exception.h>
3
4
5namespace DB
6{
7
8namespace ErrorCodes
9{
10 extern const int CANNOT_WRITE_TO_OSTREAM;
11}
12
13void WriteBufferFromOStream::nextImpl()
14{
15 if (!offset())
16 return;
17
18 ostr->write(working_buffer.begin(), offset());
19 ostr->flush();
20
21 if (!ostr->good())
22 throw Exception("Cannot write to ostream at offset " + std::to_string(count()),
23 ErrorCodes::CANNOT_WRITE_TO_OSTREAM);
24}
25
26WriteBufferFromOStream::WriteBufferFromOStream(
27 size_t size,
28 char * existing_memory,
29 size_t alignment)
30 : BufferWithOwnMemory<WriteBuffer>(size, existing_memory, alignment)
31{
32}
33
34WriteBufferFromOStream::WriteBufferFromOStream(
35 std::ostream & ostr_,
36 size_t size,
37 char * existing_memory,
38 size_t alignment)
39 : BufferWithOwnMemory<WriteBuffer>(size, existing_memory, alignment), ostr(&ostr_)
40{
41}
42
43WriteBufferFromOStream::~WriteBufferFromOStream()
44{
45 try
46 {
47 next();
48 }
49 catch (...)
50 {
51 tryLogCurrentException(__PRETTY_FUNCTION__);
52 }
53}
54
55}
56