Adjust handling of ByteBuffer some more

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1761636 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2016-09-20 20:24:54 +00:00
parent a4c3ce29ea
commit c669e2ef67

View File

@ -88,27 +88,26 @@ public class FileBackedDataSource extends DataSource {
throw new IndexOutOfBoundsException("Position " + position + " past the end of the file"); 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; ByteBuffer dst;
int worked = -1;
if (writable) { if (writable) {
dst = channel.map(FileChannel.MapMode.READ_WRITE, position, length); dst = channel.map(FileChannel.MapMode.READ_WRITE, position, length);
worked = 0;
// remember the buffer for cleanup if necessary // remember this buffer for cleanup
buffersToClean.add(dst); buffersToClean.add(dst);
} else { } else {
// Read // allocate the buffer on the heap if we cannot map the data in directly
channel.position(position); channel.position(position);
dst = ByteBuffer.allocate(length); dst = ByteBuffer.allocate(length);
worked = IOUtils.readFully(channel, dst);
}
// Check // Read the contents and check that we could read some data
int worked = IOUtils.readFully(channel, dst);
if(worked == -1) { if(worked == -1) {
throw new IndexOutOfBoundsException("Position " + position + " past the end of the file"); throw new IndexOutOfBoundsException("Position " + position + " past the end of the file");
} }
}
// Ready it for reading // make it ready for reading
dst.position(0); dst.position(0);
// All done // All done