Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

size of std::istream object

56 views
Skip to first unread message

Mark

unread,
Nov 16, 2014, 2:19:13 PM11/16/14
to

I'm using a library that returns an istream object. At present I do:

std::istream &stream = readerObj.stream () ;
std::stringstream str;
str << stream.rdbuf() ;

I'd like to determine the size of the istream object before extraction with rdbuf(). How can I achieve this.


NOTE: If the returned object was an ifstream object then I can seek to the end and determine the size in advance but this is an 'istream' object and tellg and seek doesn't work as it does with ifstream

Thanks

Ian Collins

unread,
Nov 16, 2014, 2:25:52 PM11/16/14
to
Mark wrote:
>
> I'm using a library that returns an istream object. At present I
> do:
>
> std::istream &stream = readerObj.stream () ; std::stringstream str;
> str << stream.rdbuf() ;
>
> I'd like to determine the size of the istream object before
> extraction with rdbuf(). How can I achieve this.

Not all sources have a known size.

--
Ian Collins
Message has been deleted

Ian Collins

unread,
Nov 16, 2014, 3:00:55 PM11/16/14
to
Stefan Ram wrote:
> Ian Collins <ian-...@hotmail.com> writes:
>> Not all sources have a known size.
>
> »sizeof stream« should be the »number of bytes in the object
> representation of« »stream«.

While true, that isn't how the question was phrased.

--
Ian Collins

Mark

unread,
Nov 16, 2014, 3:19:21 PM11/16/14
to
Stefan I'm not in front of a compiler right now but are you saying I should be able to do sizeof(stream) to achieve my objective?

Luca Risolia

unread,
Nov 16, 2014, 3:26:26 PM11/16/14
to
Il 16/11/2014 20:19, Mark ha scritto:
>
> I'm using a library that returns an istream object. At present I do:
>
> std::istream &stream = readerObj.stream () ;
> std::stringstream str;
> str << stream.rdbuf() ;
>
> I'd like to determine the size of the istream object before extraction with rdbuf(). How can I achieve this.

There is no "size" associated with streams. However, you can obtain a
lower bound on the characters available in a stream buffer with
std::basic_streambuf::in_avail()

Ian Collins

unread,
Nov 16, 2014, 3:34:25 PM11/16/14
to
Mark wrote:

Please quote contest and wrap your lines!

> Stefan I'm not in front of a compiler right now but are you saying I
> should be able to do sizeof(stream) to achieve my objective?

You need to clarify your question, do you mean the size of the object (I
doubt), or the amount of data to be read?

--
Ian Collins

Öö Tiib

unread,
Nov 16, 2014, 5:34:28 PM11/16/14
to
On Sunday, 16 November 2014 21:19:13 UTC+2, Mark wrote:
> I'm using a library that returns an istream object. At present I do:
>
> std::istream &stream = readerObj.stream () ;
> std::stringstream str;
> str << stream.rdbuf() ;

You copy everything from stream to stream?

> I'd like to determine the size of the istream object before extraction with
> rdbuf(). How can I achieve this.

You can't. There can be 'istream' whose size is not constant. For example
it can be some fifo or pipe that other application is writing into from
other side. So if the size matters then you have to read block by block.
At positive side '.read()' (or '.readsome()') and '.write()' cycles are
usually better performing than those copies.

Mark

unread,
Nov 16, 2014, 5:55:39 PM11/16/14
to
On Sunday, November 16, 2014 5:34:28 PM UTC-5, Öö Tiib wrote:

> > I'd like to determine the size of the istream object before extraction with
> > rdbuf(). How can I achieve this.
>
> You can't. There can be 'istream' whose size is not constant. For example
> it can be some fifo or pipe that other application is writing into from
> other side. So if the size matters then you have to read block by block.
> At positive side '.read()' (or '.readsome()') and '.write()' cycles are
> usually better performing than those copies.

Thanks a lot. I switched over to usignt the read method
unsigned int const MAX_SIZE_SUPPORTED ( 10485760 );
typedef boost::shared_array < char > BOOST_SHARED_CHAR_ARRAY ;
BOOST_SHARED_CHAR_ARRAY ptrBase64Data ;
ptrBase64Data.reset ( new char [ MAX_SIZE_SUPPORTED ] ) ;
std::istream &stream = readerObj.stream () ;
unsigned int offset ( 0 );
unsigned int const payload ( 1048576 );
do {
std::streamsize const xxx = stream.read
( &ptrBase64Data [ offset ], payload ).gcount() ;
offset += xxx ;
//to offset against MAX_SIZE_SUPPORTED so we don't crash
} while ( !stream.good() ) ;
Mark

Chris Vine

unread,
Nov 16, 2014, 6:54:09 PM11/16/14
to
No. Stefan is giving a pedantic but correct answer to the question you
put. Unfortunately the question you put is not the one to which you
wanted an answer.

You are not interested in "the size of the istream object", as you
thought you were. You are interested in the size of the data
accessible from the stream. In many cases that is unbounded
(terminals, sockets, pipes, fifos for example), as others have pointed
out.

Chris

Christopher Pisz

unread,
Nov 17, 2014, 12:42:49 PM11/17/14
to
You already asked this question about 20 messages down the list. What's
changed?

red floyd

unread,
Nov 17, 2014, 2:06:08 PM11/17/14
to
Only nit I have is that offset should probably std::streamsize
instead of unsigned.

Nobody

unread,
Nov 19, 2014, 4:52:43 AM11/19/14
to
On Sun, 16 Nov 2014 14:34:18 -0800, Öö Tiib wrote:

>> I'd like to determine the size of the istream object before extraction with
>> rdbuf(). How can I achieve this.
>
> You can't. There can be 'istream' whose size is not constant. For example
> it can be some fifo or pipe that other application is writing into from
> other side.

It could even be a regular file which some other application is writing
into.

In that case, it will have a defined size at any moment in time, but that
doesn't mean that you'll be able to read it all before the size changes.

0 new messages