[iostreams] binary memory stream

I'm still pretty inexperienced with the iostreams library, so this may be a simple question. I'm trying to use GIL to write a PNG to a memory stream instead of a file stream. The write_view() function in the new version of GIL's io extension expects an ostream opened in binary mode as it's first parameter, such as: using namespace std; using namespace boost::gil; ofstream outputStream( "foo.png", ios::out | ios::binary ); write_view( outputStream, const_view(image), png_tag() ); I'm attempting to write to memory instead of a file, by doing this: using namespace boost::gil; using namespace boost::iostreams; typedef stream< back_insert_device< std::vector<char> > > BufferStream; std::vector<char> buffer; BufferStream bufferStream( buffer ); write_view( bufferStream, const_view(image), png_tag() ); The data that ends up in buffer is identical to the data that ends up in foo.png from the first example if I don't open outputStream in binary mode. So how can I make a binary memory stream with the iostreams library? Do I need to write my own filter?

Kenny Riddile wrote:
I'm still pretty inexperienced with the iostreams library, so this may be a simple question. I'm trying to use GIL to write a PNG to a memory stream instead of a file stream. The write_view() function in the new version of GIL's io extension expects an ostream opened in binary mode as it's first parameter, such as:
using namespace std; using namespace boost::gil;
ofstream outputStream( "foo.png", ios::out | ios::binary ); write_view( outputStream, const_view(image), png_tag() );
I'm attempting to write to memory instead of a file, by doing this:
using namespace boost::gil; using namespace boost::iostreams;
typedef stream< back_insert_device< std::vector<char> > > BufferStream;
std::vector<char> buffer; BufferStream bufferStream( buffer );
write_view( bufferStream, const_view(image), png_tag() );
The data that ends up in buffer is identical to the data that ends up in foo.png from the first example if I don't open outputStream in binary mode. So how can I make a binary memory stream with the iostreams library? Do I need to write my own filter?
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Ok, so redefining bufferStream like so works: std::stringstream bufferStream( std::ios_base::in | std::ios_base::out | std::ios_base::binary ); but it just feels "weird". Is there any cleaner way to do this with the iostreams library?

On Sun, Mar 29, 2009 at 9:10 PM, Kenny Riddile <kfriddile@yahoo.com> wrote:
Kenny Riddile wrote:
I'm still pretty inexperienced with the iostreams library, so this may be a simple question. I'm trying to use GIL to write a PNG to a memory stream instead of a file stream. The write_view() function in the new version of GIL's io extension expects an ostream opened in binary mode as it's first parameter, such as:
using namespace std; using namespace boost::gil;
ofstream outputStream( "foo.png", ios::out | ios::binary ); write_view( outputStream, const_view(image), png_tag() );
I'm attempting to write to memory instead of a file, by doing this:
using namespace boost::gil; using namespace boost::iostreams;
typedef stream< back_insert_device< std::vector<char> > > BufferStream;
std::vector<char> buffer; BufferStream bufferStream( buffer );
write_view( bufferStream, const_view(image), png_tag() );
The data that ends up in buffer is identical to the data that ends up in foo.png from the first example if I don't open outputStream in binary mode. So how can I make a binary memory stream with the iostreams library? Do I need to write my own filter?
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Ok, so redefining bufferStream like so works:
std::stringstream bufferStream( std::ios_base::in | std::ios_base::out | std::ios_base::binary );
but it just feels "weird". Is there any cleaner way to do this with the iostreams library?
This doesn't look weird to me. This is how I would do it. You should probably post this to the boost-users list. Chris

I think what Kenny is referring to is that fact that you have to use a stringstream buffer for writing binary data. People think of strings normally as text. But then again you can always use typedef. ;-) Christian

Chris Weed wrote:
On Sun, Mar 29, 2009 at 9:10 PM, Kenny Riddile <kfriddile@yahoo.com> wrote:
Kenny Riddile wrote:
I'm still pretty inexperienced with the iostreams library, so this may be a simple question. I'm trying to use GIL to write a PNG to a memory stream instead of a file stream. The write_view() function in the new version of GIL's io extension expects an ostream opened in binary mode as it's first parameter, such as:
using namespace std; using namespace boost::gil;
ofstream outputStream( "foo.png", ios::out | ios::binary ); write_view( outputStream, const_view(image), png_tag() );
I'm attempting to write to memory instead of a file, by doing this:
using namespace boost::gil; using namespace boost::iostreams;
typedef stream< back_insert_device< std::vector<char> > > BufferStream;
std::vector<char> buffer; BufferStream bufferStream( buffer );
write_view( bufferStream, const_view(image), png_tag() );
The data that ends up in buffer is identical to the data that ends up in foo.png from the first example if I don't open outputStream in binary mode. So how can I make a binary memory stream with the iostreams library? Do I need to write my own filter?
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Ok, so redefining bufferStream like so works:
std::stringstream bufferStream( std::ios_base::in | std::ios_base::out | std::ios_base::binary );
but it just feels "weird". Is there any cleaner way to do this with the iostreams library?
This doesn't look weird to me. This is how I would do it.
Obviously I can use a typedef to hide the fact that it's a stringstream, but the weird part for me is accessing the data via: bufferStream.string().c_str() Of course, that can be abstracted too. I was just wondering if the iostreams library already offered some simple, elegant way of creating a binary memory stream before I reinvent the wheel.
You should probably post this to the boost-users list.
Honestly, I usually get better and more prompt responses when posting here than on the users list...I just go where the answers are.

At 7:41 PM -0500 3/29/09, Kenny Riddile wrote:
I'm attempting to write to memory instead of a file, by doing this:
using namespace boost::gil; using namespace boost::iostreams;
typedef stream< back_insert_device< std::vector<char> > > BufferStream;
std::vector<char> vbuffer; boost::iostreams::filtering_ostream outs( boost::iostreams::back_inserter(vbuffer)) is what I've been using to create an in-memory binary output stream. That is, a filtering stream that has only a sink and no intervening filters in the chain.

Kim Barrett wrote:
At 7:41 PM -0500 3/29/09, Kenny Riddile wrote:
I'm attempting to write to memory instead of a file, by doing this:
using namespace boost::gil; using namespace boost::iostreams;
typedef stream< back_insert_device< std::vector<char> > > BufferStream;
std::vector<char> vbuffer; boost::iostreams::filtering_ostream outs( boost::iostreams::back_inserter(vbuffer))
is what I've been using to create an in-memory binary output stream. That is, a filtering stream that has only a sink and no intervening filters in the chain. _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
That still appears to be converting the data to characters, similar to opening a std::ofstream without explicitly putting it in binary mode.
participants (4)
-
Chris Weed
-
Christian Henning
-
Kenny Riddile
-
Kim Barrett