ndn-cxx 0.9.0-42-g8cff8f06
Loading...
Searching...
No Matches
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-2026 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
33namespace ndn::io {
34
35shared_ptr<Buffer>
36loadBuffer(std::istream& is, IoEncoding encoding)
37{
38 namespace t = ndn::security::transform;
39
40 if (!is)
41 NDN_THROW(Error("Cannot read from input stream"));
42
44 try {
45 switch (encoding) {
46 case NO_ENCODING:
47 t::streamSource(is) >> t::streamSink(os);
48 break;
49 case BASE64:
50 t::streamSource(is) >> t::stripSpace("\n") >> t::base64Decode(false) >> t::streamSink(os);
51 break;
52 case HEX:
53 t::streamSource(is) >> t::hexDecode() >> t::streamSink(os);
54 break;
55 default:
56 NDN_THROW(std::invalid_argument("Unknown IoEncoding " + std::to_string(encoding)));
57 }
58 }
59 catch (const std::runtime_error& e) {
60 NDN_THROW_NESTED(Error(e.what()));
61 }
62
63 return os.buf();
64}
65
66void
67saveBuffer(span<const uint8_t> buf, std::ostream& os, IoEncoding encoding)
68{
69 namespace t = ndn::security::transform;
70
71 try {
72 switch (encoding) {
73 case NO_ENCODING:
74 t::bufferSource(buf) >> t::streamSink(os);
75 break;
76 case BASE64:
77 t::bufferSource(buf) >> t::base64Encode() >> t::streamSink(os);
78 break;
79 case HEX:
80 t::bufferSource(buf) >> t::hexEncode(true) >> t::streamSink(os);
81 break;
82 default:
83 NDN_THROW(std::invalid_argument("Unknown IoEncoding " + std::to_string(encoding)));
84 }
85 }
86 catch (const std::runtime_error& e) {
87 NDN_THROW_NESTED(Error(e.what()));
88 }
89
90 if (!os)
91 NDN_THROW(Error("Cannot write to output stream"));
92}
93
94} // namespace ndn::io
An output stream that writes to a Buffer.
std::shared_ptr< Buffer > buf()
Return a shared pointer to the underlying buffer.
#define NDN_THROW_NESTED(e)
Definition exception.hpp:65
#define NDN_THROW(e)
Definition exception.hpp:56
Definition io.cpp:33
void saveBuffer(span< const uint8_t > buf, std::ostream &os, IoEncoding encoding)
Writes a sequence of bytes to a stream.
Definition io.cpp:67
IoEncoding
Indicates how a file or stream of bytes is encoded.
Definition io.hpp:41
@ NO_ENCODING
Raw binary, without encoding.
Definition io.hpp:45
@ HEX
Hexadecimal encoding.
Definition io.hpp:60
@ BASE64
Base64 encoding.
Definition io.hpp:53
shared_ptr< Buffer > loadBuffer(std::istream &is, IoEncoding encoding)
Reads bytes from a stream until EOF.
Definition io.cpp:36