aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org
diff options
context:
space:
mode:
authorDominik Stadler <centic@apache.org>2020-05-16 13:06:07 +0000
committerDominik Stadler <centic@apache.org>2020-05-16 13:06:07 +0000
commit3cad9e685173bd33e13bd2407f0d8f6cfe0c5522 (patch)
tree5d0e97314f1457559b6a609ccc72d4be3106ff1e /src/java/org
parentff919eb0e4b25a7ba9a97ad6c8fdc64a381a96b0 (diff)
downloadpoi-3cad9e685173bd33e13bd2407f0d8f6cfe0c5522.tar.gz
poi-3cad9e685173bd33e13bd2407f0d8f6cfe0c5522.zip
Bug 64322: Optimize performance of reading ole2 files
Remember channel-size to no re-read it for every read-access, but reset it if we extend the size of the file profiling indicates Channel.size() sometimes has similar runtime overhead as position() or read()! git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1877816 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org')
-rw-r--r--src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java18
1 files changed, 16 insertions, 2 deletions
diff --git a/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java b/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java
index 5b77db4d92..2d4af278c1 100644
--- a/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java
+++ b/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java
@@ -39,6 +39,8 @@ public class FileBackedDataSource extends DataSource {
private final static POILogger logger = POILogFactory.getLogger(FileBackedDataSource.class);
private final FileChannel channel;
+ private Long channelSize;
+
private final boolean writable;
// remember file base, which needs to be closed too
private RandomAccessFile srcFile;
@@ -97,8 +99,9 @@ public class FileBackedDataSource extends DataSource {
// remember this buffer for cleanup
buffersToClean.put(dst,dst);
} else {
- // allocate the buffer on the heap if we cannot map the data in directly
channel.position(position);
+
+ // allocate the buffer on the heap if we cannot map the data in directly
dst = ByteBuffer.allocate(length);
// Read the contents and check that we could read some data
@@ -118,6 +121,11 @@ public class FileBackedDataSource extends DataSource {
@Override
public void write(ByteBuffer src, long position) throws IOException {
channel.write(src, position);
+
+ // we have to re-read size if we write "after" the recorded one
+ if(channelSize != null && position >= channelSize) {
+ channelSize = null;
+ }
}
@Override
@@ -131,7 +139,13 @@ public class FileBackedDataSource extends DataSource {
@Override
public long size() throws IOException {
- return channel.size();
+ // this is called often and profiling showed that channel.size()
+ // was taking a large part of processing-time, so we only read it
+ // once
+ if(channelSize == null) {
+ channelSize = channel.size();
+ }
+ return channelSize;
}
public void releaseBuffer(ByteBuffer buffer) {