From fd1d11ec395509c488979d4cdfc41b63f9df6ee0 Mon Sep 17 00:00:00 2001 From: norman Date: Wed, 12 Oct 2011 17:09:02 +0200 Subject: [PATCH] Add support for FileRegion in OIOWorker. --- .../netty/channel/socket/oio/OioWorker.java | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/jboss/netty/channel/socket/oio/OioWorker.java b/src/main/java/org/jboss/netty/channel/socket/oio/OioWorker.java index 74e18ef942..eaee7fbdb8 100644 --- a/src/main/java/org/jboss/netty/channel/socket/oio/OioWorker.java +++ b/src/main/java/org/jboss/netty/channel/socket/oio/OioWorker.java @@ -20,12 +20,15 @@ import static org.jboss.netty.channel.Channels.*; import java.io.OutputStream; import java.io.PushbackInputStream; import java.net.SocketException; +import java.nio.channels.Channels; import java.nio.channels.ClosedChannelException; +import java.nio.channels.WritableByteChannel; import java.util.regex.Pattern; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelFuture; +import org.jboss.netty.channel.FileRegion; /** * @@ -119,13 +122,39 @@ class OioWorker implements Runnable { } try { - ChannelBuffer a = (ChannelBuffer) message; - int length = a.readableBytes(); - synchronized (out) { - a.getBytes(a.readerIndex(), out, length); + int length = 0; + + // Add support to write a FileRegion. This in fact will not give any performance gain but at least it not fail and + // we did the best to emulate it + if (message instanceof FileRegion) { + FileRegion fr = (FileRegion) message; + try { + synchronized (out) { + WritableByteChannel bchannel = Channels.newChannel(out); + + long i = 0; + while ((i = fr.transferTo(bchannel, length)) > 0) { + length += i; + if (length >= fr.getCount()) { + break; + } + } + } + } finally { + fr.releaseExternalResources(); + + } + } else { + ChannelBuffer a = (ChannelBuffer) message; + length = a.readableBytes(); + synchronized (out) { + a.getBytes(a.readerIndex(), out, length); + } } + fireWriteComplete(channel, length); future.setSuccess(); + } catch (Throwable t) { // Convert 'SocketException: Socket closed' to // ClosedChannelException.