File indexing completed on 2024-04-06 11:56:18
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef GENERS_COMPRESSEDIO_HH_
0009 #define GENERS_COMPRESSEDIO_HH_
0010
0011 #include "Alignment/Geners/interface/CStringStream.hh"
0012 #include "Alignment/Geners/interface/GenericIO.hh"
0013
0014 namespace gs {
0015
0016 template <class Item>
0017 bool write_compressed_item(std::ostream &os,
0018 const Item &item,
0019 CStringStream::CompressionMode m = CStringStream::ZLIB,
0020 int compressionLevel = -1,
0021 unsigned minSizeToCompress = 1024U,
0022 unsigned bufSize = 1048576U);
0023
0024 template <class Item>
0025 void restore_compressed_item(std::istream &in, Item *item);
0026
0027 template <class Item>
0028 std::unique_ptr<Item> read_compressed_item(std::istream &in);
0029 }
0030
0031 namespace gs {
0032 template <class Item>
0033 inline bool write_compressed_item(std::ostream &os,
0034 const Item &item,
0035 const CStringStream::CompressionMode m,
0036 const int compressionLevel,
0037 const unsigned minSizeToCompress,
0038 const unsigned bufSize) {
0039 CStringStream cs(m, compressionLevel, minSizeToCompress, bufSize);
0040 unsigned compressionCode = 0;
0041 long long len = 0;
0042 const std::streampos base = os.tellp();
0043 write_pod(os, len);
0044 write_pod(os, compressionCode);
0045 if (os.fail() || os.bad())
0046 return false;
0047 const std::streampos start = os.tellp();
0048 cs.setSink(os);
0049 if (!write_item(cs, item))
0050 return false;
0051 cs.flush();
0052 compressionCode = static_cast<unsigned>(cs.writeCompressed());
0053 const std::streampos now = os.tellp();
0054 const std::streamoff off = now - start;
0055 len = off;
0056 os.seekp(base);
0057 write_pod(os, len);
0058 write_pod(os, compressionCode);
0059 os.seekp(now);
0060 return !(cs.fail() || cs.bad() || os.fail() || os.bad());
0061 }
0062
0063 template <class Item>
0064 inline void restore_compressed_item(std::istream &is, Item *item) {
0065 long long len;
0066 read_pod(is, &len);
0067 unsigned compressionCode;
0068 read_pod(is, &compressionCode);
0069 CStringStream::CompressionMode m = static_cast<CStringStream::CompressionMode>(compressionCode);
0070 CStringStream cs(m, -1, 1024U, 1048576U);
0071 cs.readCompressed(is, compressionCode, len);
0072 if (!is.good())
0073 throw IOReadFailure(
0074 "In restore_compressed_item: "
0075 "input stream failure");
0076 restore_item(cs, item);
0077 }
0078
0079 template <class Item>
0080 inline std::unique_ptr<Item> read_compressed_item(std::istream &is) {
0081 long long len;
0082 read_pod(is, &len);
0083 unsigned compressionCode;
0084 read_pod(is, &compressionCode);
0085 CStringStream::CompressionMode m = static_cast<CStringStream::CompressionMode>(compressionCode);
0086 CStringStream cs(m, -1, 1024U, 1048576U);
0087 cs.readCompressed(is, compressionCode, len);
0088 if (!is.good())
0089 throw IOReadFailure(
0090 "In read_compressed_item: "
0091 "input stream failure");
0092 return read_item<Item, std::istream>(cs);
0093 }
0094 }
0095
0096 #endif