如何定义自己的SelectableChannel?

时间:2022-10-31 19:00:40

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

我如何定义一种新类型的java.nio.channels.SelectableChannel(比如串口)?

2 个解决方案

#1


My understanding is that the java implementation is based on the unix select() c function (I seem to remember that the Windows implementation was slightly different)

我的理解是java实现基于unix select()c函数(我似乎记得Windows实现略有不同)

Depending on OS (and JVM args!) different native OS functions are called, but what they have in common is that it's native code - the basic functionality is not implemented in Java.

根据OS(和JVM args!),调用不同的本机OS函数,但它们的共同点是它的本机代码 - 基本功能不是用Java实现的。

If you want to make a lib that access the select() (or similar) of the underlying OS (which does indeed rely on filehandles), I think you are pretty much forced to use JNI. I don't believe there are any ways around it.

如果你想创建一个访问底层操作系统的select()(或类似)的lib(确实依赖于文件句柄),我认为你几乎*使用JNI。我不相信有任何方法可以解决它。

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

Java中的Selector / SelectableChannel实际上是select()可以做的贫血子集。

#2


You probably want to extend java.nio.channels.spi.AbstractSelectableChannel to create the implementation that you need. If you are asking for something different, you need to give a more detailed question. The JDK source code is downloadable under a few different licenses, depending on what version you are downloading. You have the option of viewing the JDK implementations (java.nio.channels.Channel, java.nio.channels.SocketChannel, etc...) to fully understand what you need to implement. If you do this, however, be careful not to copy code from the JDK source unless you can obey the license of the source code that you downloaded.

您可能希望扩展java.nio.channels.spi.AbstractSelectableChannel以创建所需的实现。如果你要求不同的东西,你需要提出一个更详细的问题。 JDK源代码可以在几个不同的许可证下下载,具体取决于您下载的版本。您可以选择查看JDK实现(java.nio.channels.Channel,java.nio.channels.SocketChannel等等)以完全了解您需要实现的内容。但是,如果执行此操作,请注意不要从JDK源复制代码,除非您可以遵守下载的源代码的许可证。

The book Java NIO may help you.

Java NIO一书可以帮到你。

#1


My understanding is that the java implementation is based on the unix select() c function (I seem to remember that the Windows implementation was slightly different)

我的理解是java实现基于unix select()c函数(我似乎记得Windows实现略有不同)

Depending on OS (and JVM args!) different native OS functions are called, but what they have in common is that it's native code - the basic functionality is not implemented in Java.

根据OS(和JVM args!),调用不同的本机OS函数,但它们的共同点是它的本机代码 - 基本功能不是用Java实现的。

If you want to make a lib that access the select() (or similar) of the underlying OS (which does indeed rely on filehandles), I think you are pretty much forced to use JNI. I don't believe there are any ways around it.

如果你想创建一个访问底层操作系统的select()(或类似)的lib(确实依赖于文件句柄),我认为你几乎*使用JNI。我不相信有任何方法可以解决它。

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

Java中的Selector / SelectableChannel实际上是select()可以做的贫血子集。

#2


You probably want to extend java.nio.channels.spi.AbstractSelectableChannel to create the implementation that you need. If you are asking for something different, you need to give a more detailed question. The JDK source code is downloadable under a few different licenses, depending on what version you are downloading. You have the option of viewing the JDK implementations (java.nio.channels.Channel, java.nio.channels.SocketChannel, etc...) to fully understand what you need to implement. If you do this, however, be careful not to copy code from the JDK source unless you can obey the license of the source code that you downloaded.

您可能希望扩展java.nio.channels.spi.AbstractSelectableChannel以创建所需的实现。如果你要求不同的东西,你需要提出一个更详细的问题。 JDK源代码可以在几个不同的许可证下下载,具体取决于您下载的版本。您可以选择查看JDK实现(java.nio.channels.Channel,java.nio.channels.SocketChannel等等)以完全了解您需要实现的内容。但是,如果执行此操作,请注意不要从JDK源复制代码,除非您可以遵守下载的源代码的许可证。

The book Java NIO may help you.

Java NIO一书可以帮到你。