public Message handleInputStream ( byte commandByte , InputStream inputStream ) { // Firmata command bytes are limited to a 0xF0 mask, or bytes 0xF0-0xFF // The extra data in the byte is for routing the command to various 'midi channels' // or in our case Firmata device pins. byte firmataCommandByte = commandByte < ( byte ) 0xF0 ? ( byte ) ( commandByte & 0xF0 ) : commandByte ; MessageBuilder messageBuilder = messageBuilderMap . get ( firmataCommandByte ) ; if ( messageBuilder != null ) { // Pull out the pin identifier bits that were padded into the byte (if any) byte channelByte = ( byte ) ( commandByte & 0x0F ) ; Message message = messageBuilder . buildMessage ( channelByte , inputStream ) ; if ( message == null ) { log . error ( "Error building Firmata messageBuilder for command byte {}." , FirmataHelper . bytesToHexString ( commandByte ) ) ; } else { return message ; } } else { log . warn ( "Dropped byte {}." , FirmataHelper . bytesToHexString ( commandByte ) ) ; } return null ; }
Attempts to identify the corresponding Firmata Message object that responds to the given commandByte If found it will ask for the message to be built using given inputStream . The builder will take away any bytes necessary to build the message and then return . To attempt to identify the correct message we must mask the incoming byte against 0xF0 if the byte is less than 0xF0 as commands below 0xF0 are only legal within this mask due to potential data padding in the same byte per the Firmata spec .