blob: 33b26ae46a97197f190f84392385e41be16a38f7 [file] [log] [blame]
Yingdi Yu3168c302015-07-04 16:45:40 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa84f4642017-08-23 16:14:51 -04002/*
3 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yu3168c302015-07-04 16:45:40 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "stream-source.hpp"
Davide Pesaventoa84f4642017-08-23 16:14:51 -040023
24#include <istream>
Yingdi Yu3168c302015-07-04 16:45:40 -070025#include <vector>
26
27namespace ndn {
28namespace security {
29namespace transform {
30
31const std::size_t StreamSource::DEFAULT_BUFFER_LEN = 1024;
32
33StreamSource::StreamSource(std::istream& is, size_t bufferSize)
34 : Source()
35 , m_is(is)
36 , m_bufferSize(bufferSize)
37{
38 BOOST_ASSERT(bufferSize > 0);
39}
40
41void
42StreamSource::doPump()
43{
44 BOOST_ASSERT(m_next != nullptr);
45
46 std::vector<uint8_t> buffer(m_bufferSize);
47 size_t dataOffset = 0;
48 size_t dataLen = 0;
49
50 while (dataLen > 0 || !m_is.eof()) {
51 if (dataLen > 0) {
52 // we have some leftover, handle them first
53 size_t nBytesWritten = m_next->write(&buffer[dataOffset], dataLen);
54
55 dataOffset += nBytesWritten;
56 dataLen -= nBytesWritten;
57 }
Junxiao Shiba6baaf2016-08-20 02:59:20 +000058 else if (!m_is) {
Yingdi Yu3168c302015-07-04 16:45:40 -070059 BOOST_THROW_EXCEPTION(Error(getIndex(), "Input stream in bad state"));
60 }
61 else if (m_is.good()) {
62 m_is.read(reinterpret_cast<char*>(&buffer.front()), buffer.size());
63 dataOffset = 0;
64 dataLen = m_is.gcount();
65 }
66 }
67 m_next->end();
68}
69
70} // namespace transform
71} // namespace security
72} // namespace ndn