Tutorial

2.1.1. Overview: Devices, stream_buffer and stream

Writing a new stream or stream buffer class using the Boost Iostreams library is easy: you simply write a class modeling the Device concept, then use that class as the template argument to stream or stream_buffer:

Here io::stream_buffer<my_device> is a derived class of std::basic_streambuf, and io::stream<my_device> is a derived class of std::basic_istream, std::basic_ostream or std::basic_iostream depending on the mode of my_device, i.e., depending on which of the fundamental i/o operations read, write and seek it supports.

The template io::stream is provided as a convenience. It's always possibly to avoid io::stream and simply use io::stream_buffer together with one of the standard library stream templates. E.g.,

In this example, the ostreamout uses the stream_bufferbuf as its underlying data sink, so that data written to out goes to the file log.txt. The same effect could be achieved by default constructing out and telling it to use stream buffer buf by invoking out.rdbuf(&buf).