Search code examples
javasocketsionio

Mixing Java NIO with IO input streams read operation


I need to use NIO to allow the server side to support timeout on write operations, but handling reading operations on the socket channels complicates my program.

I was wondering if it's possible to write to the sockets using NIO but to read using regular IO, something like

((SocketChannel) selectedKey.channel()).socket().getInputStream().read(buffer)

Will this work?


Solution

  • No. You can only use streams on the channel socket if the channel is in blocking mode, in which case you wouldn't have a SelectionKey.