Commit 0626e3d2 authored by Max Kellermann's avatar Max Kellermann

input/buffering: at end of input, seek to first hole

parent 869d2150
......@@ -127,6 +127,22 @@ BufferingInputStream::Read(std::unique_lock<Mutex> &lock, void *ptr, size_t s)
}
}
size_t
BufferingInputStream::FindFirstHole() const noexcept
{
auto r = buffer.Read(0);
if (r.undefined_size > 0)
/* a hole at the beginning */
return 0;
if (r.defined_buffer.size < size())
/* a hole in the middle */
return r.defined_buffer.size;
/* the file has been read completely */
return INVALID_OFFSET;
}
void
BufferingInputStream::RunThread() noexcept
{
......@@ -173,7 +189,26 @@ BufferingInputStream::RunThread() noexcept
client_cond.notify_one();
OnBufferAvailable();
}
} else if (input->IsAvailable() && !input->IsEOF()) {
} else if (input->IsEOF()) {
/* our input has reached its end: prepare
reading the first remaining hole */
size_t new_offset = FindFirstHole();
if (new_offset == INVALID_OFFSET) {
/* the file has been read completely */
wake_cond.wait(lock);
continue;
}
/* seek to the first hole */
try {
input->Seek(lock, new_offset);
} catch (...) {
read_error = std::current_exception();
client_cond.notify_one();
OnBufferAvailable();
}
} else if (input->IsAvailable()) {
const auto read_offset = input->GetOffset();
auto w = buffer.Write(read_offset);
......
......@@ -63,6 +63,8 @@ class BufferingInputStream : InputStreamHandler {
std::exception_ptr read_error, seek_error;
static constexpr size_t INVALID_OFFSET = ~size_t(0);
public:
explicit BufferingInputStream(InputStreamPtr _input);
~BufferingInputStream() noexcept;
......@@ -84,6 +86,8 @@ protected:
virtual void OnBufferAvailable() noexcept {}
private:
size_t FindFirstHole() const noexcept;
void RunThread() noexcept;
/* virtual methods from class InputStreamHandler */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment