void BZ2Stream::read(void* ptr, size_t size) { BZ2_bzRead(&bzerror_, bzfile_, ptr, size); advanceOffset(size); switch (bzerror_) { case BZ_OK: return; case BZ_STREAM_END: if (getUnused() || getUnusedLength() > 0) ROS_ERROR("unused data already available"); else { char* unused; int nUnused; BZ2_bzReadGetUnused(&bzerror_, bzfile_, (void**) &unused, &nUnused); setUnused(unused); setUnusedLength(nUnused); } return; case BZ_IO_ERROR: throw BagIOException("BZ_IO_ERROR: error reading from compressed stream"); break; case BZ_UNEXPECTED_EOF: throw BagIOException("BZ_UNEXPECTED_EOF: compressed stream ended before logical end-of-stream detected"); break; case BZ_DATA_ERROR: throw BagIOException("BZ_DATA_ERROR: data integrity error detected in compressed stream"); break; case BZ_DATA_ERROR_MAGIC: throw BagIOException("BZ_DATA_ERROR_MAGIC: stream does not begin with requisite header bytes"); break; case BZ_MEM_ERROR: throw BagIOException("BZ_MEM_ERROR: insufficient memory available"); break; } }
void UncompressedStream::write(void* ptr, size_t size) { size_t result = fwrite(ptr, 1, size, getFilePointer()); if (result != size) throw BagIOException((format("Error writing to file: writing %1% bytes, wrote %2% bytes") % size % result).str()); advanceOffset(size); }
void BZ2Stream::stopRead() { BZ2_bzReadClose(&bzerror_, bzfile_); switch (bzerror_) { case BZ_IO_ERROR: throw BagIOException("BZ_IO_ERROR"); } }
void UncompressedStream::read(void* ptr, size_t size) { size_t nUnused = (size_t) getUnusedLength(); char* unused = getUnused(); if (nUnused > 0) { // We have unused data from the last compressed read if (nUnused == size) { // Copy the unused data into the buffer memcpy(ptr, unused, nUnused); clearUnused(); } else if (nUnused < size) { // Copy the unused data into the buffer memcpy(ptr, unused, nUnused); // Still have data to read size -= nUnused; // Read the remaining data from the file int result = fread((char*) ptr + nUnused, 1, size, getFilePointer()); if ((size_t) result != size) throw BagIOException((format("Error reading from file + unused: wanted %1% bytes, read %2% bytes") % size % result).str()); advanceOffset(size); clearUnused(); } else { // nUnused_ > size memcpy(ptr, unused, size); setUnused(unused + size); setUnusedLength(nUnused - size); } } // No unused data - read from stream int result = fread(ptr, 1, size, getFilePointer()); if ((size_t) result != size) throw BagIOException((format("Error reading from file: wanted %1% bytes, read %2% bytes") % size % result).str()); advanceOffset(size); }
void BZ2Stream::stopWrite() { unsigned int nbytes_in; unsigned int nbytes_out; BZ2_bzWriteClose(&bzerror_, bzfile_, 0, &nbytes_in, &nbytes_out); switch (bzerror_) { case BZ_IO_ERROR: throw BagIOException("BZ_IO_ERROR"); } advanceOffset(nbytes_out); setCompressedIn(0); }