public static IoBuffer encodeOutgoingData ( Packet packet ) { log . debug ( "encode outgoing: {}" , packet ) ; // get the payload data IoBuffer data = packet . getData ( ) ; // get the frame length based on the byte count int frameLen = data . limit ( ) ; // start with frame length + 2b (header info) IoBuffer buffer = IoBuffer . allocate ( frameLen + 2 , false ) ; buffer . setAutoExpand ( true ) ; // set the proper flags / opcode for the data byte frameInfo = ( byte ) ( 1 << 7 ) ; switch ( packet . getType ( ) ) { case TEXT : if ( log . isTraceEnabled ( ) ) { log . trace ( "Encoding text frame \r\n{}" , new String ( packet . getData ( ) . array ( ) ) ) ; } frameInfo = ( byte ) ( frameInfo | 1 ) ; break ; case BINARY : log . trace ( "Encoding binary frame" ) ; frameInfo = ( byte ) ( frameInfo | 2 ) ; break ; case CLOSE : frameInfo = ( byte ) ( frameInfo | 8 ) ; break ; case CONTINUATION : frameInfo = ( byte ) ( frameInfo | 0 ) ; break ; case PING : log . trace ( "ping out" ) ; frameInfo = ( byte ) ( frameInfo | 9 ) ; break ; case PONG : log . trace ( "pong out" ) ; frameInfo = ( byte ) ( frameInfo | 0xa ) ; break ; default : break ; } buffer . put ( frameInfo ) ; // set the frame length log . trace ( "Frame length {} " , frameLen ) ; if ( frameLen <= 125 ) { buffer . put ( ( byte ) ( ( byte ) frameLen & ( byte ) 0x7F ) ) ; } else if ( frameLen > 125 && frameLen <= 65535 ) { buffer . put ( ( byte ) ( ( byte ) 126 & ( byte ) 0x7F ) ) ; buffer . putShort ( ( short ) frameLen ) ; } else { buffer . put ( ( byte ) ( ( byte ) 127 & ( byte ) 0x7F ) ) ; buffer . putLong ( ( int ) frameLen ) ; } buffer . put ( data ) ; buffer . flip ( ) ; if ( log . isTraceEnabled ( ) ) { log . trace ( "Encoded: {}" , buffer ) ; } return buffer ; }
Encode the in buffer according to the Section 5 . 2 . RFC 6455