[#1516] Fix resource leakage which was caused by the AbstractDiskHttpData which did not release the buffer after copy to disk

This commit is contained in:
Norman Maurer 2013-07-04 10:41:49 +02:00
parent 08b75e594c
commit 45d20d5c9f

View File

@ -99,55 +99,67 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData {
if (buffer == null) { if (buffer == null) {
throw new NullPointerException("buffer"); throw new NullPointerException("buffer");
} }
size = buffer.readableBytes(); try {
if (definedSize > 0 && definedSize < size) { size = buffer.readableBytes();
throw new IOException("Out of size: " + size + " > " + definedSize); if (definedSize > 0 && definedSize < size) {
throw new IOException("Out of size: " + size + " > " + definedSize);
}
if (file == null) {
file = tempFile();
}
if (buffer.readableBytes() == 0) {
// empty file
file.createNewFile();
return;
}
FileOutputStream outputStream = new FileOutputStream(file);
FileChannel localfileChannel = outputStream.getChannel();
ByteBuffer byteBuffer = buffer.nioBuffer();
int written = 0;
while (written < size) {
written += localfileChannel.write(byteBuffer);
}
buffer.readerIndex(buffer.readerIndex() + written);
localfileChannel.force(false);
localfileChannel.close();
outputStream.close();
completed = true;
} finally {
// Release the buffer as it was retained before and we not need a reference to it at all
// See https://github.com/netty/netty/issues/1516
buffer.release();
} }
if (file == null) {
file = tempFile();
}
if (buffer.readableBytes() == 0) {
// empty file
file.createNewFile();
return;
}
FileOutputStream outputStream = new FileOutputStream(file);
FileChannel localfileChannel = outputStream.getChannel();
ByteBuffer byteBuffer = buffer.nioBuffer();
int written = 0;
while (written < size) {
written += localfileChannel.write(byteBuffer);
}
buffer.readerIndex(buffer.readerIndex() + written);
localfileChannel.force(false);
localfileChannel.close();
outputStream.close();
completed = true;
} }
@Override @Override
public void addContent(ByteBuf buffer, boolean last) public void addContent(ByteBuf buffer, boolean last)
throws IOException { throws IOException {
if (buffer != null) { if (buffer != null) {
int localsize = buffer.readableBytes(); try {
if (definedSize > 0 && definedSize < size + localsize) { int localsize = buffer.readableBytes();
throw new IOException("Out of size: " + (size + localsize) + if (definedSize > 0 && definedSize < size + localsize) {
" > " + definedSize); throw new IOException("Out of size: " + (size + localsize) +
" > " + definedSize);
}
ByteBuffer byteBuffer = buffer.nioBufferCount() == 1 ? buffer.nioBuffer() : buffer.copy().nioBuffer();
int written = 0;
if (file == null) {
file = tempFile();
}
if (fileChannel == null) {
FileOutputStream outputStream = new FileOutputStream(file);
fileChannel = outputStream.getChannel();
}
while (written < localsize) {
written += fileChannel.write(byteBuffer);
}
size += localsize;
buffer.readerIndex(buffer.readerIndex() + written);
} finally {
// Release the buffer as it was retained before and we not need a reference to it at all
// See https://github.com/netty/netty/issues/1516
buffer.release();
} }
ByteBuffer byteBuffer = buffer.nioBufferCount() == 1 ? buffer.nioBuffer() : buffer.copy().nioBuffer();
int written = 0;
if (file == null) {
file = tempFile();
}
if (fileChannel == null) {
FileOutputStream outputStream = new FileOutputStream(file);
fileChannel = outputStream.getChannel();
}
while (written < localsize) {
written += fileChannel.write(byteBuffer);
}
size += localsize;
buffer.readerIndex(buffer.readerIndex() + written);
} }
if (last) { if (last) {
if (file == null) { if (file == null) {