public static ChunkHeader read ( IoBuffer in ) { int remaining = in . remaining ( ) ; if ( remaining > 0 ) { byte headerByte = in . get ( ) ; ChunkHeader h = new ChunkHeader ( ) ; // going to check highest 2 bits h . format = ( byte ) ( ( 0b11000000 & headerByte ) >> 6 ) ; int fmt = headerByte & 0x3f ; switch ( fmt ) { case 0 : // two byte header h . size = 2 ; if ( remaining < 2 ) { throw new ProtocolException ( "Bad chunk header, at least 2 bytes are expected" ) ; } h . channelId = 64 + ( in . get ( ) & 0xff ) ; break ; case 1 : // three byte header h . size = 3 ; if ( remaining < 3 ) { throw new ProtocolException ( "Bad chunk header, at least 3 bytes are expected" ) ; } byte b1 = in . get ( ) ; byte b2 = in . get ( ) ; h . channelId = 64 + ( ( b2 & 0xff ) << 8 | ( b1 & 0xff ) ) ; break ; default : // single byte header h . size = 1 ; h . channelId = 0x3f & headerByte ; break ; } // check channel id is valid if ( h . channelId < 0 ) { throw new ProtocolException ( "Bad channel id: " + h . channelId ) ; } log . trace ( "CHUNK header byte {}, count {}, header {}, channel {}" , String . format ( "%02x" , headerByte ) , h . size , 0 , h . channelId ) ; return h ; } else { // at least one byte for valid decode throw new ProtocolException ( "Bad chunk header, at least 1 byte is expected" ) ; } }
Read chunk header from the buffer .