diff --git a/src/main/java/loci/common/NIOFileHandle.java b/src/main/java/loci/common/NIOFileHandle.java index d370e588..bf291b8b 100644 --- a/src/main/java/loci/common/NIOFileHandle.java +++ b/src/main/java/loci/common/NIOFileHandle.java @@ -105,6 +105,8 @@ public class NIOFileHandle extends AbstractNIOHandle { /** The original length of the file. */ private Long defaultLength; + private long effectiveLength; + // -- Constructors -- /** @@ -121,6 +123,7 @@ public NIOFileHandle(File file, String mode, int bufferSize) mapMode = FileChannel.MapMode.READ_WRITE; } raf = new RandomAccessFile(file, mode); + effectiveLength = raf.length(); channel = raf.getChannel(); byteBufferProvider = new NIOByteBufferProvider(channel, mapMode); buffer(position, 0); @@ -199,8 +202,9 @@ public int getBufferSize() { @Override public void setLength(long length) throws IOException { if (raf.length() < length) { - raf.setLength(length); + raf.setLength(length + getBufferSize()); } + effectiveLength = length; raf.seek(length - 1); buffer = null; } @@ -210,6 +214,9 @@ public void setLength(long length) throws IOException { /* @see IRandomAccess.close() */ @Override public void close() throws IOException { + if (isReadWrite && channel.isOpen() && raf.length() != length()) { + raf.setLength(length()); + } raf.close(); } @@ -225,7 +232,7 @@ public long length() throws IOException { if (defaultLength != null) { return defaultLength; } - return raf.length(); + return effectiveLength; } /* @see IRandomAccess.getOrder() */