@@ -105,6 +105,8 @@ public class NIOFileHandle extends AbstractNIOHandle {
105105 /** The original length of the file. */
106106 private Long defaultLength ;
107107
108+ private long effectiveLength ;
109+
108110 // -- Constructors --
109111
110112 /**
@@ -121,6 +123,7 @@ public NIOFileHandle(File file, String mode, int bufferSize)
121123 mapMode = FileChannel .MapMode .READ_WRITE ;
122124 }
123125 raf = new RandomAccessFile (file , mode );
126+ effectiveLength = raf .length ();
124127 channel = raf .getChannel ();
125128 byteBufferProvider = new NIOByteBufferProvider (channel , mapMode );
126129 buffer (position , 0 );
@@ -199,8 +202,9 @@ public int getBufferSize() {
199202 @ Override
200203 public void setLength (long length ) throws IOException {
201204 if (raf .length () < length ) {
202- raf .setLength (length );
205+ raf .setLength (length + getBufferSize () );
203206 }
207+ effectiveLength = length ;
204208 raf .seek (length - 1 );
205209 buffer = null ;
206210 }
@@ -210,6 +214,9 @@ public void setLength(long length) throws IOException {
210214 /* @see IRandomAccess.close() */
211215 @ Override
212216 public void close () throws IOException {
217+ if (isReadWrite && channel .isOpen () && raf .length () != length ()) {
218+ raf .setLength (length ());
219+ }
213220 raf .close ();
214221 }
215222
@@ -225,7 +232,7 @@ public long length() throws IOException {
225232 if (defaultLength != null ) {
226233 return defaultLength ;
227234 }
228- return raf . length () ;
235+ return effectiveLength ;
229236 }
230237
231238 /* @see IRandomAccess.getOrder() */
0 commit comments