@@ -31,23 +31,28 @@ public class CachingSeekableInputStream extends SeekableInputStream {
3131
3232 private final FileIO fileIO ;
3333 private final Path path ;
34- private final long fileSize ;
3534 private final BlockDiskCache cache ;
3635 private long pos ;
36+ private long fileSize = -1 ;
3737 @ Nullable private SeekableInputStream remoteStream ;
3838
39- public CachingSeekableInputStream (
40- FileIO fileIO , Path path , long fileSize , BlockDiskCache cache ) {
39+ public CachingSeekableInputStream (FileIO fileIO , Path path , BlockDiskCache cache ) {
4140 this .fileIO = fileIO ;
4241 this .path = path ;
43- this .fileSize = fileSize ;
4442 this .cache = cache ;
4543 this .pos = 0 ;
4644 }
4745
46+ private long fileSize () throws IOException {
47+ if (fileSize == -1 ) {
48+ fileSize = fileIO .getFileStatus (path ).getLen ();
49+ }
50+ return fileSize ;
51+ }
52+
4853 @ Override
4954 public void seek (long desired ) throws IOException {
50- this .pos = Math .max (0 , Math .min (desired , fileSize ));
55+ this .pos = Math .max (0 , Math .min (desired , fileSize () ));
5156 }
5257
5358 @ Override
@@ -57,7 +62,7 @@ public long getPos() throws IOException {
5762
5863 @ Override
5964 public int read () throws IOException {
60- if (pos >= fileSize ) {
65+ if (pos >= fileSize () ) {
6166 return -1 ;
6267 }
6368 int blockSize = cache .blockSize ();
@@ -73,12 +78,12 @@ public int read(byte[] b, int off, int len) throws IOException {
7378 if (len == 0 ) {
7479 return 0 ;
7580 }
76- if (pos >= fileSize ) {
81+ if (pos >= fileSize () ) {
7782 return -1 ;
7883 }
7984
8085 int blockSize = cache .blockSize ();
81- long end = Math .min (pos + len , fileSize );
86+ long end = Math .min (pos + len , fileSize () );
8287 int totalRead = 0 ;
8388
8489 while (pos < end ) {
@@ -106,7 +111,7 @@ private byte[] readBlock(int blockIndex) throws IOException {
106111
107112 int blockSize = cache .blockSize ();
108113 long offset = (long ) blockIndex * blockSize ;
109- int readSize = (int ) Math .min (blockSize , fileSize - offset );
114+ int readSize = (int ) Math .min (blockSize , fileSize () - offset );
110115
111116 SeekableInputStream stream = getRemoteStream ();
112117 stream .seek (offset );
0 commit comments