diff --git a/tools/rosbag_storage/src/bz2_stream.cpp b/tools/rosbag_storage/src/bz2_stream.cpp index 900cd046c2..78e175381e 100644 --- a/tools/rosbag_storage/src/bz2_stream.cpp +++ b/tools/rosbag_storage/src/bz2_stream.cpp @@ -70,6 +70,10 @@ void BZ2Stream::startWrite() { } void BZ2Stream::write(void* ptr, size_t size) { + if (!bzfile_) { + throw BagException("cannot write to unopened bzfile"); + } + BZ2_bzWrite(&bzerror_, bzfile_, ptr, size); switch (bzerror_) { @@ -80,6 +84,10 @@ void BZ2Stream::write(void* ptr, size_t size) { } void BZ2Stream::stopWrite() { + if (!bzfile_) { + throw BagException("cannot close unopened bzfile"); + } + unsigned int nbytes_in; unsigned int nbytes_out; BZ2_bzWriteClose(&bzerror_, bzfile_, 0, &nbytes_in, &nbytes_out); @@ -107,6 +115,10 @@ void BZ2Stream::startRead() { } void BZ2Stream::read(void* ptr, size_t size) { + if (!bzfile_) { + throw BagException("cannot read from unopened bzfile"); + } + BZ2_bzRead(&bzerror_, bzfile_, ptr, size); advanceOffset(size); @@ -133,6 +145,10 @@ void BZ2Stream::read(void* ptr, size_t size) { } void BZ2Stream::stopRead() { + if (!bzfile_) { + throw BagException("cannot close unopened bzfile"); + } + BZ2_bzReadClose(&bzerror_, bzfile_); switch (bzerror_) { diff --git a/tools/rosbag_storage/src/lz4_stream.cpp b/tools/rosbag_storage/src/lz4_stream.cpp index 950266494e..297bcf7b33 100644 --- a/tools/rosbag_storage/src/lz4_stream.cpp +++ b/tools/rosbag_storage/src/lz4_stream.cpp @@ -46,6 +46,7 @@ LZ4Stream::LZ4Stream(ChunkedFile* file) : Stream(file), block_size_id_(6) { buff_size_ = roslz4_blockSizeFromIndex(block_size_id_) + 64; buff_ = new char[buff_size_]; + lz4s_.state = NULL; } LZ4Stream::~LZ4Stream() { @@ -57,6 +58,10 @@ CompressionType LZ4Stream::getCompressionType() const { } void LZ4Stream::startWrite() { + if (lz4s_.state) { + throw BagException("cannot start writing to already opened lz4 stream"); + } + setCompressedIn(0); int ret = roslz4_compressStart(&lz4s_, block_size_id_); @@ -71,6 +76,10 @@ void LZ4Stream::startWrite() { } void LZ4Stream::write(void* ptr, size_t size) { + if (!lz4s_.state) { + throw BagException("cannot write to unopened lz4 stream"); + } + lz4s_.input_left = size; lz4s_.input_next = (char*) ptr; @@ -112,12 +121,20 @@ void LZ4Stream::writeStream(int action) { } void LZ4Stream::stopWrite() { + if (!lz4s_.state) { + throw BagException("cannot close unopened lz4 stream"); + } + writeStream(ROSLZ4_FINISH); setCompressedIn(0); roslz4_compressEnd(&lz4s_); } void LZ4Stream::startRead() { + if (lz4s_.state) { + throw BagException("cannot start reading from already opened lz4 stream"); + } + int ret = roslz4_decompressStart(&lz4s_); switch(ret) { case ROSLZ4_OK: break; @@ -137,6 +154,10 @@ void LZ4Stream::startRead() { } void LZ4Stream::read(void* ptr, size_t size) { + if (!lz4s_.state) { + throw BagException("cannot read from unopened lz4 stream"); + } + // Setup stream by filling buffer with data from file int to_read = buff_size_ - lz4s_.input_left; char *input_start = buff_ + lz4s_.input_left; @@ -181,6 +202,10 @@ void LZ4Stream::read(void* ptr, size_t size) { } void LZ4Stream::stopRead() { + if (!lz4s_.state) { + throw BagException("cannot close unopened lz4 stream"); + } + roslz4_decompressEnd(&lz4s_); }