2
votes

I am using boost::iostreams filtering_stream to implement my custom archive format. This format should support various compression algorithm so that each file could be compressed by using a different method.

To that end I keep a global stream for reading (or writing) and add or remove filters as required. However it is unclear whether adding and removing filters on the fly is actually possible.

In essence, I am trying to do something like this:

void ArchiveFile::readFromStream( std::istream& inputStream, unsigned filters )
{
    // create the filtered stream
    boost::iostreams::filtering_istream in;

    if ( filters & FILTERS_BZIP )
    {
        in.push( boost::iostreams::bzip2_decompressor() );
    }

    // add the source stream
    in.push( inputStream );

    // read file content
    in.read( &mFileContent[0], mFileSize );
}

I am calling readFromStream for each file, using the same inputStream. However I keep getting gibberish this way even when not using any filter. When I directly use inputStream the file is read ok.

What am I doing wrong?

1

1 Answers

0
votes

I know it's been a while, but I just stumbled upon that question. So the answer turned out to be putting the whole code section within a new scope:

void ArchiveFile::readFromStream( std::istream& inputStream, unsigned filter
{
    // new scope
    {
        // create the filtered stream
        boost::iostreams::filtering_istream in;

        if ( filters & FILTERS_BZIP )
        {
          in.push( boost::iostreams::bzip2_decompressor() );
        }

        // add the source stream
        in.push( inputStream );

        // read file content
        in.read( &mFileContent[0], mFileSize );
    }
}

This was probably necessary due to some bug in the compiler I was using at the time (some version of MS Visual Studio).