Skip to content

Commit

Permalink
Test bzfile_ and lz4s_ before reading/writing/closing (#1183)
Browse files Browse the repository at this point in the history
* Test bzfile_ before reading/writing/closing

* Test lz4stream before reading/writing
  • Loading branch information
jwon02 authored and dirk-thomas committed Oct 26, 2017
1 parent 4ff5bd8 commit 7aa64f9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tools/rosbag_storage/src/bz2_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_) {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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_) {
Expand Down
25 changes: 25 additions & 0 deletions tools/rosbag_storage/src/lz4_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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_);
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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_);
}

Expand Down

0 comments on commit 7aa64f9

Please sign in to comment.