NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
io.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2022 Regents of the University of California.
4  *
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 "ndn-cxx/util/io.hpp"
32 
33 namespace ndn {
34 namespace io {
35 
36 shared_ptr<Buffer>
37 loadBuffer(std::istream& is, IoEncoding encoding)
38 {
39  namespace t = ndn::security::transform;
40 
41  OBufferStream os;
42  try {
43  switch (encoding) {
44  case NO_ENCODING:
45  t::streamSource(is) >> t::streamSink(os);
46  return os.buf();
47  case BASE64:
48  t::streamSource(is) >> t::stripSpace("\n") >> t::base64Decode(false) >> t::streamSink(os);
49  return os.buf();
50  case HEX:
52  return os.buf();
53  }
54  }
55  catch (const std::runtime_error& e) {
56  NDN_THROW_NESTED(Error(e.what()));
57  }
58 
59  NDN_THROW(std::invalid_argument("Unknown IoEncoding " + to_string(encoding)));
60 }
61 
62 void
63 saveBuffer(span<const uint8_t> buf, std::ostream& os, IoEncoding encoding)
64 {
65  namespace t = ndn::security::transform;
66 
67  try {
68  switch (encoding) {
69  case NO_ENCODING:
70  t::bufferSource(buf) >> t::streamSink(os);
71  return;
72  case BASE64:
74  return;
75  case HEX:
76  t::bufferSource(buf) >> t::hexEncode(true) >> t::streamSink(os);
77  return;
78  }
79  }
80  catch (const std::runtime_error& e) {
81  NDN_THROW_NESTED(Error(e.what()));
82  }
83 
84  NDN_THROW(std::invalid_argument("Unknown IoEncoding " + to_string(encoding)));
85 }
86 
87 } // namespace io
88 } // namespace ndn
#define NDN_THROW_NESTED(e)
Definition: exception.hpp:71
Copyright (c) 2011-2015 Regents of the University of California.
std::string to_string(const T &val)
Definition: backports.hpp:86
unique_ptr< Transform > base64Decode(bool expectNewlineEvery64Bytes)
unique_ptr< Transform > hexEncode(bool useUpperCase)
Definition: hex-encode.cpp:70
unique_ptr< Transform > stripSpace(const char *whitespaces)
constructs a StripSpace transform
Definition: strip-space.cpp:54
#define NDN_THROW(e)
Definition: exception.hpp:61
Hexadecimal encoding.
Definition: io.hpp:58
Raw binary, without encoding.
Definition: io.hpp:45
unique_ptr< Sink > streamSink(std::ostream &os)
Definition: stream-sink.cpp:53
unique_ptr< Transform > hexDecode()
Definition: hex-decode.cpp:115
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
implements an output stream that constructs ndn::Buffer
Base64 encoding.
Definition: io.hpp:52
void saveBuffer(span< const uint8_t > buf, std::ostream &os, IoEncoding encoding)
Writes a sequence of bytes to a stream.
Definition: io.cpp:63
unique_ptr< Transform > base64Encode(bool needBreak)
shared_ptr< Buffer > loadBuffer(std::istream &is, IoEncoding encoding)
Reads bytes from a stream until EOF.
Definition: io.cpp:37
IoEncoding
Indicates how a file or stream of bytes is encoded.
Definition: io.hpp:42