Abstracted out some repeated I/O helpers into a IoUtil and updated affected components.
Change-Id: I151b880b29c526cb1ccb9717fabc63d1842cd1c1
diff --git a/src/util/io-util.hpp b/src/util/io-util.hpp
new file mode 100644
index 0000000..d32ac65
--- /dev/null
+++ b/src/util/io-util.hpp
@@ -0,0 +1,156 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+* Copyright (c) 2016 Regents of the University of California.
+*
+* This file is part of the nTorrent codebase.
+*
+* nTorrent 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.
+*
+* nTorrent 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 nTorrent, e.g., in COPYING.md file. If not, see
+* <http://www.gnu.org/licenses/>.
+*
+* See AUTHORS for complete list of nTorrent authors and contributors.
+*/
+
+#ifndef INCLUDED_UTIL_IO_UTIL_H
+#define INCLUDED_UTIL_IO_UTIL_H
+
+#include <boost/filesystem.hpp>
+#include <boost/filesystem/fstream.hpp>
+
+#include <ndn-cxx/data.hpp>
+#include <ndn-cxx/name.hpp>
+#include <ndn-cxx/util/io.hpp>
+
+#include <set>
+#include <string>
+#include <vector>
+
+namespace fs = boost::filesystem;
+
+namespace ndn {
+namespace ntorrent {
+
+class TorrentFile;
+class FileManifest;
+
+class IoUtil {
+ public:
+ /*
+ * An enum used to identify the type of content to which a name refers
+ */
+ enum NAME_TYPE
+ {
+ FILE_MANIFEST,
+ TORRENT_FILE,
+ DATA_PACKET,
+ UNKNOWN
+ };
+
+ template<typename T>
+ static std::vector<T>
+ load_directory(const std::string& dirPath,
+ ndn::io::IoEncoding encoding = ndn::io::IoEncoding::BASE_64);
+
+ static std::vector<ndn::Data>
+ packetize_file(const fs::path& filePath,
+ const ndn::Name& commonPrefix,
+ size_t dataPacketSize,
+ size_t subManifestSize,
+ size_t subManifestNum);
+
+ /*
+ * @brief Write the @p segment torrent segment to disk at the specified path.
+ * @param segment The torrent file segment to be written to disk
+ * @param path The path at which to write the torrent file segment
+ * Write the segment to disk, return 'true' if data successfully written to disk 'false'
+ * otherwise. Behavior is undefined unless @segment is a correct segment for the torrent file of
+ * this manager and @p path is the directory used for all segments of this torrent file.
+ */
+ static bool
+ writeTorrentSegment(const TorrentFile& segment, const std::string& path);
+
+ /*
+ * @brief Write the @p manifest file manifest to disk at the specified @p path.
+ * @param manifest The file manifest to be written to disk
+ * @param path The path at which to write the file manifest
+ * Write the file manifest to disk, return 'true' if data successfully written to disk 'false'
+ * otherwise. Behavior is undefined unless @manifest is a correct file manifest for a file in the
+ * torrent file of this manager and @p path is the directory used for all file manifests of this
+ * torrent file.
+ */
+ static bool
+ writeFileManifest(const FileManifest& manifest, const std::string& path);
+
+ /*
+ * @brief Write @p packet composed of torrent date to disk.
+ * @param packet The data packet to be written to the disk
+ * @param os The output stream to which to write
+ * Write the Data packet to disk, return 'true' if data successfully written to disk 'false'
+ * otherwise. Behavior is undefined unless the corresponding file manifest has already been
+ * downloaded.
+ */
+ static bool
+ writeData(const Data& packet,
+ const FileManifest& manifest,
+ size_t subManifestSize,
+ fs::fstream& os);
+
+ /*
+ * @brief Read a data packet from the provided stream
+ * @param packetFullName The fullname of the expected Data packet
+ * @param manifest The file manifest for the requested Data packet
+ * @param subManifestSize The number of Data packets in each catalog for this Data packet
+ * @param is The stream from which to read the packet
+ * Read the data packet from the @p is stream, validate it against the provided @p packetFullName
+ * and @p manifest, if successful return a pointer to the packet, otherwise return nullptr.
+ */
+ static std::shared_ptr<Data>
+ readDataPacket(const Name& packetFullName,
+ const FileManifest& manifest,
+ size_t subManifestSize,
+ fs::fstream& is);
+
+ /*
+ * @brief Return the type of the specified name
+ */
+ static IoUtil::NAME_TYPE
+ findType(const Name& name);
+
+};
+
+template<typename T>
+inline std::vector<T>
+IoUtil::load_directory(const std::string& dirPath,
+ ndn::io::IoEncoding encoding) {
+ std::vector<T> structures;
+ std::set<std::string> fileNames;
+ if (fs::exists(dirPath)) {
+ for(fs::recursive_directory_iterator it(dirPath);
+ it != fs::recursive_directory_iterator();
+ ++it)
+ {
+ fileNames.insert(it->path().string());
+ }
+ for (const auto& f : fileNames) {
+ auto data_ptr = ndn::io::load<T>(f, encoding);
+ if (nullptr != data_ptr) {
+ structures.push_back(*data_ptr);
+ }
+ }
+ }
+ structures.shrink_to_fit();
+ return structures;
+}
+
+} // namespace ntorrent
+} // namespace ndn
+
+#endif // INCLUDED_UTIL_IO_UTIL_H
\ No newline at end of file