|
Boost Users : |
From: Jonathan Turkanis (turkanis_at_[hidden])
Date: 2008-03-05 12:14:39
Robert Dailey wrote:
> On second thought, even though I've found the operators they don't do
> what I expected them to do in my tests. So, I have the following stream,
> which is a seekable device:
>
> boost::iostreams::stream<MyStreamDevice> IOStream;
>
> void foo()
> {
> char data;
> IOStream >> data;
> }
>
> I expected here that MyStreamDevice::read() would be called with a
> request for 1 byte,
Boost.Iostreams provides buffering automatically, so I would expect that
the request to read would ask for a full buffer of data. The buffer size
can be set at construction (http://tinyurl.com/2ptvu2), but is never
zero because of the need for filters to be able to put back characters.
Try specifying a buffer size of 0 and a putback buffer size of 1 if you
want a small buffer.
What behavior are you observing?
> since the sizeof( data ) is 1 byte. I want my stream
> to act as binary, so the bitshift operators should attempt to "fill" the
> right operand with as much as it can. For longs, this would be 4 bytes
> for example.
Standard iostreams do not support binary i/o directly. When you attempt
to read a long from a stream, the stream attempts to interpret the first
few characters in the stream as a formatted number, depending on the
locale. For example:
#include <assert>
#include <stringstream>
int main()
{
std::istringstream in("10000");
long x;
in >> x;
assert(x == 10000);
}
If you want to do binary i/o, you should not use the overloaded bitshift
operators at all. Use the unformatted operations (such as istream::get,
istream::read, streambuf::sgetc, streambuf::sgetn) to read one or more
characters, then examine the characters carefully, taking into account
sign and byte order.
You can see some examples here:
http://www.boost.org/boost/iostreams/filter/gzip.hpp. Look for the
functions read_uint8 and read_unit32, and the functions that call them.
> Do I have to overload my own generic bitshift operators for this
> behavior or does Boost.Iostreams supply some overloaded bitshift
> operators that provide such functionality?
-- Jonathan Turkanis CodeRage http://www.coderage.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