void gatherProtocol ( int maxPos ) throws IOException { // protocol buffer has max capacity, shouldn't need resizing try { while ( this . bufferPosition < maxPos ) { byte b = this . buffer [ this . bufferPosition ] ; this . bufferPosition ++ ; if ( gotCR ) { if ( b == NatsConnection . LF ) { this . protocolBuffer . flip ( ) ; this . mode = Mode . PARSE_PROTO ; this . gotCR = false ; break ; } else { throw new IllegalStateException ( "Bad socket data, no LF after CR" ) ; } } else if ( b == NatsConnection . CR ) { this . gotCR = true ; } else { if ( ! protocolBuffer . hasRemaining ( ) ) { this . protocolBuffer = this . connection . enlargeBuffer ( this . protocolBuffer , 0 ) ; // just double it } this . protocolBuffer . put ( b ) ; } } } catch ( IllegalStateException | NumberFormatException | NullPointerException ex ) { this . encounteredProtocolError ( ex ) ; } }
Gather bytes for a protocol line