diff options
| author | ljfa <ljfa-ag@web.de> | 2015-09-21 08:30:25 +0200 |
|---|---|---|
| committer | ljfa <ljfa-ag@web.de> | 2015-09-21 08:33:16 +0200 |
| commit | 64e6d2abc719869e386caa760bfbe1e65bd7559b (patch) | |
| tree | f666d9f05a11058e31703879f07e297ef40b1f86 | |
| parent | 07a9300246630705d5f4e6978cdc5cf8a8b4afd7 (diff) | |
| download | Project-Tick-64e6d2abc719869e386caa760bfbe1e65bd7559b.tar.gz Project-Tick-64e6d2abc719869e386caa760bfbe1e65bd7559b.zip | |
Create ozlibstream::close method
Set failbit on output stream on failure
| -rw-r--r-- | include/io/ozlibstream.h | 4 | ||||
| -rw-r--r-- | src/io/ozlibstream.cpp | 10 | ||||
| -rw-r--r-- | test/zlibstream_test.h | 7 |
3 files changed, 19 insertions, 2 deletions
diff --git a/include/io/ozlibstream.h b/include/io/ozlibstream.h index d4ea8c2840..770db0029d 100644 --- a/include/io/ozlibstream.h +++ b/include/io/ozlibstream.h @@ -48,6 +48,8 @@ public: std::ostream& get_ostr() const { return os; } + ///Finishes compression and writes all pending data to the output + void close(); private: std::ostream& os; std::vector<char> in; @@ -84,6 +86,8 @@ public: ///@return the wrapped ostream std::ostream& get_ostr() const { return buf.get_ostr(); } + ///Finishes compression and writes all pending data to the output + void close() { buf.close(); } private: deflate_streambuf buf; }; diff --git a/src/io/ozlibstream.cpp b/src/io/ozlibstream.cpp index 97702a37e1..605d1397d0 100644 --- a/src/io/ozlibstream.cpp +++ b/src/io/ozlibstream.cpp @@ -40,7 +40,7 @@ deflate_streambuf::~deflate_streambuf() noexcept { try { - deflate_chunk(Z_FINISH); + close(); } catch(...) { @@ -49,6 +49,11 @@ deflate_streambuf::~deflate_streambuf() noexcept deflateEnd(&zstr); } +void deflate_streambuf::close() +{ + deflate_chunk(Z_FINISH); +} + void deflate_streambuf::deflate_chunk(int flush) { zstr.next_in = reinterpret_cast<Bytef*>(pbase()); @@ -59,7 +64,10 @@ void deflate_streambuf::deflate_chunk(int flush) zstr.avail_out = out.size(); int ret = deflate(&zstr, flush); if(ret != Z_OK && ret != Z_STREAM_END) + { + os.setstate(std::ios_base::failbit); throw zlib_error(zstr.msg, ret); + } int have = out.size() - zstr.avail_out; if(!os.write(out.data(), have)) throw std::ios_base::failure("Could not write to the output stream"); diff --git a/test/zlibstream_test.h b/test/zlibstream_test.h index 754f4e5d58..a8357e2692 100644 --- a/test/zlibstream_test.h +++ b/test/zlibstream_test.h @@ -143,8 +143,9 @@ public: { ozlibstream ozls(str, -1, false, 256); ozls.exceptions(std::ios::failbit | std::ios::badbit); - ozls << bigtest; + TS_ASSERT_THROWS_NOTHING(ozls << bigtest); TS_ASSERT(ozls.good()); + TS_ASSERT_THROWS_NOTHING(ozls.close()); } TS_ASSERT(str.good()); { @@ -166,6 +167,7 @@ public: std::string half2 = bigtest.substr(bigtest.size()/2); TS_ASSERT_THROWS_NOTHING(ozls << half1 << std::flush << half2); TS_ASSERT(ozls.good()); + TS_ASSERT_THROWS_NOTHING(ozls.close()); } TS_ASSERT(str.good()); { @@ -183,6 +185,8 @@ public: ozls.exceptions(std::ios::failbit | std::ios::badbit); TS_ASSERT_THROWS_NOTHING(ozls << bigtest); TS_ASSERT(ozls.good()); + TS_ASSERT_THROWS_NOTHING(ozls.close()); + TS_ASSERT_THROWS_NOTHING(ozls.close()); //closing twice shouldn't be a problem } TS_ASSERT(str.good()); { @@ -202,6 +206,7 @@ public: ozls.exceptions(std::ios::failbit | std::ios::badbit); TS_ASSERT_THROWS_NOTHING(ozls << bigtest); TS_ASSERT(ozls.good()); + TS_ASSERT_THROWS_NOTHING(ozls.close()); } TS_ASSERT(str.good()); { |
