From c641f24e8f947f4b17854f984693bdf0462bce74 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Sat, 22 Oct 2011 20:27:48 +0200 Subject: [PATCH] Only release FileRegion after transfer was done if its configured to do so. The default is true in the master branch. See NETTY-440 --- .../netty/channel/DefaultFileRegion.java | 26 ++++++++++++++++++- .../org/jboss/netty/channel/FileRegion.java | 8 ++++++ .../socket/nio/SocketSendBufferPool.java | 6 +++-- .../netty/channel/socket/oio/OioWorker.java | 4 ++- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/jboss/netty/channel/DefaultFileRegion.java b/src/main/java/org/jboss/netty/channel/DefaultFileRegion.java index 9a8bc100ee..f07c435d83 100644 --- a/src/main/java/org/jboss/netty/channel/DefaultFileRegion.java +++ b/src/main/java/org/jboss/netty/channel/DefaultFileRegion.java @@ -14,13 +14,28 @@ 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; } + /** + * Calls {@link #DefaultFileRegion(FileChannel, long, long, boolean)} with true + * as the last argument. + * + * @param file + * @param position + * @param count + */ + public DefaultFileRegion(FileChannel file, long position, long count) { + this(file, position, count, true); + } + + @Override public long getPosition() { return position; @@ -54,4 +69,13 @@ public class DefaultFileRegion implements FileRegion { logger.warn("Failed to close a file.", e); } } + + @Override + public boolean releaseAfterTransfer() { + return releaseAfterTransfer; + } + + public void setReleaseAfterTransfer(boolean releaseAfterTransfer) { + this.releaseAfterTransfer = releaseAfterTransfer; + } } diff --git a/src/main/java/org/jboss/netty/channel/FileRegion.java b/src/main/java/org/jboss/netty/channel/FileRegion.java index 7399453e4d..cd5c5cc7d1 100644 --- a/src/main/java/org/jboss/netty/channel/FileRegion.java +++ b/src/main/java/org/jboss/netty/channel/FileRegion.java @@ -84,4 +84,12 @@ public interface FileRegion extends ExternalResourceReleasable { * byte of the region transferred. */ long transferTo(WritableByteChannel target, long position) throws IOException; + + /** + * Returns true if {@link #releaseExternalResources()} should be called after the + * transfer of the {@link FileRegion} was complete. + * + * @return release + */ + public boolean releaseAfterTransfer(); } \ No newline at end of file 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 4c29ee79b3..36606ce784 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 @@ -306,8 +306,10 @@ final class SocketSendBufferPool { @Override public void release() { - // Make sure the FileRegion resource are released otherwise it may cause a FD leak or something similar - file.releaseExternalResources(); + if (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 82fac7509d..69814d0a48 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 @@ -141,7 +141,9 @@ class OioWorker implements Runnable { } } } finally { - fr.releaseExternalResources(); + if (fr.releaseAfterTransfer()) { + fr.releaseExternalResources(); + } } } else {