comparison OrthancFramework/Sources/DicomFormat/StreamBlockReader.cpp @ 4220:92a21efa5c96

reorganization of DicomStreamReader
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 30 Sep 2020 15:33:47 +0200
parents
children 3b70a2e6a06c
comparison
equal deleted inserted replaced
4219:b8ed2852a35d 4220:92a21efa5c96
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
23 #include "../PrecompiledHeaders.h"
24 #include "StreamBlockReader.h"
25
26 #include "../OrthancException.h"
27
28
29 namespace Orthanc
30 {
31 StreamBlockReader::StreamBlockReader(std::istream& stream) :
32 stream_(stream),
33 blockPos_(0),
34 processedBytes_(0)
35 {
36 }
37
38
39 void StreamBlockReader::Schedule(size_t blockSize)
40 {
41 if (!block_.empty())
42 {
43 throw OrthancException(ErrorCode_BadSequenceOfCalls);
44 }
45 else
46 {
47 block_.resize(blockSize);
48 blockPos_ = 0;
49 }
50 }
51
52
53 bool StreamBlockReader::Read(std::string& block)
54 {
55 if (block_.empty())
56 {
57 if (blockPos_ != 0)
58 {
59 throw OrthancException(ErrorCode_BadSequenceOfCalls);
60 }
61
62 block.clear();
63 return true;
64 }
65 else
66 {
67 while (blockPos_ < block_.size())
68 {
69 size_t remainingBytes = block_.size() - blockPos_;
70 std::streamsize r = stream_.readsome(&block_[blockPos_], remainingBytes);
71 if (r == 0)
72 {
73 return false;
74 }
75 else
76 {
77 blockPos_ += r;
78 }
79 }
80
81 processedBytes_ += block_.size();
82
83 block.swap(block_);
84 block_.clear();
85 return true;
86 }
87 }
88 }