@Override
    public Message buildMessage(Byte channelByte, InputStream inputStream) {
        byte sysexCommandByte;
        try {
            sysexCommandByte = (byte) inputStream.read();
        } catch (IOException e) {
            log.error("Error reading Sysex command byte.");
            return null;
        }

        ByteArrayOutputStream messageBodyBuilder = new ByteArrayOutputStream(16);
        try {
            int messagePiece;
            while ((messagePiece = inputStream.read()) != -1) {
                if ((byte) messagePiece == CommandBytes.END_SYSEX.getCommandByte()) {
                    break;
                }
                messageBodyBuilder.write(messagePiece);
            }
        } catch (IOException e) {
            log.error("Error reading Sysex message body for command {}.",
                    FirmataHelper.bytesToHexString(sysexCommandByte));
            return null;
        }

        byte[] messageBodyBytes = messageBodyBuilder.toByteArray();

        try {
            messageBodyBuilder.close();
        } catch (IOException e) {
            log.error("Programming error. Cannot close our byte buffer.");
        }

        SysexMessageBuilder messageBuilder = messageBuilderMap.get(sysexCommandByte);
        if (messageBuilder == null) {
            log.error("There is no Sysex message parser registered for command {}. Body: {}",
                    FirmataHelper.bytesToHexString(sysexCommandByte),
                    FirmataHelper.bytesToHexString(messageBodyBuilder.toByteArray()));
            return null;
        }

        return messageBuilder.buildMessage(messageBodyBytes);
    }
@ Override public Message buildMessage ( Byte channelByte , InputStream inputStream ) { byte sysexCommandByte ; try { sysexCommandByte = ( byte ) inputStream . read ( ) ; } catch ( IOException e ) { log . error ( "Error reading Sysex command byte." ) ; return null ; } ByteArrayOutputStream messageBodyBuilder = new ByteArrayOutputStream ( 16 ) ; try { int messagePiece ; while ( ( messagePiece = inputStream . read ( ) ) != - 1 ) { if ( ( byte ) messagePiece == CommandBytes . END_SYSEX . getCommandByte ( ) ) { break ; } messageBodyBuilder . write ( messagePiece ) ; } } catch ( IOException e ) { log . error ( "Error reading Sysex message body for command {}." , FirmataHelper . bytesToHexString ( sysexCommandByte ) ) ; return null ; } byte [ ] messageBodyBytes = messageBodyBuilder . toByteArray ( ) ; try { messageBodyBuilder . close ( ) ; } catch ( IOException e ) { log . error ( "Programming error. Cannot close our byte buffer." ) ; } SysexMessageBuilder messageBuilder = messageBuilderMap . get ( sysexCommandByte ) ; if ( messageBuilder == null ) { log . error ( "There is no Sysex message parser registered for command {}. Body: {}" , FirmataHelper . bytesToHexString ( sysexCommandByte ) , FirmataHelper . bytesToHexString ( messageBodyBuilder . toByteArray ( ) ) ) ; return null ; } return messageBuilder . buildMessage ( messageBodyBytes ) ; }
Attempts to identify the corresponding Firmata Sysex Message object that responds to the
identified sysexCommandByte. If found, it will ask for the sysex message to be built using
the identified bytes between the sysexCommandByte and the END_SYSEX commandByte later in the input stream.

@param channelByte channelByte indicating the pin/port to perform the command against.
@param inputStream InputStream representing the data following the START_SYSEX command byte.
@return Message representing the Firmata Sysex Message that the SerialPort communications device sent.