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.