From: Dominik Stadler Date: Tue, 20 Sep 2016 20:24:54 +0000 (+0000) Subject: Adjust handling of ByteBuffer some more X-Git-Tag: REL_3_16_BETA1~172 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=406bc9c6c1ecd39c5f40e0567069a630b31a34e8;p=poi.git Adjust handling of ByteBuffer some more git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1761636 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java b/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java index 47637daa3a..32a8701fb8 100644 --- a/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java +++ b/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java @@ -88,27 +88,26 @@ public class FileBackedDataSource extends DataSource { throw new IndexOutOfBoundsException("Position " + position + " past the end of the file"); } - // Do we read or map (for read/write? + // Do we read or map (for read/write)? ByteBuffer dst; - int worked = -1; if (writable) { dst = channel.map(FileChannel.MapMode.READ_WRITE, position, length); - worked = 0; - // remember the buffer for cleanup if necessary - buffersToClean.add(dst); + + // remember this buffer for cleanup + buffersToClean.add(dst); } else { - // Read + // allocate the buffer on the heap if we cannot map the data in directly channel.position(position); dst = ByteBuffer.allocate(length); - worked = IOUtils.readFully(channel, dst); - } - // Check - if(worked == -1) { - throw new IndexOutOfBoundsException("Position " + position + " past the end of the file"); + // Read the contents and check that we could read some data + int worked = IOUtils.readFully(channel, dst); + if(worked == -1) { + throw new IndexOutOfBoundsException("Position " + position + " past the end of the file"); + } } - // Ready it for reading + // make it ready for reading dst.position(0); // All done