public Object readMessage() throws IOException, FormatErrorException {
  while (true) {
    fillBuffer(HDRLEN);
    long pos=position();
    int s1=buffer.get() & 0xFF;
    int s2=buffer.get() & 0xFF;
    int msgSize=s1 + (256 * s2);
    int msgType=buffer.get() & 0xFF;
    try {
      fillBuffer(msgSize);
    }
 catch (    EOFException e) {
      errors.add(new FormatErrorException(pos,"Unexpected end of file"));
      throw e;
    }
    Object msg;
switch (msgType) {
case MESSAGE_TYPE_DATA:
      s1=buffer.get() & 0xFF;
    s2=buffer.get() & 0xFF;
  int msgID=s1 + (256 * s2);
Subscription subscription=null;
if (msgID < messageSubscriptions.size()) subscription=messageSubscriptions.get(msgID);
if (subscription == null) {
position(pos);
errors.add(new FormatErrorException(pos,"Unknown DATA subscription ID: " + msgID));
buffer.position(buffer.position() + msgSize - 1);
continue;
}
msg=new MessageData(subscription.format,buffer,subscription.multiID);
break;
case MESSAGE_TYPE_INFO:
msg=new MessageInfo(buffer);
break;
case MESSAGE_TYPE_PARAMETER:
msg=new MessageParameter(buffer);
break;
case MESSAGE_TYPE_FORMAT:
msg=new MessageFormat(buffer,msgSize);
break;
case MESSAGE_TYPE_ADD_LOGGED_MSG:
msg=new MessageAddLogged(buffer,msgSize);
break;
case MESSAGE_TYPE_DROPOUT:
msg=new MessageDropout(buffer);
break;
case MESSAGE_TYPE_LOG:
msg=new MessageLog(buffer,msgSize);
break;
case MESSAGE_TYPE_REMOVE_LOGGED_MSG:
case MESSAGE_TYPE_SYNC:
buffer.position(buffer.position() + msgSize);
continue;
default :
buffer.position(buffer.position() + msgSize);
errors.add(new FormatErrorException(pos,"Unknown message type: " + msgType));
continue;
}
int sizeParsed=(int)(position() - pos - HDRLEN);
if (sizeParsed != msgSize) {
errors.add(new FormatErrorException(pos,"Message size mismatch, parsed: " + sizeParsed + ", msg size: "+ msgSize));
buffer.position(buffer.position() + msgSize - sizeParsed);
}
return msg;
}
}

public Object readMessage ( ) throws IOException , FormatErrorException { while ( true ) { fillBuffer ( HDRLEN ) ; long pos = position ( ) ; int s1 = buffer . get ( ) & 0xFF ; int s2 = buffer . get ( ) & 0xFF ; int msgSize = s1 + ( 256 * s2 ) ; int msgType = buffer . get ( ) & 0xFF ; try { fillBuffer ( msgSize ) ; } catch ( EOFException e ) { errors . add ( new FormatErrorException ( pos , "Unexpected end of file" ) ) ; throw e ; } Object msg ; switch ( msgType ) { case MESSAGE_TYPE_DATA : s1 = buffer . get ( ) & 0xFF ; s2 = buffer . get ( ) & 0xFF ; int msgID = s1 + ( 256 * s2 ) ; Subscription subscription = null ; if ( msgID < messageSubscriptions . size ( ) ) subscription = messageSubscriptions . get ( msgID ) ; if ( subscription == null ) { position ( pos ) ; errors . add ( new FormatErrorException ( pos , "Unknown DATA subscription ID: " + msgID ) ) ; buffer . position ( buffer . position ( ) + msgSize - 1 ) ; continue ; } msg = new MessageData ( subscription . format , buffer , subscription . multiID ) ; break ; case MESSAGE_TYPE_INFO : msg = new MessageInfo ( buffer ) ; break ; case MESSAGE_TYPE_PARAMETER : msg = new MessageParameter ( buffer ) ; break ; case MESSAGE_TYPE_FORMAT : msg = new MessageFormat ( buffer , msgSize ) ; break ; case MESSAGE_TYPE_ADD_LOGGED_MSG : msg = new MessageAddLogged ( buffer , msgSize ) ; break ; case MESSAGE_TYPE_DROPOUT : msg = new MessageDropout ( buffer ) ; break ; case MESSAGE_TYPE_LOG : msg = new MessageLog ( buffer , msgSize ) ; break ; case MESSAGE_TYPE_REMOVE_LOGGED_MSG : case MESSAGE_TYPE_SYNC : buffer . position ( buffer . position ( ) + msgSize ) ; continue ; default : buffer . position ( buffer . position ( ) + msgSize ) ; errors . add ( new FormatErrorException ( pos , "Unknown message type: " + msgType ) ) ; continue ; } int sizeParsed = ( int ) ( position ( ) - pos - HDRLEN ) ; if ( sizeParsed != msgSize ) { errors . add ( new FormatErrorException ( pos , "Message size mismatch, parsed: " + sizeParsed + ", msg size: " + msgSize ) ) ; buffer . position ( buffer . position ( ) + msgSize - sizeParsed ) ; } return msg ; } }
read next message from log
