Boost logo

Boost Users :

From: Jonathan Turkanis (technews_at_[hidden])
Date: 2005-12-11 20:02:14


Rodrigo Baroni wrote:
> Hi all,
>
> How can I know the size of a stream< io::array_sink >
> memoryStream( buffer, size ) , after some operations that has filled
> the stream (the memory) ? Is there some way ?

I don't think so, although if you use a stream< io::array > instead of stream<
io::array_sink > you should be able to query the number of characters written by
calling ostream::tellp().

> Example:
>
> //--------------- Compress the content of buffer to
> compressedBuffer----------------------
>
> char buffer = "Helloooo compressed filter example!";
> int SIZE = 10000;
>
> char compressedBuffer = new char [ SIZE ];
>
> io::stream< io::array_source > bufferStream( buffer, std::strlen(
> buffer ) ); io::stream< io::array_sink > compressedBufferStream(
> &compressedBuffer, SIZE );
>
> io::filtering_ostream compressFilterOutput;
> compressFilterOutput.push( io::zlib_compressor( ) );
> compressFilterOutput.push( compressedBufferStream );
>
> io::copy( bufferStream, compressedFilterOutput );
> compressedBufferStream.flush( );
> compressFilterOutput.reset( );
>
> //-----------------------------
>
> How can I know the size of the compressedBufferStream, or
> compressedBuffer now?

  Here's a simpler way to accomplish the above:

  #include <iterator>
  #include <string>

  std::string compressed;
  io::filtering_ostream
    out( io::zlib_compressor() |
         std::back_inserter(compressed) );
  out.write(buffer, std::strlen(buffer));
  out.reset();

Now you can query the length with compressed.size().

> std::strlen( compressedBuffer ) gives me wrong
> value.

This is to be expected, since compressed data may contain embedded nulls
characters, and the output of zlib_compressor isn't null-terminated.

> Thanks in advance,
> Rodrigo Ferreira Baroni

-- 
Jonathan Turkanis
www.kangaroologic.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net