Skip to content

Commit 9e6a5aa

Browse files
committed
ReadBufferDataHandle: cache length for performance
Closes #467.
1 parent b3b4326 commit 9e6a5aa

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/main/java/org/scijava/io/handle/ReadBufferDataHandle.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ public class ReadBufferDataHandle<L extends Location> extends AbstractHigherOrde
5555
private final LRUReplacementStrategy replacementStrategy;
5656
private final Map<Integer, Integer> pageToSlot;
5757

58+
/**
59+
* Cached length value, for performance. When reading data, length is not
60+
* expected to change, but querying it (e.g. via native filesystem access)
61+
* can be slow, and we need to query the length frequently.
62+
*/
63+
private long length = -1;
5864
private long offset = 0l;
5965
private byte[] currentPage;
6066
private int currentPageID = -1;
@@ -190,6 +196,12 @@ public void seek(final long pos) throws IOException {
190196
this.offset = pos;
191197
}
192198

199+
@Override
200+
public long length() throws IOException {
201+
if (length < 0) length = super.length();
202+
return length;
203+
}
204+
193205
@Override
194206
public int read(final byte[] b, final int targetOffset, final int len)
195207
throws IOException

0 commit comments

Comments
 (0)