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

View File

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