diff --git a/dbms/src/IO/ReadBufferFromIStream.cpp b/dbms/src/IO/ReadBufferFromIStream.cpp new file mode 100644 index 00000000000..a65946e5170 --- /dev/null +++ b/dbms/src/IO/ReadBufferFromIStream.cpp @@ -0,0 +1,36 @@ +#include +#include + + +namespace DB +{ + +namespace ErrorCodes +{ + extern const int CANNOT_READ_FROM_ISTREAM; +} + +bool ReadBufferFromIStream::nextImpl() +{ + istr.read(internal_buffer.begin(), internal_buffer.size()); + size_t gcount = istr.gcount(); + + if (!gcount) + { + if (istr.eof()) + return false; + else + throw Exception("Cannot read from istream", ErrorCodes::CANNOT_READ_FROM_ISTREAM); + } + else + working_buffer.resize(gcount); + + return true; +} + +ReadBufferFromIStream::ReadBufferFromIStream(std::istream & istr_, size_t size) + : BufferWithOwnMemory(size), istr(istr_) +{ +} + +} diff --git a/dbms/src/IO/ReadBufferFromIStream.h b/dbms/src/IO/ReadBufferFromIStream.h index 203200b0cac..7f804783ba2 100644 --- a/dbms/src/IO/ReadBufferFromIStream.h +++ b/dbms/src/IO/ReadBufferFromIStream.h @@ -2,8 +2,6 @@ #include -#include - #include #include @@ -11,38 +9,15 @@ namespace DB { -namespace ErrorCodes -{ - extern const int CANNOT_READ_FROM_ISTREAM; -} - - class ReadBufferFromIStream : public BufferWithOwnMemory { private: std::istream & istr; - bool nextImpl() override - { - istr.read(internal_buffer.begin(), internal_buffer.size()); - size_t gcount = istr.gcount(); - - if (!gcount) - { - if (istr.eof()) - return false; - else - throw Exception("Cannot read from istream", ErrorCodes::CANNOT_READ_FROM_ISTREAM); - } - else - working_buffer.resize(gcount); - - return true; - } + bool nextImpl() override; public: - ReadBufferFromIStream(std::istream & istr_, size_t size = DBMS_DEFAULT_BUFFER_SIZE) - : BufferWithOwnMemory(size), istr(istr_) {} + ReadBufferFromIStream(std::istream & istr_, size_t size = DBMS_DEFAULT_BUFFER_SIZE); }; } diff --git a/dbms/src/IO/WriteBufferFromOStream.cpp b/dbms/src/IO/WriteBufferFromOStream.cpp new file mode 100644 index 00000000000..c0225f77075 --- /dev/null +++ b/dbms/src/IO/WriteBufferFromOStream.cpp @@ -0,0 +1,54 @@ +#include +#include + + +namespace DB +{ + +namespace ErrorCodes +{ + extern const int CANNOT_WRITE_TO_OSTREAM; +} + +void WriteBufferFromOStream::nextImpl() +{ + if (!offset()) + return; + + ostr->write(working_buffer.begin(), offset()); + ostr->flush(); + + if (!ostr->good()) + throw Exception("Cannot write to ostream", ErrorCodes::CANNOT_WRITE_TO_OSTREAM); +} + +WriteBufferFromOStream::WriteBufferFromOStream( + size_t size, + char * existing_memory, + size_t alignment) + : BufferWithOwnMemory(size, existing_memory, alignment) +{ +} + +WriteBufferFromOStream::WriteBufferFromOStream( + std::ostream & ostr_, + size_t size, + char * existing_memory, + size_t alignment) + : BufferWithOwnMemory(size, existing_memory, alignment), ostr(&ostr_) +{ +} + +WriteBufferFromOStream::~WriteBufferFromOStream() +{ + try + { + next(); + } + catch (...) + { + tryLogCurrentException(__PRETTY_FUNCTION__); + } +} + +} diff --git a/dbms/src/IO/WriteBufferFromOStream.h b/dbms/src/IO/WriteBufferFromOStream.h index 355e4fb7e46..6a323be34d7 100644 --- a/dbms/src/IO/WriteBufferFromOStream.h +++ b/dbms/src/IO/WriteBufferFromOStream.h @@ -2,8 +2,6 @@ #include -#include - #include #include @@ -11,53 +9,23 @@ namespace DB { -namespace ErrorCodes -{ - extern const int CANNOT_WRITE_TO_OSTREAM; -} - class WriteBufferFromOStream : public BufferWithOwnMemory { protected: std::ostream * ostr; - void nextImpl() override - { - if (!offset()) - return; - - ostr->write(working_buffer.begin(), offset()); - ostr->flush(); - - if (!ostr->good()) - throw Exception("Cannot write to ostream", ErrorCodes::CANNOT_WRITE_TO_OSTREAM); - } - - WriteBufferFromOStream(size_t size = DBMS_DEFAULT_BUFFER_SIZE, char * existing_memory = nullptr, size_t alignment = 0) - : BufferWithOwnMemory(size, existing_memory, alignment) - { - } + void nextImpl() override; + WriteBufferFromOStream(size_t size = DBMS_DEFAULT_BUFFER_SIZE, char * existing_memory = nullptr, size_t alignment = 0); public: WriteBufferFromOStream( std::ostream & ostr_, size_t size = DBMS_DEFAULT_BUFFER_SIZE, char * existing_memory = nullptr, - size_t alignment = 0) - : BufferWithOwnMemory(size, existing_memory, alignment), ostr(&ostr_) {} + size_t alignment = 0); - ~WriteBufferFromOStream() override - { - try - { - next(); - } - catch (...) - { - tryLogCurrentException(__PRETTY_FUNCTION__); - } - } + ~WriteBufferFromOStream() override; }; }