izlibstream.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 IZLIBSTREAM_H_INCLUDED
  21. #define IZLIBSTREAM_H_INCLUDED
  22. #include "io/zlib_streambuf.h"
  23. #include <istream>
  24. #include <zlib.h>
  25. namespace zlib
  26. {
  27. /**
  28. * @brief Stream buffer used by zlib::izlibstream
  29. * @sa izlibstream
  30. */
  31. class NBT_EXPORT inflate_streambuf : public zlib_streambuf
  32. {
  33. public:
  34. /**
  35. * @param input the istream to wrap
  36. * @param bufsize the size of the internal buffers
  37. * @param window_bits the base two logarithm of the maximum window size that
  38. * zlib will use.
  39. * This parameter also determines which type of input to expect.
  40. * The default argument will autodetect between zlib and gzip data.
  41. * Refer to the zlib documentation of inflateInit2 for more details.
  42. *
  43. * @throw zlib_error if zlib encounters a problem during initialization
  44. */
  45. explicit inflate_streambuf(std::istream& input, size_t bufsize = 32768, int window_bits = 32 + 15);
  46. ~inflate_streambuf() noexcept;
  47. ///@return the wrapped istream
  48. std::istream& get_istr() const { return is; }
  49. private:
  50. std::istream& is;
  51. bool stream_end;
  52. int_type underflow() override;
  53. };
  54. /**
  55. * @brief An istream adapter that decompresses data using zlib
  56. *
  57. * This istream wraps another istream. The izlibstream will read compressed
  58. * data from the wrapped istream and inflate (decompress) it with zlib.
  59. *
  60. * @note If you want to read more data from the wrapped istream after the end
  61. * of the compressed data, then it must allow seeking. It is unavoidable for
  62. * the izlibstream to consume more data after the compressed data.
  63. * It will automatically attempt to seek the wrapped istream back to the point
  64. * after the end of the compressed data.
  65. * @sa inflate_streambuf
  66. */
  67. class NBT_EXPORT izlibstream : public std::istream
  68. {
  69. public:
  70. /**
  71. * @param input the istream to wrap
  72. * @param bufsize the size of the internal buffers
  73. */
  74. explicit izlibstream(std::istream& input, size_t bufsize = 32768):
  75. std::istream(&buf), buf(input, bufsize)
  76. {}
  77. ///@return the wrapped istream
  78. std::istream& get_istr() const { return buf.get_istr(); }
  79. private:
  80. inflate_streambuf buf;
  81. };
  82. }
  83. #endif // IZLIBSTREAM_H_INCLUDED