util: ndn::io::loadBlock and saveBlock

refs #3741

Change-Id: I5eafc7ebe82e289efc74471ab5debf36786d3d74
diff --git a/src/util/io.cpp b/src/util/io.cpp
new file mode 100644
index 0000000..84db1e4
--- /dev/null
+++ b/src/util/io.cpp
@@ -0,0 +1,88 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2016 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "io.hpp"
+#include "../encoding/buffer-stream.hpp"
+#include "../security/transform.hpp"
+
+namespace ndn {
+namespace io {
+
+optional<Block>
+loadBlock(std::istream& is, IoEncoding encoding)
+{
+  namespace t = ndn::security::transform;
+
+  OBufferStream os;
+  try {
+    switch (encoding) {
+      case NO_ENCODING:
+        t::streamSource(is) >> t::streamSink(os);
+        break;
+      case BASE_64:
+        t::streamSource(is) >> t::base64Decode() >> t::streamSink(os);
+        break;
+      case HEX:
+        t::streamSource(is) >> t::hexDecode() >> t::streamSink(os);
+        break;
+      default:
+        return nullopt;
+    }
+  }
+  catch (const t::Error&) {
+    return nullopt;
+  }
+
+  try {
+    return make_optional<Block>(os.buf());
+  }
+  catch (const tlv::Error&) {
+    return nullopt;
+  }
+}
+
+void
+saveBlock(const Block& block, std::ostream& os, IoEncoding encoding)
+{
+  namespace t = ndn::security::transform;
+
+  try {
+    switch (encoding) {
+      case NO_ENCODING:
+        t::bufferSource(block.wire(), block.size()) >> t::streamSink(os);
+        break;
+      case BASE_64:
+        t::bufferSource(block.wire(), block.size()) >> t::base64Encode() >> t::streamSink(os);
+        break;
+      case HEX:
+        t::bufferSource(block.wire(), block.size()) >> t::hexEncode(true) >> t::streamSink(os);
+        break;
+      default:
+        BOOST_THROW_EXCEPTION(Error("unrecognized IoEncoding"));
+    }
+  }
+  catch (const t::Error& e) {
+    BOOST_THROW_EXCEPTION(Error(e.what()));
+  }
+}
+
+} // namespace io
+} // namespace ndn
diff --git a/src/util/io.hpp b/src/util/io.hpp
index 7af2a9e..a169113 100644
--- a/src/util/io.hpp
+++ b/src/util/io.hpp
@@ -23,8 +23,6 @@
 #define NDN_UTIL_IO_HPP
 
 #include "../encoding/block.hpp"
-#include "../encoding/buffer-stream.hpp"
-#include "../security/transform.hpp"
 
 #include <iostream>
 #include <fstream>
@@ -50,6 +48,13 @@
   HEX ///< uppercase hexadecimal encoding
 };
 
+/** \brief loads a TLV block from a stream
+ *  \return if success, the Block and true
+ *          otherwise, a default-constructed Block and false
+ */
+optional<Block>
+loadBlock(std::istream& is, IoEncoding encoding = BASE_64);
+
 /** \brief loads a TLV element from a stream
  *  \tparam T type of TLV element; T must be WireDecodable,
  *            and must have a Error nested type
@@ -59,30 +64,14 @@
 shared_ptr<T>
 load(std::istream& is, IoEncoding encoding = BASE_64)
 {
-  OBufferStream os;
-  try {
-    namespace t = ndn::security::transform;
-    switch (encoding) {
-      case NO_ENCODING:
-        t::streamSource(is) >> t::streamSink(os);
-        break;
-      case BASE_64:
-        t::streamSource(is) >> t::base64Decode() >> t::streamSink(os);
-        break;
-      case HEX:
-        t::streamSource(is) >> t::hexDecode() >> t::streamSink(os);
-        break;
-      default:
-        return nullptr;
-    }
-  }
-  catch (const ndn::security::transform::Error&) {
+  optional<Block> block = loadBlock(is, encoding);
+  if (!block) {
     return nullptr;
   }
 
   try {
     auto obj = make_shared<T>();
-    obj->wireDecode(Block(os.buf()));
+    obj->wireDecode(*block);
     return obj;
   }
   catch (const typename T::Error& e) {
@@ -103,6 +92,12 @@
   return load<T>(is, encoding);
 }
 
+/** \brief saves a TLV block to a stream
+ *  \throw Error error during saving
+ */
+void
+saveBlock(const Block& block, std::ostream& os, IoEncoding encoding = BASE_64);
+
 /** \brief saves a TLV element to a stream
  *  \tparam T type of TLV element; T must be WireEncodable,
  *            and must have a Error nested type
@@ -123,25 +118,7 @@
     BOOST_THROW_EXCEPTION(Error(e.what()));
   }
 
-  try {
-    namespace t = ndn::security::transform;
-    switch (encoding) {
-      case NO_ENCODING:
-        t::bufferSource(block.wire(), block.size()) >> t::streamSink(os);
-        break;
-      case BASE_64:
-        t::bufferSource(block.wire(), block.size()) >> t::base64Encode() >> t::streamSink(os);
-        break;
-      case HEX:
-        t::bufferSource(block.wire(), block.size()) >> t::hexEncode(true) >> t::streamSink(os);
-        break;
-      default:
-        BOOST_THROW_EXCEPTION(Error("unrecognized IoEncoding"));
-    }
-  }
-  catch (const ndn::security::transform::Error& e) {
-    BOOST_THROW_EXCEPTION(Error(e.what()));
-  }
+  saveBlock(block, os, encoding);
 }
 
 /** \brief saves a TLV element to a file
diff --git a/tests/unit-tests/security/dummy-keychain.cpp b/tests/unit-tests/security/dummy-keychain.cpp
index 524fc0c..947bebf 100644
--- a/tests/unit-tests/security/dummy-keychain.cpp
+++ b/tests/unit-tests/security/dummy-keychain.cpp
@@ -20,7 +20,7 @@
  */
 
 #include "dummy-keychain.hpp"
-
+#include "encoding/buffer-stream.hpp"
 #include "util/io.hpp"
 #include <boost/iostreams/device/array.hpp>
 
diff --git a/tools/ndnsec/util.hpp b/tools/ndnsec/util.hpp
index 696d348..f096515 100644
--- a/tools/ndnsec/util.hpp
+++ b/tools/ndnsec/util.hpp
@@ -37,7 +37,9 @@
 #include <boost/asio.hpp>
 #include <boost/exception/all.hpp>
 
+#include "encoding/buffer-stream.hpp"
 #include "security/key-chain.hpp"
+#include "security/transform.hpp"
 #include "util/io.hpp"
 
 bool