ozlibstream.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * libnbt++ - A library for the Minecraft Named Binary Tag format.
  3. * Copyright (C) 2013, 2015 ljfa-ag
  4. *
  5. * This file is part of libnbt++.
  6. *
  7. * libnbt++ is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * libnbt++ is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with libnbt++. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef OZLIBSTREAM_H_INCLUDED
  21. #define OZLIBSTREAM_H_INCLUDED
  22. #include "io/zlib_streambuf.h"
  23. #include <ostream>
  24. #include <zlib.h>
  25. namespace zlib
  26. {
  27. /**
  28. * @brief Stream buffer used by zlib::ozlibstream
  29. * @sa ozlibstream
  30. */
  31. class NBT_EXPORT deflate_streambuf : public zlib_streambuf
  32. {
  33. public:
  34. /**
  35. * @param output the ostream to wrap
  36. * @param bufsize the size of the internal buffers
  37. * @param level the compression level, ranges from 0 to 9, or -1 for default
  38. *
  39. * Refer to the zlib documentation of deflateInit2 for details about the arguments.
  40. *
  41. * @throw zlib_error if zlib encounters a problem during initialization
  42. */
  43. explicit deflate_streambuf(std::ostream& output, size_t bufsize = 32768, int level = Z_DEFAULT_COMPRESSION, int window_bits = 15, int mem_level = 8, int strategy = Z_DEFAULT_STRATEGY);
  44. ~deflate_streambuf() noexcept;
  45. ///@return the wrapped ostream
  46. std::ostream& get_ostr() const { return os; }
  47. ///Finishes compression and writes all pending data to the output
  48. void close();
  49. private:
  50. std::ostream& os;
  51. void deflate_chunk(int flush = Z_NO_FLUSH);
  52. int_type overflow(int_type ch) override;
  53. int sync() override;
  54. };
  55. /**
  56. * @brief An ostream adapter that compresses data using zlib
  57. *
  58. * This ostream wraps another ostream. Data written to an ozlibstream will be
  59. * deflated (compressed) with zlib and written to the wrapped ostream.
  60. *
  61. * @sa deflate_streambuf
  62. */
  63. class NBT_EXPORT ozlibstream : public std::ostream
  64. {
  65. public:
  66. /**
  67. * @param output the ostream to wrap
  68. * @param level the compression level, ranges from 0 to 9, or -1 for default
  69. * @param gzip if true, the output will be in gzip format rather than zlib
  70. * @param bufsize the size of the internal buffers
  71. */
  72. explicit ozlibstream(std::ostream& output, int level = Z_DEFAULT_COMPRESSION, bool gzip = false, size_t bufsize = 32768):
  73. std::ostream(&buf), buf(output, bufsize, level, 15 + (gzip ? 16 : 0))
  74. {}
  75. ///@return the wrapped ostream
  76. std::ostream& get_ostr() const { return buf.get_ostr(); }
  77. ///Finishes compression and writes all pending data to the output
  78. void close();
  79. private:
  80. deflate_streambuf buf;
  81. };
  82. }
  83. #endif // OZLIBSTREAM_H_INCLUDED