DeflatingStream.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // DeflatingStream.h
  3. //
  4. // $Id: //poco/1.3/Foundation/include/Poco/DeflatingStream.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Streams
  8. // Module: ZLibStream
  9. //
  10. // Definition of the DeflatingStream class.
  11. //
  12. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // Permission is hereby granted, free of charge, to any person or organization
  16. // obtaining a copy of the software and accompanying documentation covered by
  17. // this license (the "Software") to use, reproduce, display, distribute,
  18. // execute, and transmit the Software, and to prepare derivative works of the
  19. // Software, and to permit third-parties to whom the Software is furnished to
  20. // do so, all subject to the following:
  21. //
  22. // The copyright notices in the Software and this entire statement, including
  23. // the above license grant, this restriction and the following disclaimer,
  24. // must be included in all copies of the Software, in whole or in part, and
  25. // all derivative works of the Software, unless such copies or derivative
  26. // works are solely in the form of machine-executable object code generated by
  27. // a source language processor.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  32. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  33. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  34. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  35. // DEALINGS IN THE SOFTWARE.
  36. //
  37. #ifndef Foundation_DeflatingStream_INCLUDED
  38. #define Foundation_DeflatingStream_INCLUDED
  39. #include "Poco/Foundation.h"
  40. #include "Poco/BufferedStreamBuf.h"
  41. #include <istream>
  42. #include <ostream>
  43. #include "Poco/zlib.h"
  44. namespace Poco {
  45. class Foundation_API DeflatingStreamBuf: public BufferedStreamBuf
  46. /// This is the streambuf class used by DeflatingInputStream and DeflatingOutputStream.
  47. /// The actual work is delegated to zlib 1.2.1 (see http://www.gzip.org).
  48. /// Both zlib (deflate) streams and gzip streams are supported.
  49. /// Output streams should always call close() to ensure
  50. /// proper completion of compression.
  51. /// A compression level (0 to 9) can be specified in the constructor.
  52. {
  53. public:
  54. enum StreamType
  55. {
  56. STREAM_ZLIB,
  57. STREAM_GZIP
  58. };
  59. DeflatingStreamBuf(std::istream& istr, StreamType type, int level);
  60. DeflatingStreamBuf(std::ostream& ostr, StreamType type, int level);
  61. ~DeflatingStreamBuf();
  62. int close();
  63. protected:
  64. int readFromDevice(char* buffer, std::streamsize length);
  65. int writeToDevice(const char* buffer, std::streamsize length);
  66. private:
  67. enum
  68. {
  69. STREAM_BUFFER_SIZE = 1024,
  70. DEFLATE_BUFFER_SIZE = 32768
  71. };
  72. std::istream* _pIstr;
  73. std::ostream* _pOstr;
  74. char* _buffer;
  75. z_stream _zstr;
  76. bool _eof;
  77. };
  78. class Foundation_API DeflatingIOS: public virtual std::ios
  79. /// The base class for DeflatingOutputStream and DeflatingInputStream.
  80. ///
  81. /// This class is needed to ensure the correct initialization
  82. /// order of the stream buffer and base classes.
  83. {
  84. public:
  85. DeflatingIOS(std::ostream& ostr, DeflatingStreamBuf::StreamType type = DeflatingStreamBuf::STREAM_ZLIB, int level = Z_DEFAULT_COMPRESSION);
  86. DeflatingIOS(std::istream& istr, DeflatingStreamBuf::StreamType type = DeflatingStreamBuf::STREAM_ZLIB, int level = Z_DEFAULT_COMPRESSION);
  87. ~DeflatingIOS();
  88. DeflatingStreamBuf* rdbuf();
  89. protected:
  90. DeflatingStreamBuf _buf;
  91. };
  92. class Foundation_API DeflatingOutputStream: public DeflatingIOS, public std::ostream
  93. /// This stream compresses all data passing through it
  94. /// using zlib's deflate algorithm.
  95. /// After all data has been written to the stream, close()
  96. /// must be called to ensure completion of compression.
  97. /// Example:
  98. /// std::ofstream ostr("data.gz", std::ios::binary);
  99. /// DeflatingOutputStream deflater(ostr, DeflatingStreamBuf::STREAM_GZIP);
  100. /// deflater << "Hello, world!" << std::endl;
  101. /// deflater.close();
  102. /// ostr.close();
  103. {
  104. public:
  105. DeflatingOutputStream(std::ostream& ostr, DeflatingStreamBuf::StreamType type = DeflatingStreamBuf::STREAM_ZLIB, int level = Z_DEFAULT_COMPRESSION);
  106. ~DeflatingOutputStream();
  107. int close();
  108. };
  109. class Foundation_API DeflatingInputStream: public DeflatingIOS, public std::istream
  110. /// This stream compresses all data passing through it
  111. /// using zlib's deflate algorithm.
  112. {
  113. public:
  114. DeflatingInputStream(std::istream& istr, DeflatingStreamBuf::StreamType type = DeflatingStreamBuf::STREAM_ZLIB, int level = Z_DEFAULT_COMPRESSION);
  115. ~DeflatingInputStream();
  116. };
  117. } // namespace Poco
  118. #endif // Foundation_DeflatingStream_INCLUDED