Merge pull request #34 from normanmaurer/3.2
Only release FileRegion if configured to do so. See NETTY-440
This commit is contained in:
commit
dbccc9e52b
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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,10 +289,14 @@ final class SocketSendBufferPool {
|
||||
}
|
||||
|
||||
public void release() {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static final class EmptySendBuffer implements SendBuffer {
|
||||
|
||||
|
@ -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 {
|
||||
if (fr instanceof DefaultFileRegion) {
|
||||
if (((DefaultFileRegion)fr).releaseAfterTransfer()) {
|
||||
fr.releaseExternalResources();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user