Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping InputStream to non blocking ReadableByteChannel

Tags:

java

nio

Is it possible to create non blocking ReadableByteChannel from InputStream object?

Channel.newChannel(inputStream) - produces channel which could be blocked by blocked InputStream.read invocation

like image 405
Mikhail Tsaplin Avatar asked Oct 31 '22 16:10

Mikhail Tsaplin


2 Answers

No it is not possible. See the Javadoc.

like image 176
user207421 Avatar answered Nov 08 '22 04:11

user207421


You may try to implement such a channel yourself, using 'Inputstream.avalable()' to avoid blocking. However, this method does not guarantee to return correct value, so you have to check the Inputstream implementation you use.

Are you sure you need non-blocking channel? Generally, it requires periodical polling to check arrival of data. Asynchronous channels, which invoke callbacks on data arrival, have more sense.

like image 28
Alexei Kaigorodov Avatar answered Nov 08 '22 04:11

Alexei Kaigorodov