@ Override public long skip ( long n ) throws IOException { if ( mInputStream . available ( ) >= n ) { return mInputStream . skip ( n ) ; } // The number of bytes to skip is possibly large, open a new stream from GCS. mInputStream . close ( ) ; mPos += n ; try { mObject = mClient . getObject ( mBucketName , mKey , null /* ignore ModifiedSince */ , null /* ignore UnmodifiedSince */ , null /* ignore MatchTags */ , null /* ignore NoneMatchTags */ , mPos /* byteRangeStart */ , null /* ignore byteRangeEnd */ ) ; mInputStream = new BufferedInputStream ( mObject . getDataInputStream ( ) ) ; } catch ( ServiceException e ) { throw new IOException ( e ) ; } return n ; }
This method leverages the ability to open a stream from GCS from a given offset . When the underlying stream has fewer bytes buffered than the skip request the stream is closed and a new stream is opened starting at the requested offset .