提交 97c5cb26 authored 作者: Thomas Mueller's avatar Thomas Mueller

Workaround for some FileChannel problems

上级 9a3e0e1c
......@@ -83,11 +83,22 @@ class FileNio extends FileBase {
@Override
public FileChannel truncate(long newLength) throws IOException {
try {
channel.truncate(newLength);
if (channel.position() > newLength) {
// looks like a bug in this FileChannel implementation, as the
// documentation says the position needs to be changed
channel.position(newLength);
long size = channel.size();
if (newLength < size) {
long pos = channel.position();
channel.truncate(newLength);
long newPos = channel.position();
if (pos < newLength) {
// position should stay
// in theory, this should not be needed
if (newPos != pos) {
channel.position(pos);
}
} else if (newPos > newLength) {
// looks like a bug in this FileChannel implementation, as the
// documentation says the position needs to be changed
channel.position(newLength);
}
}
return this;
} catch (NonWritableChannelException e) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论