本文介绍了如何从InputStream获取SelectableChannel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从java.io.InputStream中读取带有超时的内容.显然,执行此操作的正确方法是使用java.nio.channels.SelectableChannel和java.nio.channels.Selector.不幸的是,目前尚不清楚如何从InputStream转到SelectableChannel.

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.

InputStream来自非常规来源- http://java.sun.com/products/javacomm/reference/api/javax/comm/CommPort.html#getInputStream()

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

推荐答案

并非所有InputStream都可以保证是可选择的,因此没有简单的方法可以做到这一点.不幸的是CommPort类没有返回SelectableChannel的方法.没有简单,可移植的方式来完成此操作(即不完全破坏封装).

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).

这篇关于如何从InputStream获取SelectableChannel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 18:47