private long validateMessage ( FileChannel channel , long start , long len , ByteBuffer buffer ) throws IOException { buffer . rewind ( ) ; int read = channel . read ( buffer , start ) ; if ( read < 4 ) return - 1 ; // check that we have sufficient bytes left in the file int size = buffer . getInt ( 0 ) ; if ( size < Message . MinHeaderSize ) return - 1 ; long next = start + 4 + size ; if ( next > len ) return - 1 ; // read the message ByteBuffer messageBuffer = ByteBuffer . allocate ( size ) ; long curr = start + 4 ; while ( messageBuffer . hasRemaining ( ) ) { read = channel . read ( messageBuffer , curr ) ; if ( read < 0 ) throw new IllegalStateException ( "File size changed during recovery!" ) ; else curr += read ; } messageBuffer . rewind ( ) ; Message message = new Message ( messageBuffer ) ; if ( ! message . isValid ( ) ) return - 1 ; else return next ; }
Read validate and discard a single message returning the next valid offset and the message being validated