Bug 1927 - No direct mapping for InputStream.available()
Summary: No direct mapping for InputStream.available()
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 1.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2011-11-08 02:29 UTC by Igor
Modified: 2014-03-28 07:52 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Igor 2011-11-08 02:29:05 UTC
Java's InputStream from BluetoothSocket is indirectly mapped into System.IO.Stream.

How then I can use java's is.available() to detect if there is any data in the stream?
There is no direct mapping for such a logic in the System.IO.Stream.

> We're wondering why you need polling access to the stream. InputStream.available() is documented as not being very useful:

I'm not sure why android's version of javadoc includes this passage. But from common sense - the available() method's efficiency depends etirely on the underlying implementation. And in case of network streams and bluetooth streams (which are BTW essentially the same nature) - data availability check is a common sence. Even .net's NetworkStream has its DataAvailable.

I mean yes, think I can workaround this by creating a separate thread and data queue (or using BeginRead), and essentially implementing my own buffering logic and available() check. But thats just another layer over the data stream abstraction - because network and bluetooth streams are for sure already contains internal queue/buffer.
> In particular, is there a reason you don't want to use Stream.BeginRead() or some other async mechanism? Why do you need polling?

We need polling because our current interaction model is based on polling.
> If there's a good need for polling, we're considering providing a `bool DataAvailable(this Stream stream)` extension method (thus translating the dubious `int` InputStream.available() return value into a `bool`, and this mechanism could also check for NetworkStream  and return NetworkStream.DataAvailable), but before we provide this we'd like to know how useful it actually is.

Think thats logical to provide access to the information underlying stream already has. I was just surprised abit that no direct api mapping was done here. I understand InputStream and IO.Stream are different, but still.
Comment 1 Jonathan Pryor 2011-11-08 16:32:22 UTC
Commit 765cf184 adds accessors to the underlying types to the relevant wrappers, e.g. there will be an Android.Runtime.InputStreamInvoker.BaseInputStream property of type Java.IO.InputStream.

Currently you'd still need to do a runtime check to see if the Stream is an InputStreamInvoker before calling invoker.BaseInputStream.Available(), but it'll be possible.
Comment 2 Jonathan Pryor 2011-11-09 13:43:41 UTC
Commit 146fa7f6 adds a Stream.IsDataAvailable() extension method.

I renamed it from `DataAvailable` so that if the source type is a NetworkStream then DataAvailable and IsDataAvailable can be used independently, as DataAvailable is a property while IsDataAvailable() is a method.
Comment 3 Udham Singh 2014-03-28 07:52:24 UTC
I have checked this issue on following builds:

Windows 8.1
Xamarin Studio : 4.2.3 (build 60)
Xamarin.Android : 4.13.0 (Business Edition)

Screencast:  http://screencast.com/t/0CaAz5fqhyn

Now I am getting InputStream.Available() directly. Hence, I am closing this issue.