1 /**
2  * Orthanc - A Lightweight, RESTful DICOM Store
3  * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4  * Department, University Hospital of Liege, Belgium
5  * Copyright (C) 2017-2020 Osimis S.A., Belgium
6  *
7  * This program is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation, either version 3 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this program. If not, see
19  * <http://www.gnu.org/licenses/>.
20  **/
21 
22 #include "../PrecompiledHeaders.h"
23 #include "BufferHttpSender.h"
24 
25 #include "../OrthancException.h"
26 
27 #include <cassert>
28 
29 namespace Orthanc
30 {
BufferHttpSender()31   BufferHttpSender::BufferHttpSender() :
32     position_(0),
33     chunkSize_(0),
34     currentChunkSize_(0)
35   {
36   }
37 
GetBuffer()38   std::string &BufferHttpSender::GetBuffer()
39   {
40     return buffer_;
41   }
42 
GetBuffer() const43   const std::string &BufferHttpSender::GetBuffer() const
44   {
45     return buffer_;
46   }
47 
SetChunkSize(size_t chunkSize)48   void BufferHttpSender::SetChunkSize(size_t chunkSize)
49   {
50     chunkSize_ = chunkSize;
51   }
52 
GetContentLength()53   uint64_t BufferHttpSender::GetContentLength()
54   {
55     return buffer_.size();
56   }
57 
58 
ReadNextChunk()59   bool BufferHttpSender::ReadNextChunk()
60   {
61     assert(position_ + currentChunkSize_ <= buffer_.size());
62 
63     position_ += currentChunkSize_;
64 
65     if (position_ == buffer_.size())
66     {
67       return false;
68     }
69     else
70     {
71       currentChunkSize_ = buffer_.size() - position_;
72 
73       if (chunkSize_ != 0 &&
74           currentChunkSize_ > chunkSize_)
75       {
76         currentChunkSize_ = chunkSize_;
77       }
78 
79       return true;
80     }
81   }
82 
83 
GetChunkContent()84   const char* BufferHttpSender::GetChunkContent()
85   {
86     return buffer_.c_str() + position_;
87   }
88 
89 
GetChunkSize()90   size_t BufferHttpSender::GetChunkSize()
91   {
92     return currentChunkSize_;
93   }
94 }
95