# HG changeset patch # User Sebastien Jodogne # Date 1779105770 -7200 # Node ID cdf8b88c60c80401ff7d400a332d38ad86817d0d # Parent e2241b1bcac8ee73b95a44db176aa2c28a8fc69d allow BufferHttpSender to be created from raw buffer diff -r e2241b1bcac8 -r cdf8b88c60c8 OrthancFramework/Sources/HttpServer/BufferHttpSender.cpp --- a/OrthancFramework/Sources/HttpServer/BufferHttpSender.cpp Mon May 18 12:57:36 2026 +0200 +++ b/OrthancFramework/Sources/HttpServer/BufferHttpSender.cpp Mon May 18 14:02:50 2026 +0200 @@ -30,14 +30,34 @@ namespace Orthanc { + void BufferHttpSender::ResetFromInternalBuffer() + { + if (internalBuffer_.get() == NULL) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + else if (internalBuffer_->empty()) + { + SetBuffer(NULL, 0); + } + else + { + SetBuffer(internalBuffer_->c_str(), internalBuffer_->size()); + } + } + + BufferHttpSender::BufferHttpSender() : + data_(NULL), + size_(0), position_(0), chunkSize_(0), currentChunkSize_(0) { } - void BufferHttpSender::SetBuffer(const std::string& buffer) + void BufferHttpSender::SetBuffer(const void* data, + size_t size) { if (position_ != 0) { @@ -45,25 +65,23 @@ } else { - buffer_ = buffer; + data_ = reinterpret_cast(data); + size_ = size; } } + void BufferHttpSender::SetBuffer(const std::string& buffer) + { + internalBuffer_.reset(new std::string); + *internalBuffer_ = buffer; + ResetFromInternalBuffer(); + } + void BufferHttpSender::SwapBuffer(std::string& buffer) { - if (position_ != 0) - { - throw OrthancException(ErrorCode_BadSequenceOfCalls); - } - else - { - buffer_.swap(buffer); - } - } - - const std::string& BufferHttpSender::GetBuffer() const - { - return buffer_; + internalBuffer_.reset(new std::string); + internalBuffer_->swap(buffer); + ResetFromInternalBuffer(); } void BufferHttpSender::SetChunkSize(size_t chunkSize) @@ -73,23 +91,23 @@ uint64_t BufferHttpSender::GetContentLength() { - return buffer_.size(); + return size_; } bool BufferHttpSender::ReadNextChunk() { - assert(position_ + currentChunkSize_ <= buffer_.size()); + assert(position_ + currentChunkSize_ <= size_); position_ += currentChunkSize_; - if (position_ == buffer_.size()) + if (position_ == size_) { return false; } else { - currentChunkSize_ = buffer_.size() - position_; + currentChunkSize_ = size_ - position_; if (chunkSize_ != 0 && currentChunkSize_ > chunkSize_) @@ -104,7 +122,7 @@ const char* BufferHttpSender::GetChunkContent() { - return buffer_.c_str() + position_; + return data_ + position_; } diff -r e2241b1bcac8 -r cdf8b88c60c8 OrthancFramework/Sources/HttpServer/BufferHttpSender.h --- a/OrthancFramework/Sources/HttpServer/BufferHttpSender.h Mon May 18 12:57:36 2026 +0200 +++ b/OrthancFramework/Sources/HttpServer/BufferHttpSender.h Mon May 18 14:02:50 2026 +0200 @@ -30,21 +30,27 @@ class ORTHANC_PUBLIC BufferHttpSender : public HttpFileSender { private: - std::string buffer_; + std::unique_ptr internalBuffer_; + + const char* data_; + size_t size_; size_t position_; size_t chunkSize_; size_t currentChunkSize_; + void ResetFromInternalBuffer(); + public: BufferHttpSender(); + void SetBuffer(const void* data, + size_t size); + void SetBuffer(const std::string& buffer); // This flavor is more efficient void SwapBuffer(std::string& buffer); - const std::string& GetBuffer() const; - // This is for test purpose. If "chunkSize" is set to "0" (the // default), the entire buffer is consumed at once. void SetChunkSize(size_t chunkSize);