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

Workaround for some FileChannel problems

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