public long seekToPrevRecord ( LogRecordSerializer formatter ) throws IOException { long position = reader . getFilePointer ( ) ; byte [ ] buffer = new byte [ 2048 ] ; int location ; int offset = 0 ; int len = 0 ; do { if ( position <= formatter . getEyeCatcherSize ( ) + 3 ) { throw new IOException ( "No eyeCatcher found in the rest of the file." ) ; } if ( position > buffer . length ) { len = buffer . length ; } else { len = ( int ) position ; } position -= len ; if ( offset > 0 ) { // keep the first eyeCatcherSize-1 bytes of the buffer. for ( int i = 0 ; i < offset ; i ++ ) { buffer [ len - offset + i ] = buffer [ i ] ; } } reader . seek ( position ) ; reader . readFully ( buffer , 0 , len - offset ) ; if ( offset == 0 ) { offset = formatter . getEyeCatcherSize ( ) - 1 ; } } while ( ( location = formatter . findLastEyeCatcher ( buffer , 0 , len ) ) < 0 ) ; position += location - 4 ; reader . seek ( position ) ; return position ; }
Repositions reader to the location of the previous record . This is done by searching prev eyeCatcher and then seek 4 bytes before its start .