This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes SocketChannel and OutputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "SocketChannel and OutputStream" Watch "SocketChannel and OutputStream" New topic

SocketChannel and OutputStream

Ranch Hand

Joined: Jul 24, 2002
Posts: 379
Hi guys, I'm trying to write a very simple servlet container which uses NIO. I've got a Response class which receives in its constructor a SocketChannel is non blocking mode. This class implements javax.servlet.ServletResponse and I implemented the getWriter() method as follows (writer is of type PrintWriter, client is of type SocketChannel):

In my servlet I've got the following:

I got the following exception:


Before returning the PrintWriter from getWriter I checked the channel which is is non-blocking state and open.
In the servlet, after getWriter the 'out' object is actually of type PrintWriter (not null) but when I first write something, I get this exception.

Please help.


Marco Tedone<br />SCJP1.4,SCJP5,SCBCD,SCWCD
Ranch Hand

Joined: Jul 24, 2002
Posts: 379
I found the solution. The problem was indeed the blocking state of the SocketChannel. Even if I tried to set the blocking mode to true, the AbstractSelectableChannel was throwing the IllegalBlockingException. I had a look at the source, and this happened because the SocketChannel had a valid key associated with it. This is true, because I registered each SocketChannel client with a Selector, in order to be notified of bytes available on the client's input stream. I had code like the following:

Then I had a closer look at my Nio book and actually to remove a key from service we need to invoke key.cancel() method.

So I replaced the above with the following:

If you use key.cancel(), this removes the relationship between a selector and a channel, and therefore you can change the blocking mode as many times as you like. For instance, you may decide for non-blocking mode if you have to serve static content (because a FileChannel can transfer directly to a SocketChannel), or for blocking mode if you need to operate with Streams (like in the case where you are implementing the javax.servlet.ServletResponse interface and the getWriter() method needs to return a PrintWriter.

The rule is: blocking with blocking, non-blocking with non-blocking.
[ November 14, 2004: Message edited by: Marco Tedone ]
I agree. Here's the link:
subject: SocketChannel and OutputStream
It's not a secret anymore!