How get the size of a stream< io::array_sink > memoryStream?
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 ? 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? std::strlen( compressedBuffer ) gives me wrong value. Thanks in advance, Rodrigo Ferreira Baroni
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
I just have one doubt now: The output to a std::string (that has internal variable size, and so, maybe use some linked queue, or whatever I guess), or to output to a pre-alocated buffer? Shouldn't be, in some cases where this task is done a lot of times in a loop (like is the case - where I'm doing compression of frames in a streaming video server) be more faster and better to output to memory directly in a pre-alocated buffer? => Case 1: output to memory-pre-alocated-stream-output:
//--------------- 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( );
//-----------------------------
=> Case 2: output to std::string dynamic-allocated-stream-output:
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().
Thanks in advance, Rodrigo F. Baroni
Rodrigo Baroni wrote:
I just have one doubt now: The output to a std::string (that has internal variable size, and so, maybe use some linked queue, or whatever I guess), or to output to a pre-alocated buffer?
Shouldn't be, in some cases where this task is done a lot of times in a loop (like is the case - where I'm doing compression of frames in a streaming video server) be more faster and better to output to memory directly in a pre-alocated buffer?
Possibly; you'd have to measure it. If you use a vector and call reserve(), I
doubt you'll notice any difference.
It's also easy to write an array_sink that counts the number of characters that
have been written to it. There's an example implementation at
Thanks once time again Jonathan!
I'm busy with some university works by now, but very soon (I hope in
this week) I'll try that, and I'll report you back the measures.
By the way, It's a honor a answer from the developer of many classes
of boost :)
Cheers,
Rodrigo F Baroni
On 12/12/05, Jonathan Turkanis
Rodrigo Baroni wrote:
I just have one doubt now: The output to a std::string (that has internal variable size, and so, maybe use some linked queue, or whatever I guess), or to output to a pre-alocated buffer?
Shouldn't be, in some cases where this task is done a lot of times in a loop (like is the case - where I'm doing compression of frames in a streaming video server) be more faster and better to output to memory directly in a pre-alocated buffer?
Possibly; you'd have to measure it. If you use a vector and call reserve(), I doubt you'll notice any difference.
It's also easy to write an array_sink that counts the number of characters that have been written to it. There's an example implementation at
. -- Jonathan Turkanis www.kangaroologic.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Jonathan Turkanis
-
Rodrigo Baroni