[Iostreams] Confusion with devices

Hi,
From my research on Iostream devices, I cannot understand why my read() statement's 'count' parameter is receiving a value of 4096. For example, I've implemented a seekable_device_tag category device that provides direct binary disk access. My stream is defined as follows:
typedef boost::iostreams::stream<DiskDevice> DiskStream; DiskStream stream; char* buffer; // assume this is a valid buffer stream.read( buffer, 8 ); The code above (at least to me) should be reading in 8 char's (bytes) of data from the stream. However, if I set a break point in DiskDevice::read(), the second parameter (named count) receives a value of 4096, which eventually gets passed into fread() and causes an exception. I also noticed that boost::iostreams::stream derives from the standard streams, which I imagine is causing the problems. For direct binary disk access, should I avoid boost.iostreams all together? I don't like that boost::stream is going through std::iostream::read() before reaching my device's read function. Perhaps, also, I am misunderstanding Boost.Iostreams. Please clarify. Thanks.

On Mon, Mar 10, 2008 at 4:40 PM, Robert Dailey <rcdailey@gmail.com> wrote:
Perhaps, also, I am misunderstanding Boost.Iostreams. Please clarify. Thanks.
You're running into an issue with buffering. Even though you only need 8 bytes, the stream buffer has a 4096 byte buffer and will try to fill it up. Unfortunately, there isn't much documentation on how Boost.Iostreams handles buffering. There are basically three options: 1) create the stream buffer directly and supply a buffer size, then pass the buffer to a boost::iostreams::stream object 2) use 'optimally_buffered_tag' in your device's category and make a member function called optimal_buffer_size() that returns an std::streamsize of 0 3) perform validation in your read method to ensure you aren't reading too much - Jim

Robert Dailey wrote:
Hi,
From my research on Iostream devices, I cannot understand why my read() statement's 'count' parameter is receiving a value of 4096. For example, I've implemented a seekable_device_tag category device that provides direct binary disk access. My stream is defined as follows:
typedef boost::iostreams::stream<DiskDevice> DiskStream;
DiskStream stream;
char* buffer; // assume this is a valid buffer stream.read( buffer, 8 );
The code above (at least to me) should be reading in 8 char's (bytes) of data from the stream. However, if I set a break point in DiskDevice::read(), the second parameter (named count) receives a value of 4096, which eventually gets passed into fread() and causes an exception. I also noticed that boost::iostreams::stream derives from the standard streams, which I imagine is causing the problems. For direct binary disk access, should I avoid boost.iostreams all together? I don't like that boost::stream is going through std::iostream::read() before reaching my device's read function.
I thought Jonathan Turkanis answered your questions on boost.users see: http://lists.boost.org/boost-users/2008/03/34390.php

On Tue, Mar 11, 2008 at 12:34 AM, eg <egoots@gmail.com> wrote:
I thought Jonathan Turkanis answered your questions on boost.users see: http://lists.boost.org/boost-users/2008/03/34390.php
While the topic was brought up in that thread, it was by no means the original question. I read over it a second time to make sure before I posted this inquiry. The information provided here is lacking in the thread you've linked, so in essence I've had my question answered.
participants (3)
-
eg
-
James Porter
-
Robert Dailey