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

Workaround for some FileChannel problems

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