public boolean parse(ByteBuffer buffer) {
        while (buffer.hasRemaining()) {
            switch (state) {
                case LENGTH: {
                    int octet = buffer.get() & 0xFF;
                    length = (length << 8) + octet;
                    if (++cursor == 3) {
                        length &= Frame.MAX_MAX_LENGTH;
                        state = State.TYPE;
                    }
                    break;
                }
                case TYPE: {
                    type = buffer.get() & 0xFF;
                    state = State.FLAGS;
                    break;
                }
                case FLAGS: {
                    flags = buffer.get() & 0xFF;
                    state = State.STREAM_ID;
                    break;
                }
                case STREAM_ID: {
                    if (buffer.remaining() >= 4) {
                        streamId = buffer.getInt();
                        // Most significant bit MUST be ignored as per specification.
                        streamId &= 0x7F_FF_FF_FF;
                        return true;
                    } else {
                        state = State.STREAM_ID_BYTES;
                        cursor = 4;
                    }
                    break;
                }
                case STREAM_ID_BYTES: {
                    int currByte = buffer.get() & 0xFF;
                    --cursor;
                    streamId += currByte << (8 * cursor);
                    if (cursor == 0) {
                        // Most significant bit MUST be ignored as per specification.
                        streamId &= 0x7F_FF_FF_FF;
                        return true;
                    }
                    break;
                }
                default: {
                    throw new IllegalStateException();
                }
            }
        }
        return false;
    }
public boolean parse ( ByteBuffer buffer ) { while ( buffer . hasRemaining ( ) ) { switch ( state ) { case LENGTH : { int octet = buffer . get ( ) & 0xFF ; length = ( length << 8 ) + octet ; if ( ++ cursor == 3 ) { length &= Frame . MAX_MAX_LENGTH ; state = State . TYPE ; } break ; } case TYPE : { type = buffer . get ( ) & 0xFF ; state = State . FLAGS ; break ; } case FLAGS : { flags = buffer . get ( ) & 0xFF ; state = State . STREAM_ID ; break ; } case STREAM_ID : { if ( buffer . remaining ( ) >= 4 ) { streamId = buffer . getInt ( ) ; streamId &= 0x7F_FF_FF_FF ; return true ; } else { state = State . STREAM_ID_BYTES ; cursor = 4 ; } break ; } case STREAM_ID_BYTES : { int currByte = buffer . get ( ) & 0xFF ; -- cursor ; streamId += currByte << ( 8 * cursor ) ; if ( cursor == 0 ) { streamId &= 0x7F_FF_FF_FF ; return true ; } break ; } default : { throw new IllegalStateException ( ) ; } } } return false ; }
<p>
Parses the header bytes in the given {@code buffer}; only the header
bytes are consumed, therefore when this method returns, the buffer may
contain unconsumed bytes.
</p>

@param buffer the buffer to parse
@return true if the whole header bytes were parsed, false if not enough
header bytes were present in the buffer