Fixed LimitReadBuffer [#CLICKHOUSE-2].

This commit is contained in:
Alexey Milovidov 2017-08-15 23:14:15 +03:00
parent f8513b932f
commit 4272265128
4 changed files with 25 additions and 6 deletions

View File

@ -22,7 +22,14 @@ bool LimitReadBuffer::nextImpl()
LimitReadBuffer::LimitReadBuffer(ReadBuffer & in_, size_t limit_)
: ReadBuffer(nullptr, 0), in(in_), limit(limit_) {}
: ReadBuffer(in_.position(), 0), in(in_), limit(limit_)
{
size_t remaining_bytes_in_buffer = in.buffer().end() - in.position();
if (remaining_bytes_in_buffer > limit)
remaining_bytes_in_buffer = limit;
working_buffer = Buffer(in.position(), in.position() + remaining_bytes_in_buffer);
}
LimitReadBuffer::~LimitReadBuffer()

View File

@ -14,11 +14,20 @@ int main(int argc, char ** argv)
size_t limit = std::stol(argv[1]);
ReadBufferFromFileDescriptor in(STDIN_FILENO);
LimitReadBuffer limit_in(in, limit);
WriteBufferFromFileDescriptor out(STDOUT_FILENO);
copyData(limit_in, out);
writeCString("--- first ---\n", out);
{
LimitReadBuffer limit_in(in, limit);
copyData(limit_in, out);
}
writeCString("\n--- second ---\n", out);
{
LimitReadBuffer limit_in(in, limit);
copyData(limit_in, out);
}
writeCString("\n--- the rest ---\n", out);
copyData(in, out);

View File

@ -1,3 +1,6 @@
--- first ---
Hello, wor
--- second ---
ld! Abcdef
--- the rest ---
ld!
ghijklmnopqrstuvwxyz.

View File

@ -1,2 +1,2 @@
#!/usr/bin/env bash
./limit_read_buffer 10 <<< "Hello, world!"
./limit_read_buffer 10 <<< 'Hello, world! Abcdefghijklmnopqrstuvwxyz.'