public void position ( long position ) throws IOException { if ( position == 0 ) { currentStreamState . setNextLedgerEntryId ( firstLedgerEntryId ) ; currentStreamState . setOffsetInEntry ( 0 ) ; entryStream = null ; } else if ( savedStreamState == null || position != savedStreamState . getReaderPosition ( ) ) { // Seek to an arbitrary position through "brute force" if ( position > Integer . MAX_VALUE ) { throw new IllegalArgumentException ( "Asked to position to " + position + ", but can only \"brute-force\" skip up" + Integer . MAX_VALUE ) ; } position ( 0 ) ; skip ( position , ( int ) position ) ; } else { // savedStream != null && position == savedStream.getReaderPosition() int bytesToSkip = 0 ; if ( savedStreamState . getOffsetInLedger ( ) > position ) { // Since reading from the input stream is buffered, we usually will // read further into the ledger than the reader has actually // read into. In this case we will need to find out exactly *what* // position within the ledger entry matches with the reader's last // known good position. long entryStartPosition = savedStreamState . getOffsetInLedger ( ) - savedStreamState . getOffsetInEntry ( ) ; bytesToSkip = ( int ) ( position - entryStartPosition ) ; } else if ( savedStreamState . getOffsetInLedger ( ) < position ) { throw new IllegalArgumentException ( "Saved offset in ledger (" + savedStreamState . getOffsetInLedger ( ) + ") < position(" + position + ")" ) ; } long nextLedgerEntryId = savedStreamState . getNextLedgerEntryId ( ) == firstLedgerEntryId ? firstLedgerEntryId : ( savedStreamState . getNextLedgerEntryId ( ) - 1 ) ; currentStreamState . setNextLedgerEntryId ( nextLedgerEntryId ) ; if ( bytesToSkip > 0 ) { entryStream = null ; skip ( position , bytesToSkip ) ; } else { if ( currentStreamState . getNextLedgerEntryId ( ) > 0 ) { currentStreamState . setNextLedgerEntryId ( currentStreamState . getNextLedgerEntryId ( ) - 1 ) ; } entryStream = nextEntryStream ( ) ; } } currentStreamState . setOffsetInLedger ( position ) ; }
Go back to the specified reader position by resetting the reader a saved state associated with that position .