Close channel when needed and optimize force() position after multiple writes not at each step

This commit is contained in:
Frédéric Brégier 2012-04-30 12:29:05 +03:00
parent 94b10d2d02
commit 65876fa7fb

View File

@ -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;
@ -195,9 +195,10 @@ public abstract class AbstractDiskHttpData extends AbstractHttpData {
while (read > 0) {
byteBuffer.position(read).flip();
written += localfileChannel.write(byteBuffer);
localfileChannel.force(false);
read = inputStream.read(bytes);
}
localfileChannel.force(false);
localfileChannel.close();
size = written;
if (definedSize > 0 && definedSize < size) {
file.delete();
@ -300,6 +301,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;