public int receive ( final MessageHandler handler ) { int messagesReceived = 0 ; final BroadcastReceiver receiver = this . receiver ; final long lastSeenLappedCount = receiver . lappedCount ( ) ; if ( receiver . receiveNext ( ) ) { if ( lastSeenLappedCount != receiver . lappedCount ( ) ) { throw new IllegalStateException ( "unable to keep up with broadcast" ) ; } final int length = receiver . length ( ) ; final int capacity = scratchBuffer . capacity ( ) ; if ( length > capacity && ! scratchBuffer . isExpandable ( ) ) { throw new IllegalStateException ( "buffer required length of " + length + " but only has " + capacity ) ; } final int msgTypeId = receiver . typeId ( ) ; scratchBuffer . putBytes ( 0 , receiver . buffer ( ) , receiver . offset ( ) , length ) ; if ( ! receiver . validate ( ) ) { throw new IllegalStateException ( "unable to keep up with broadcast" ) ; } handler . onMessage ( msgTypeId , scratchBuffer , 0 , length ) ; messagesReceived = 1 ; } return messagesReceived ; }
Receive one message from the broadcast buffer .