Close channel when needed / Call force() only when necessary

- Contribution by @fredericBregier
This commit is contained in:
Trustin Lee 2012-05-30 17:26:41 -07:00
parent 92f010d688
commit dba9a6d408
2 changed files with 13 additions and 9 deletions

View File

@ -15,6 +15,9 @@
*/
package io.netty.handler.codec.http;
import io.netty.buffer.ChannelBuffer;
import io.netty.buffer.ChannelBuffers;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@ -24,9 +27,6 @@ import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import io.netty.buffer.ChannelBuffer;
import io.netty.buffer.ChannelBuffers;
/**
* Abstract Disk HttpData implementation
*/
@ -116,9 +116,9 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData {
int written = 0;
while (written < size) {
written += localfileChannel.write(byteBuffer);
localfileChannel.force(false);
}
buffer.readerIndex(buffer.readerIndex() + written);
localfileChannel.force(false);
localfileChannel.close();
completed = true;
}
@ -143,7 +143,6 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData {
}
while (written < localsize) {
written += fileChannel.write(byteBuffer);
fileChannel.force(false);
}
size += localsize;
buffer.readerIndex(buffer.readerIndex() + written);
@ -156,6 +155,7 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData {
FileOutputStream outputStream = new FileOutputStream(file);
fileChannel = outputStream.getChannel();
}
fileChannel.force(false);
fileChannel.close();
fileChannel = null;
completed = true;
@ -198,6 +198,8 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData {
localfileChannel.force(false);
read = inputStream.read(bytes);
}
localfileChannel.force(false);
localfileChannel.close();
size = written;
if (definedSize > 0 && definedSize < size) {
file.delete();
@ -300,6 +302,8 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData {
FileChannel in = inputStream.getChannel();
FileChannel out = outputStream.getChannel();
long destsize = in.transferTo(0, size, out);
in.close();
out.close();
if (destsize == size) {
file.delete();
file = dest;

View File

@ -15,6 +15,9 @@
*/
package io.netty.handler.codec.http;
import io.netty.buffer.ChannelBuffer;
import io.netty.buffer.ChannelBuffers;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@ -24,9 +27,6 @@ import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import io.netty.buffer.ChannelBuffer;
import io.netty.buffer.ChannelBuffers;
/**
* Abstract Memory HttpData implementation
*/
@ -212,8 +212,8 @@ public abstract class AbstractMemoryHttpData extends AbstractHttpData {
int written = 0;
while (written < length) {
written += fileChannel.write(byteBuffer);
fileChannel.force(false);
}
fileChannel.force(false);
fileChannel.close();
isRenamed = true;
return written == length;