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

istream

57 views
Skip to first unread message

Mark

unread,
Oct 8, 2014, 3:28:56 PM10/8/14
to

I'm receiving multipart messages from clients. These messages could be XML or imagery. The library I'm using returns a reference to the stream.
Ex:
std::istream& iss = myObject.stream();


At present Idetermine the size/length of the istream object by copying the istream object to a string. Ex:

std::string str ;
Poco::StreamCopier::copyToString ( iss, str );

How can I first determine the size/length of the stream before performing the copy operation?

I've tried.
std::streamsize const streamLen
= iss.rdbuf()->in_avail();

But streamLen is always zero.

Jorgen Grahn

unread,
Oct 8, 2014, 4:04:06 PM10/8/14
to
On Wed, 2014-10-08, Mark wrote:
>
> I'm receiving multipart messages from clients. These messages
> could be XML or imagery. The library I'm using returns a reference
> to the stream.
> Ex:
> std::istream& iss = myObject.stream();
>
> At present I determine the size/length of the istream object by
> copying the istream object to a string. Ex:
>
> std::string str ;
> Poco::StreamCopier::copyToString ( iss, str );
>
> How can I first determine the size/length of the stream before
> performing the copy operation?

(Don't you mean /instead of/ performing the copy?)

An istream is a stream: you don't know how long it is unless you
consume it and hit the end. That way the stream can be infinitely
long.

Why do you want to know the size? Hopefully there is an alternative
solution to whatever problem you have.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Christopher Pisz

unread,
Oct 8, 2014, 4:05:44 PM10/8/14
to
I might be talking out my butt here, but wouldn't you cast the istream
to the particular type of stream that it really is? istream is a base
class for concrete stream types, those types having a size method.

I'd assume istream does not because it doesnt even know what kind of
data it contains so it cant give you the number of elements.


Mark

unread,
Oct 8, 2014, 4:37:33 PM10/8/14
to

I have a threshold on the size of the imagery I'm willing to accept.

Clearly I could do

int const MAX_SIZE ( 0x100000) ;
typedef boost::shared_array < char > SHARED_ARRAY;
SHARED _ARRAY sequence ;
sequence.reset ( new char [ MAX_SIZE ] );
iss.read ( &sequence[0], MAX_SIZE );


But again if the size of the imagery the client sent me is greater (say 1 terabyte) than max_size I'd rather report a meaningful error rather than imposing a hack as shown above and I really don't want to allocate 1 terabyte of storage to determine 'hey size is unsupported'.

I want to determine the length/size from istream without having to copy the istream object via rdbuf or read or using the vendor utility. Sounds like the answer is no way around this. True/false?

Jorgen Grahn

unread,
Oct 8, 2014, 5:39:20 PM10/8/14
to
On Wed, 2014-10-08, Mark wrote:
>
> I have a threshold on the size of the imagery I'm willing to accept.
>
> Clearly I could do
>
> int const MAX_SIZE ( 0x100000) ;
> typedef boost::shared_array < char > SHARED_ARRAY;
> SHARED _ARRAY sequence ;
> sequence.reset ( new char [ MAX_SIZE ] );
> iss.read ( &sequence[0], MAX_SIZE );
>
> But again if the size of the imagery the client sent me is greater
> (say 1 terabyte) than max_size I'd rather report a meaningful error
> rather than imposing a hack as shown above and I really don't want to
> allocate 1 terabyte of storage to determine 'hey size is unsupported'.

No, you don't. Well, I'd say "rejecting the message, because it's
larger than 2 MB" is a decent error message to hand to a user.

(In superficially similar situations it's not. I hate filling in a
field in e.g. a web form and being told 'error, max 255 characters',
when I don't know if I've entered 340 characters or 257. But that's
when I'm likely to hit a very low limit without noticing, and when
it's obvious that the system knows exactly how many I entered.)

> I want to determine the length/size from istream without having to
> copy the istream object via rdbuf or read or using the vendor utility.
> Sounds like the answer is no way around this. True/false?

True, because like I said a std::istream can and often is infinite.

Someone upthread suggested casting to the "real" stream type, but
chances are that's not a type you have access to. Or if you do, it
doesn't know the size either.

Then there's the option to make yourself not care about the size. E.g.
a HTTP client can spend a year downloading a terabyte of data at a
kilobyte per second, letting the user's patience and disk quota set
the limit. Noone else gets hurt much. Not applicable in some other
scenarios, though.

Charles J. Daniels

unread,
Oct 8, 2014, 7:42:44 PM10/8/14
to
istream let's you read in chunks at a time in the size you specify, so you could read in 10k at a time, accumulate the input until done, and if you hit your absolute limit, choose to stop. So if your only concern was a max limit, that's fully covered depending on how you grab the data out of the stream. if you can use istream::read, that would do it

me

unread,
Nov 24, 2014, 9:23:34 PM11/24/14
to
As others have pointed out, istream has no concept of size. Streams just
flow and flow and flow. You will need to wrap it in something to add the
requested functionality yourself, and handle errors and exceptions
accordingly.

0 new messages