RejectedSoftware Forums

Sign up

InputStream read has seemingly weird behaviour

cf. https://vibed.org/api/vibe.core.stream/InputStream.read

ubyte[256] buffer;
auto n = req.bodyReader.read(buffer, IOMode.once);

appears to work fine as long as there are 256 ubytes to read. If however the end of stream means there are less than 256 ubytes available then an exception is thrown.

At first sight this seems inconsistent with the read returning the number of ubytes read.

Or am I missing something?

Re: InputStream read has seemingly weird behaviour

On 2018-07-27 20:58, Russel Winder wrote:

cf. https://vibed.org/api/vibe.core.stream/InputStream.read

ubyte[256] buffer;
auto n = req.bodyReader.read(buffer, IOMode.once);

appears to work fine as long as there are 256 ubytes to read. If however the end of stream means there are less than 256 ubytes available then an exception is thrown.

At first sight this seems inconsistent with the read returning the number of ubytes read.

Or am I missing something?

The documentation says:

"The return value is guaranteed to be dst.length for IOMode.all."

"IOMode.all" is the default. I'm not sure if that's the reason.

/Jacob Carlborg

Re: InputStream read has seemingly weird behaviour

On Fri, 27 Jul 2018 22:03:10 +0200, Jacob Carlborg wrote:

On 2018-07-27 20:58, Russel Winder wrote:

cf. https://vibed.org/api/vibe.core.stream/InputStream.read

ubyte[256] buffer;
auto n = req.bodyReader.read(buffer, IOMode.once);

appears to work fine as long as there are 256 ubytes to read. If however the end of stream means there are less than 256 ubytes available then an exception is thrown.

At first sight this seems inconsistent with the read returning the number of ubytes read.

Or am I missing something?

The documentation says:

"The return value is guaranteed to be dst.length for IOMode.all."

"IOMode.all" is the default. I'm not sure if that's the reason.

It appears that the documentation does not reflect the behaviour of the code: it seems that the behaviour of the code is "fill the buffer totally or throw an exception". In effect the behaviour of the documentation is not implemented. I tried with IOMode.all as well as IOMode.once and the behaviour is the same.

It seems this bit of vibe.d has to be considered as broken. :-(

Re: InputStream read has seemingly weird behaviour

Am 28.07.2018 um 14:07 schrieb Russel Winder:

On Fri, 27 Jul 2018 22:03:10 +0200, Jacob Carlborg wrote:

On 2018-07-27 20:58, Russel Winder wrote:

cf. https://vibed.org/api/vibe.core.stream/InputStream.read

ubyte[256] buffer;
auto n = req.bodyReader.read(buffer, IOMode.once);

appears to work fine as long as there are 256 ubytes to read. If however the end of stream means there are less than 256 ubytes available then an exception is thrown.

At first sight this seems inconsistent with the read returning the number of ubytes read.

Or am I missing something?

The documentation says:

"The return value is guaranteed to be dst.length for IOMode.all."

"IOMode.all" is the default. I'm not sure if that's the reason.

It appears that the documentation does not reflect the behaviour of the code: it seems that the behaviour of the code is "fill the buffer totally or throw an exception". In effect the behaviour of the documentation is not implemented. I tried with IOMode.all as well as IOMode.once and the behaviour is the same.

It seems this bit of vibe.d has to be considered as broken. :-(

This is definitely a bug - the idea is indeed to have the buffer
partially filled. I'll have to look in the code, but it's likely that
there is a wrapper input stream involved (e.g. for chunked HTTP reading)
that doesn't implement proper support for IOMode, yet.

Re: InputStream read has seemingly weird behaviour

On 2018-07-29 10:50, Sönke Ludwig wrote:

This is definitely a bug - the idea is indeed to have the buffer
partially filled. I'll have to look in the code, but it's likely that
there is a wrapper input stream involved (e.g. for chunked HTTP reading)
that doesn't implement proper support for IOMode, yet.

How is vibe.d itself dealing with this without having an exception
thrown? This seems to be a pretty core part of the library.

/Jacob Carlborg

Re: InputStream read has seemingly weird behaviour

I found a workaround to the code that raised this issue, which is actually much nicer code. However it relies on the existence of the leastSize property. Unfortunately this property is scheduled for deprecation. This means you have to work with fixed size buffers rather than using a dynamic sized buffer, to avoid deprecated features. Obviously if read(buffer, IOMode) return the number of ubytes read (à la UNIX API style) then there is no actual problem, but being able to read a whole available buffer at a time is a really nice optimisation. One that apparently is being deprecated.

Unless I am just using the wrong algorithm and/or am missing a better way of doing things.

Re: InputStream read has seemingly weird behaviour

I have posted issue 2194 on the Vibe.d GitHub issues.

https://github.com/vibe-d/vibe.d/issues/2194