3

I want to read from an java.io.InputStream with a timeout. Apparently the correct way to do this is to use java.nio.channels.SelectableChannel and java.nio.channels.Selector. Unfortunately, it's not clear how to go from an InputStream to a SelectableChannel.

The InputStream is coming from a non-conventional source -- http://java.sun.com/products/javacomm/reference/api/javax/comm/CommPort.html#getInputStream()

2
  • 2
    Sorry Gili, that's not going to work with javax.comm. You might be able to hack the rxtx project to implement your own selectable channel... but unless you are trying to integrate with a package that already works with any SelectableChannel, that's probably more work than necessary to get non-blocking serial I/O.
    – erickson
    Apr 19, 2009 at 20:10
  • 1
    How would you implement a brand new SelectableChannel? It's not clear how RXTX can even provide its own implementation.
    – Gili
    Apr 19, 2009 at 22:00

2 Answers 2

3

Not all InputStreams are guaranteed to be selectable, so there is no straightforward way to do this. It's unfortunate that the CommPort class doesn't have a method for returning a SelectableChannel; there is no easy, portable way to accomplish this (i.e., without completely breaking encapsulation).

0
2

NIO is broken in this regard. All you have to do is look around a bit at the code and see all the casts that demand channels be SocketChannel etc. It's not possible to reuse anything in NIO for another type of filedescriptor based I/O channel like it should be.

Last I asked there were no plans to fix this for generic reuse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.