diff --git a/src/main/java/org/jboss/netty/channel/DefaultFileRegion.java b/src/main/java/org/jboss/netty/channel/DefaultFileRegion.java index 4bc9419462..57f72aa0b4 100644 --- a/src/main/java/org/jboss/netty/channel/DefaultFileRegion.java +++ b/src/main/java/org/jboss/netty/channel/DefaultFileRegion.java @@ -14,21 +14,29 @@ public class DefaultFileRegion implements FileRegion { private final FileChannel file; private final long position; private final long count; + private boolean releaseAfterTransfer; - public DefaultFileRegion(FileChannel file, long position, long count) { + public DefaultFileRegion(FileChannel file, long position, long count, boolean releaseAfterTransfer) { this.file = file; this.position = position; this.count = count; + this.releaseAfterTransfer = releaseAfterTransfer; } + public DefaultFileRegion(FileChannel file, long position, long count) { + this(file, position, count, false); + } + public long getPosition() { return position; } + public long getCount() { return count; } + public long transferTo(WritableByteChannel target, long position) throws IOException { long count = this.count - position; if (count < 0 || position < 0) { @@ -43,6 +51,7 @@ public class DefaultFileRegion implements FileRegion { return file.transferTo(this.position + position, count, target); } + public void releaseExternalResources() { try { file.close(); @@ -50,4 +59,13 @@ public class DefaultFileRegion implements FileRegion { logger.warn("Failed to close a file.", e); } } + + + public boolean releaseAfterTransfer() { + return releaseAfterTransfer; + } + + public void setReleaseAfterTransfer(boolean releaseAfterTransfer) { + this.releaseAfterTransfer = releaseAfterTransfer; + } } diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/SocketSendBufferPool.java b/src/main/java/org/jboss/netty/channel/socket/nio/SocketSendBufferPool.java index d4f898ab8c..9b1900fb12 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/SocketSendBufferPool.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/SocketSendBufferPool.java @@ -23,6 +23,7 @@ import java.nio.channels.DatagramChannel; import java.nio.channels.WritableByteChannel; import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.channel.DefaultFileRegion; import org.jboss.netty.channel.FileRegion; /** @@ -288,8 +289,12 @@ final class SocketSendBufferPool { } public void release() { - // Make sure the FileRegion resource are released otherwise it may cause a FD leak or something similar - file.releaseExternalResources(); + if (file instanceof DefaultFileRegion) { + if (((DefaultFileRegion)file).releaseAfterTransfer()) { + // Make sure the FileRegion resource are released otherwise it may cause a FD leak or something similar + file.releaseExternalResources(); + } + } } } 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 eaee7fbdb8..03c3104647 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 @@ -28,6 +28,7 @@ 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.DefaultFileRegion; import org.jboss.netty.channel.FileRegion; /** @@ -141,7 +142,11 @@ class OioWorker implements Runnable { } } } finally { - fr.releaseExternalResources(); + if (fr instanceof DefaultFileRegion) { + if (((DefaultFileRegion)fr).releaseAfterTransfer()) { + fr.releaseExternalResources(); + } + } } } else {