public MAVLinkMessage(MAVLinkSchema schema,ByteBuffer buffer) throws MAVLinkProtocolException, MAVLinkUnknownMessage, BufferUnderflowException {
  if (buffer.remaining() < NON_PAYLOAD_LENGTH) {
    throw new BufferUnderflowException();
  }
  int startPos=buffer.position();
  byte startSign=buffer.get();
  if (startSign != START_OF_FRAME) {
    throw new MAVLinkProtocolException(String.format("Invalid start sign: %02x, should be %02x",startSign,START_OF_FRAME));
  }
  int payloadLen=buffer.get() & 0xff;
  if (buffer.remaining() < payloadLen + NON_PAYLOAD_LENGTH - 2) {
    buffer.position(startPos);
    throw new BufferUnderflowException();
  }
  sequence=buffer.get();
  systemID=buffer.get() & 0xff;
  componentID=buffer.get() & 0xff;
  msgID=buffer.get() & 0xff;
  this.schema=schema;
  this.definition=schema.getMessageDefinition(msgID);
  if (definition == null) {
    buffer.position(buffer.position() + payloadLen + CRC_LENGTH);
    throw new MAVLinkUnknownMessage(String.format("Unknown message: %s",msgID));
  }
  if (payloadLen != definition.payloadLength) {
    buffer.position(buffer.position() + payloadLen + CRC_LENGTH);
    throw new MAVLinkUnknownMessage(String.format("Invalid payload len for msg %s (%s): %s, should be %s",definition.name,msgID,payloadLen,definition.payloadLength));
  }
  this.payload=new byte[definition.payloadLength];
  buffer.get(payload);
  crc=Short.reverseBytes(buffer.getShort()) & 0xffff;
  int endPos=buffer.position();
  buffer.position(startPos);
  int crcCalc=calculateCRC(buffer);
  buffer.position(endPos);
  if (crc != crcCalc) {
    throw new MAVLinkUnknownMessage(String.format("CRC error for msg %s (%s): %02x, should be %02x",definition.name,msgID,crc,crcCalc));
  }
  this.payloadBB=ByteBuffer.wrap(payload);
  payloadBB.order(schema.getByteOrder());
}

public MAVLinkMessage ( MAVLinkSchema schema , ByteBuffer buffer ) throws MAVLinkProtocolException , MAVLinkUnknownMessage , BufferUnderflowException { if ( buffer . remaining ( ) < NON_PAYLOAD_LENGTH ) { throw new BufferUnderflowException ( ) ; } int startPos = buffer . position ( ) ; byte startSign = buffer . get ( ) ; if ( startSign != START_OF_FRAME ) { throw new MAVLinkProtocolException ( String . format ( "Invalid start sign: %02x, should be %02x" , startSign , START_OF_FRAME ) ) ; } int payloadLen = buffer . get ( ) & 0xff ; if ( buffer . remaining ( ) < payloadLen + NON_PAYLOAD_LENGTH - 2 ) { buffer . position ( startPos ) ; throw new BufferUnderflowException ( ) ; } sequence = buffer . get ( ) ; systemID = buffer . get ( ) & 0xff ; componentID = buffer . get ( ) & 0xff ; msgID = buffer . get ( ) & 0xff ; this . schema = schema ; this . definition = schema . getMessageDefinition ( msgID ) ; if ( definition == null ) { buffer . position ( buffer . position ( ) + payloadLen + CRC_LENGTH ) ; throw new MAVLinkUnknownMessage ( String . format ( "Unknown message: %s" , msgID ) ) ; } if ( payloadLen != definition . payloadLength ) { buffer . position ( buffer . position ( ) + payloadLen + CRC_LENGTH ) ; throw new MAVLinkUnknownMessage ( String . format ( "Invalid payload len for msg %s (%s): %s, should be %s" , definition . name , msgID , payloadLen , definition . payloadLength ) ) ; } this . payload = new byte [ definition . payloadLength ] ; buffer . get ( payload ) ; crc = Short . reverseBytes ( buffer . getShort ( ) ) & 0xffff ; int endPos = buffer . position ( ) ; buffer . position ( startPos ) ; int crcCalc = calculateCRC ( buffer ) ; buffer . position ( endPos ) ; if ( crc != crcCalc ) { throw new MAVLinkUnknownMessage ( String . format ( "CRC error for msg %s (%s): %02x, should be %02x" , definition . name , msgID , crc , crcCalc ) ) ; } this . payloadBB = ByteBuffer . wrap ( payload ) ; payloadBB . order ( schema . getByteOrder ( ) ) ; }
create message from buffer ( for parsing )
