2016-06-07 08:23:15 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstring>
|
2017-02-09 10:10:13 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <iostream>
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <IO/BufferBase.h>
|
2016-06-07 08:23:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int CANNOT_WRITE_AFTER_END_OF_BUFFER;
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/** A simple abstract class for buffered data writing (char sequences) somewhere.
|
|
|
|
* Unlike std::ostream, it provides access to the internal buffer,
|
|
|
|
* and also allows you to manually manage the position inside the buffer.
|
2016-06-07 08:23:15 +00:00
|
|
|
*
|
2017-05-28 14:29:40 +00:00
|
|
|
* The successors must implement the nextImpl() method.
|
2016-06-07 08:23:15 +00:00
|
|
|
*/
|
|
|
|
class WriteBuffer : public BufferBase
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
WriteBuffer(Position ptr, size_t size) : BufferBase(ptr, size, 0) {}
|
|
|
|
void set(Position ptr, size_t size) { BufferBase::set(ptr, size, 0); }
|
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/** write the data in the buffer (from the beginning of the buffer to the current position);
|
|
|
|
* set the position to the beginning; throw an exception, if something is wrong
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
|
|
|
inline void next()
|
|
|
|
{
|
|
|
|
if (!offset())
|
|
|
|
return;
|
|
|
|
bytes += offset();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
nextImpl();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2017-05-28 14:29:40 +00:00
|
|
|
/** If the nextImpl() call was unsuccessful, move the cursor to the beginning,
|
|
|
|
* so that later (for example, when the stack was expanded) there was no second attempt to write data.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
|
|
|
pos = working_buffer.begin();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = working_buffer.begin();
|
|
|
|
}
|
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/** it is desirable in the successors to place the next() call in the destructor,
|
|
|
|
* so that the last data is written
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
|
|
|
virtual ~WriteBuffer() {}
|
|
|
|
|
|
|
|
|
|
|
|
inline void nextIfAtEnd()
|
|
|
|
{
|
|
|
|
if (!hasPendingData())
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void write(const char * from, size_t n)
|
|
|
|
{
|
|
|
|
size_t bytes_copied = 0;
|
|
|
|
|
|
|
|
while (bytes_copied < n)
|
|
|
|
{
|
|
|
|
nextIfAtEnd();
|
|
|
|
size_t bytes_to_copy = std::min(static_cast<size_t>(working_buffer.end() - pos), n - bytes_copied);
|
|
|
|
std::memcpy(pos, from + bytes_copied, bytes_to_copy);
|
|
|
|
pos += bytes_to_copy;
|
|
|
|
bytes_copied += bytes_to_copy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void write(char x)
|
|
|
|
{
|
|
|
|
nextIfAtEnd();
|
|
|
|
*pos = x;
|
|
|
|
++pos;
|
|
|
|
}
|
2016-06-07 08:23:15 +00:00
|
|
|
|
|
|
|
private:
|
2017-05-28 14:29:40 +00:00
|
|
|
/** Write the data in the buffer (from the beginning of the buffer to the current position).
|
|
|
|
* Throw an exception if something is wrong.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
|
|
|
virtual void nextImpl() { throw Exception("Cannot write after end of buffer.", ErrorCodes::CANNOT_WRITE_AFTER_END_OF_BUFFER); };
|
2016-06-07 08:23:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-02-09 10:10:13 +00:00
|
|
|
using WriteBufferPtr = std::shared_ptr<WriteBuffer>;
|
|
|
|
|
|
|
|
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|