ClickHouse/dbms/include/DB/IO/HexWriteBuffer.h
2017-03-09 07:26:17 +03:00

52 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <DB/IO/BufferWithOwnMemory.h>
#include <DB/IO/WriteBuffer.h>
/// Так как HexWriteBuffer часто создаётся во внутреннем цикле, сделаем у него размер буфера маленьким.
#define DBMS_HEX_WRITE_BUFFER_SIZE 32
namespace DB
{
/** Всё что в него пишут, переводит в HEX (большими буквами) и пишет в другой WriteBuffer.
*/
class HexWriteBuffer : public WriteBuffer
{
protected:
char buf[DBMS_HEX_WRITE_BUFFER_SIZE];
WriteBuffer & out;
void nextImpl() override
{
if (!offset())
return;
for (Position p = working_buffer.begin(); p != pos; ++p)
{
out.write("0123456789ABCDEF"[static_cast<unsigned char>(*p) >> 4]);
out.write("0123456789ABCDEF"[static_cast<unsigned char>(*p) & 0xF]);
}
}
public:
HexWriteBuffer(WriteBuffer & out_) : WriteBuffer(buf, sizeof(buf)), out(out_) {}
~HexWriteBuffer() override
{
try
{
nextImpl();
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
}
};
}