DeflatingStream.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // DeflatingStream.h
  3. //
  4. // Library: Foundation
  5. // Package: Streams
  6. // Module: ZLibStream
  7. //
  8. // Definition of the DeflatingStream class.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_DeflatingStream_INCLUDED
  16. #define Foundation_DeflatingStream_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/BufferedStreamBuf.h"
  19. #include <istream>
  20. #include <ostream>
  21. #if defined(POCO_UNBUNDLED)
  22. #include <zlib.h>
  23. #else
  24. #include "Poco/zlib.h"
  25. #endif
  26. namespace Poco {
  27. class Foundation_API DeflatingStreamBuf: public BufferedStreamBuf
  28. /// This is the streambuf class used by DeflatingInputStream and DeflatingOutputStream.
  29. /// The actual work is delegated to zlib (see http://zlib.net).
  30. /// Both zlib (deflate) streams and gzip streams are supported.
  31. /// Output streams should always call close() to ensure
  32. /// proper completion of compression.
  33. /// A compression level (0 to 9) can be specified in the constructor.
  34. {
  35. public:
  36. enum StreamType
  37. {
  38. STREAM_ZLIB, /// Create a zlib header, use Adler-32 checksum.
  39. STREAM_GZIP /// Create a gzip header, use CRC-32 checksum.
  40. };
  41. DeflatingStreamBuf(std::istream& istr, StreamType type, int level);
  42. /// Creates a DeflatingStreamBuf for compressing data read
  43. /// from the given input stream.
  44. DeflatingStreamBuf(std::istream& istr, int windowBits, int level);
  45. /// Creates a DeflatingStreamBuf for compressing data read
  46. /// from the given input stream.
  47. ///
  48. /// Please refer to the zlib documentation of deflateInit2() for a description
  49. /// of the windowBits parameter.
  50. DeflatingStreamBuf(std::ostream& ostr, StreamType type, int level);
  51. /// Creates a DeflatingStreamBuf for compressing data passed
  52. /// through and forwarding it to the given output stream.
  53. DeflatingStreamBuf(std::ostream& ostr, int windowBits, int level);
  54. /// Creates a DeflatingStreamBuf for compressing data passed
  55. /// through and forwarding it to the given output stream.
  56. ///
  57. /// Please refer to the zlib documentation of deflateInit2() for a description
  58. /// of the windowBits parameter.
  59. ~DeflatingStreamBuf();
  60. /// Destroys the DeflatingStreamBuf.
  61. int close();
  62. /// Finishes up the stream.
  63. ///
  64. /// Must be called when deflating to an output stream.
  65. protected:
  66. int readFromDevice(char* buffer, std::streamsize length);
  67. int writeToDevice(const char* buffer, std::streamsize length);
  68. virtual int sync();
  69. private:
  70. enum
  71. {
  72. STREAM_BUFFER_SIZE = 1024,
  73. DEFLATE_BUFFER_SIZE = 32768
  74. };
  75. std::istream* _pIstr;
  76. std::ostream* _pOstr;
  77. char* _buffer;
  78. z_stream _zstr;
  79. bool _eof;
  80. };
  81. class Foundation_API DeflatingIOS: public virtual std::ios
  82. /// The base class for DeflatingOutputStream and DeflatingInputStream.
  83. ///
  84. /// This class is needed to ensure the correct initialization
  85. /// order of the stream buffer and base classes.
  86. {
  87. public:
  88. DeflatingIOS(std::ostream& ostr, DeflatingStreamBuf::StreamType type = DeflatingStreamBuf::STREAM_ZLIB, int level = Z_DEFAULT_COMPRESSION);
  89. /// Creates a DeflatingIOS for compressing data passed
  90. /// through and forwarding it to the given output stream.
  91. DeflatingIOS(std::ostream& ostr, int windowBits, int level);
  92. /// Creates a DeflatingIOS for compressing data passed
  93. /// through and forwarding it to the given output stream.
  94. ///
  95. /// Please refer to the zlib documentation of deflateInit2() for a description
  96. /// of the windowBits parameter.
  97. DeflatingIOS(std::istream& istr, DeflatingStreamBuf::StreamType type = DeflatingStreamBuf::STREAM_ZLIB, int level = Z_DEFAULT_COMPRESSION);
  98. /// Creates a DeflatingIOS for compressing data read
  99. /// from the given input stream.
  100. DeflatingIOS(std::istream& istr, int windowBits, int level);
  101. /// Creates a DeflatingIOS for compressing data read
  102. /// from the given input stream.
  103. ///
  104. /// Please refer to the zlib documentation of deflateInit2() for a description
  105. /// of the windowBits parameter.
  106. ~DeflatingIOS();
  107. /// Destroys the DeflatingIOS.
  108. DeflatingStreamBuf* rdbuf();
  109. /// Returns a pointer to the underlying stream buffer.
  110. protected:
  111. DeflatingStreamBuf _buf;
  112. };
  113. class Foundation_API DeflatingOutputStream: public std::ostream, public DeflatingIOS
  114. /// This stream compresses all data passing through it
  115. /// using zlib's deflate algorithm.
  116. /// After all data has been written to the stream, close()
  117. /// must be called to ensure completion of compression.
  118. /// Example:
  119. /// std::ofstream ostr("data.gz", std::ios::binary);
  120. /// DeflatingOutputStream deflater(ostr, DeflatingStreamBuf::STREAM_GZIP);
  121. /// deflater << "Hello, world!" << std::endl;
  122. /// deflater.close();
  123. /// ostr.close();
  124. {
  125. public:
  126. DeflatingOutputStream(std::ostream& ostr, DeflatingStreamBuf::StreamType type = DeflatingStreamBuf::STREAM_ZLIB, int level = Z_DEFAULT_COMPRESSION);
  127. /// Creates a DeflatingOutputStream for compressing data passed
  128. /// through and forwarding it to the given output stream.
  129. DeflatingOutputStream(std::ostream& ostr, int windowBits, int level);
  130. /// Creates a DeflatingOutputStream for compressing data passed
  131. /// through and forwarding it to the given output stream.
  132. ///
  133. /// Please refer to the zlib documentation of deflateInit2() for a description
  134. /// of the windowBits parameter.
  135. ~DeflatingOutputStream();
  136. /// Destroys the DeflatingOutputStream.
  137. int close();
  138. /// Finishes up the stream.
  139. ///
  140. /// Must be called when deflating to an output stream.
  141. protected:
  142. virtual int sync();
  143. };
  144. class Foundation_API DeflatingInputStream: public std::istream, public DeflatingIOS
  145. /// This stream compresses all data passing through it
  146. /// using zlib's deflate algorithm.
  147. {
  148. public:
  149. DeflatingInputStream(std::istream& istr, DeflatingStreamBuf::StreamType type = DeflatingStreamBuf::STREAM_ZLIB, int level = Z_DEFAULT_COMPRESSION);
  150. /// Creates a DeflatingIOS for compressing data read
  151. /// from the given input stream.
  152. DeflatingInputStream(std::istream& istr, int windowBits, int level);
  153. /// Creates a DeflatingIOS for compressing data read
  154. /// from the given input stream.
  155. ///
  156. /// Please refer to the zlib documentation of deflateInit2() for a description
  157. /// of the windowBits parameter.
  158. ~DeflatingInputStream();
  159. /// Destroys the DeflatingInputStream.
  160. };
  161. } // namespace Poco
  162. #endif // Foundation_DeflatingStream_INCLUDED