How to define your own SelectableChannel?

How to define a new type java.nio.channels.SelectableChannel (say for serial ports)?

+6
java nio
source share
2 answers

I understand that the java implementation is based on the unix select() c function (I seem to remember that the Windows implementation was a bit different)

Depending on the OS (and JVM args!), Different native OS functions are called, but what they have in common is that this is their own code - the basic functionality is not implemented in Java.

If you want to create a lib that accesses the select() (or the like) of the underlying OS (which really relies on file descriptors), I think you are pretty much forced to use JNI. I do not believe that there are any ways around.

The / SelectableChannel in Java is indeed an anemic subset of what select() can do.

+3
source share

You probably want to extend java.nio.channels.spi.AbstractSelectableChannel to create the implementation you need. If you ask for something else, you need to ask a more detailed question. The JDK source code can be downloaded in several licenses, depending on which version you download. You have the ability to view JDK implementations ( java.nio.channels.Channel , java.nio.channels.SocketChannel , etc.) to fully understand what you need to implement. However, if you do this, be careful not to copy the code from the JDK source if you cannot license the source code you downloaded.

Java NIO book can help you.

+2
source share

All Articles