DataInputStream read vs. readFully

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

DataInputStream read vs. readFully

Ufuk Celebi-2
Hey devs,

had a quick discussion with Stephan about usage of DataInputStream
read and readFully. In most cases we want to use readFully, but it's
easy to accidentally use read instead (happened to me here for
example: https://issues.apache.org/jira/browse/FLINK-4332), which only
reads as many bytes as are available in the stream.

We asked ourselves whether it is possible to prevent read calls by
default and only allow them on a case by case basis. Unfortunately, I
couldn't find anything that could work.

Does someone else have an idea there?

– Ufuk
mxm
Reply | Threaded
Open this post in threaded view
|

Re: DataInputStream read vs. readFully

mxm
Hi Ufuk,

`read(buf)` is not always `read(buf, 0, buf.length)`. Whereas
`readFully(buf)` ensures `read(buf, 0, buf.length)`, right? The method
is clearly documented but these mistakes can happen like a forgotten
null pointer check.

If we want to prevent mistakes like this, we can replace `read(buf)`
with `readFully(buf)` by default. This wouldn't break any code. We
could then add another method to explicitly enable partial reads.

Cheers,
Max


On Wed, Sep 28, 2016 at 12:34 PM, Ufuk Celebi <[hidden email]> wrote:

> Hey devs,
>
> had a quick discussion with Stephan about usage of DataInputStream
> read and readFully. In most cases we want to use readFully, but it's
> easy to accidentally use read instead (happened to me here for
> example: https://issues.apache.org/jira/browse/FLINK-4332), which only
> reads as many bytes as are available in the stream.
>
> We asked ourselves whether it is possible to prevent read calls by
> default and only allow them on a case by case basis. Unfortunately, I
> couldn't find anything that could work.
>
> Does someone else have an idea there?
>
> – Ufuk