Skip to content
This repository has been archived by the owner on Aug 25, 2021. It is now read-only.

ByteSourceInputStream read(byte[]) behaves as readFully when it should tryRead #8

Open
mbrannstrom opened this issue Feb 16, 2016 · 0 comments

Comments

@mbrannstrom
Copy link
Contributor

The read(byte[]) method in ByteSource was designed to behave like readFully in DataInputStream, i.e. fail when trying to read more data than available. For example the Blink codec relies on this behaviour.

The read(byte[]) method in InputStream is designed to read as much as possible, i.e. a "try read". The wrapper ByteSourceInputStream copies the behaviour from ByteSource to InputStream, and apparently this is not OK. For example the JSON codec (Jackson behind the scenes) relies on the "try read" behaviour to fill an internal parse buffer from the InputStream. This will fail whenever the internal parse buffer is larger than the remaining data, i.e. in most cases.

The ByteSourceInputStream wrapper is not easily fixed, since there is no "tryRead" in ByteSource or any way to find out how many remaining bytes there are.

I think an API change in ByteSource is hard to avoid, and hence this will likely be a major version bump. For example, rename existing read(byte[]) into readFully and add a new "try" read(byte[]) with InputStream semantics, or add a int remaining() or add tryRead(byte[]). All implementations of ByteSource will break...

If you need a failing test case in msgcodec-json to illustrate this, let me know.

mbrannstrom added a commit to mbrannstrom/msgcodec that referenced this issue Jun 14, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant