Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-30 02:32:40

0001 // Compressed string stream.
0002 //
0003 // Properties we need to have in this class:
0004 //
0005 // 1. Ability to use it as std::ostringstream for uncompressed writes.
0006 //
0007 // 2. Ability to use it as std::istringstream for uncompressed reads.
0008 //
0009 // 3. Ability to fill the compression buffer from an istream.
0010 //
0011 // 4. Ability to dump the compression buffer to an ostream.
0012 //
0013 // 5. Ability to convert data between compressed and uncompressed buffers.
0014 
0015 #ifndef GENERS_CSTRINGSTREAM_HH_
0016 #define GENERS_CSTRINGSTREAM_HH_
0017 
0018 #include <iostream>
0019 #include <vector>
0020 
0021 #include "Alignment/Geners/interface/CPP11_auto_ptr.hh"
0022 #include "Alignment/Geners/interface/CStringBuf.hh"
0023 #include "Alignment/Geners/interface/ZlibHandle.hh"
0024 
0025 namespace gs {
0026   class CStringStream : public std::basic_iostream<char> {
0027   public:
0028     enum CompressionMode { NOT_COMPRESSED = 0, ZLIB, BZIP2 };
0029 
0030     CStringStream(CompressionMode m, int compressionLevel, unsigned minSizeToCompress, unsigned bufSize);
0031 
0032     // Basic inspectors
0033     inline CompressionMode compressionMode() const { return mode_; }
0034     inline int compressionLevel() const { return compressionLevel_; }
0035     inline unsigned minSizeToCompress() const { return minSizeToCompress_; }
0036     inline std::size_t bufferSize() const { return comprBuf_.size(); }
0037 
0038     // "setCompressionMode" calls "reset" internally.
0039     // All unprocessed data will be lost.
0040     void setCompressionMode(CompressionMode m);
0041 
0042     // The sink must be set before calling "writeCompressed".
0043     // This is where the compressed data will be dumped.
0044     inline void setSink(std::ostream &os) { sink_ = &os; }
0045 
0046     // "writeCompressed" compresses the content of this stream
0047     // and dumps them to sink. The write pointer is repositioned
0048     // at the beginning of the stream. The compression mode is
0049     // returned (always NOT_COMPRESSED if the amount of data
0050     // was below "minSizeToCompress").
0051     CompressionMode writeCompressed();
0052 
0053     // Fill this stream from compressed data. Could be called
0054     // repetitively. Uncompressed data is appended at the end
0055     // (internal pointers are not reset).
0056     void readCompressed(std::istream &in, unsigned compressionCode, unsigned long long len);
0057 
0058     // Reposition both read and write pointers of the stream
0059     // at the beginning of the stream
0060     void reset();
0061 
0062     // Parse the compression mode. Returns "true" on success.
0063     static bool getCompressionModeByName(const char *name, CompressionMode *m);
0064 
0065     // String representation of the compression mode
0066     static std::string compressionModeName(CompressionMode m, bool useShortName = true);
0067 
0068     CStringStream(const CStringStream &) = delete;
0069     CStringStream &operator=(const CStringStream &) = delete;
0070 
0071   private:
0072     CStringBuf buf_;
0073     CompressionMode mode_;
0074     int compressionLevel_;
0075     unsigned minSizeToCompress_;
0076     std::vector<char> comprBuf_;
0077     std::vector<char> readBuf_;
0078     std::ostream *sink_;
0079     CPP11_auto_ptr<ZlibInflateHandle> inflator_;
0080     CPP11_auto_ptr<ZlibDeflateHandle> deflator_;
0081   };
0082 }  // namespace gs
0083 
0084 #endif  // GENERS_CSTRINGSTREAM_HH_