diff --git a/src/input/AsyncInputStream.cxx b/src/input/AsyncInputStream.cxx index 8aefac0eb..9133dada4 100644 --- a/src/input/AsyncInputStream.cxx +++ b/src/input/AsyncInputStream.cxx @@ -217,23 +217,28 @@ AsyncInputStream::CommitWriteBuffer(size_t nbytes) noexcept } void -AsyncInputStream::AppendToBuffer(const void *data, size_t append_size) noexcept +AsyncInputStream::AppendToBuffer(std::span src) noexcept { auto w = buffer.Write(); assert(!w.empty()); - size_t nbytes = std::min(w.size(), append_size); - memcpy(w.data(), data, nbytes); - buffer.Append(nbytes); + std::span second{}; - const size_t remaining = append_size - nbytes; - if (remaining > 0) { + if (w.size() < src.size()) { + second = src.subspan(w.size()); + src = src.first(w.size()); + } + + std::copy(src.begin(), src.end(), w.begin()); + buffer.Append(src.size()); + + if (!second.empty()) { w = buffer.Write(); assert(!w.empty()); - assert(w.size() >= remaining); + assert(w.size() >= second.size()); - memcpy(w.data(), (const std::byte *)data + nbytes, remaining); - buffer.Append(remaining); + std::copy(second.begin(), second.end(), w.begin()); + buffer.Append(second.size()); } if (!IsReady()) diff --git a/src/input/AsyncInputStream.hxx b/src/input/AsyncInputStream.hxx index a7160af8a..80e6c40a5 100644 --- a/src/input/AsyncInputStream.hxx +++ b/src/input/AsyncInputStream.hxx @@ -139,7 +139,7 @@ protected: * Append data to the buffer. The size must fit into the * buffer; see GetBufferSpace(). */ - void AppendToBuffer(const void *data, size_t append_size) noexcept; + void AppendToBuffer(std::span src) noexcept; /** * Implement code here that will resume the stream after it diff --git a/src/input/plugins/CurlInputPlugin.cxx b/src/input/plugins/CurlInputPlugin.cxx index db1da42d5..fe6e4ae7c 100644 --- a/src/input/plugins/CurlInputPlugin.cxx +++ b/src/input/plugins/CurlInputPlugin.cxx @@ -353,7 +353,7 @@ CurlInputStream::OnData(std::span data) throw CurlResponseHandler::Pause{}; } - AppendToBuffer(data.data(), data.size()); + AppendToBuffer(data); } void diff --git a/src/input/plugins/NfsInputPlugin.cxx b/src/input/plugins/NfsInputPlugin.cxx index 12da41ab0..b64cf7388 100644 --- a/src/input/plugins/NfsInputPlugin.cxx +++ b/src/input/plugins/NfsInputPlugin.cxx @@ -164,7 +164,8 @@ NfsInputStream::OnNfsFileRead(std::span src) noexcept const std::scoped_lock protect(mutex); assert(!IsBufferFull()); assert(IsBufferFull() == (GetBufferSpace() == 0)); - AppendToBuffer(src.data(), src.size()); + + AppendToBuffer(src); next_offset += src.size();