game-engine/src/main/java/com/jzy/game/engine/mina/websocket/WebSocketDecoder.java
// all and return one complete decoded buffer.
all and return one complete decoded buffer.
all and return one complete decoded buffer .
private static IoBuffer buildWSDataBuffer(IoBuffer in, IoSession session) {
        IoBuffer resultBuffer = null;
        do{
            byte frameInfo = in.get();            
            byte opCode = (byte) (frameInfo & 0x0f);
            if (opCode == 8) {
                session.close(true);
                return resultBuffer;
            }        
            int frameLen = (in.get() & (byte) 0x7F);
            if(frameLen == 126){
                frameLen = in.getShort();
            }
            if(frameLen+4 > in.remaining()){                
                return null;
            }
            byte mask[] = new byte[4];
            for (int i = 0; i < 4; i++) {
                mask[i] = in.get();
            }
            /*  now un-mask frameLen bytes as per Section 5.3 RFC 6455
                Octet i of the transformed data ("transformed-octet-i") is the XOR of
                octet i of the original data ("original-octet-i") with octet at index
                i modulo 4 of the masking key ("masking-key-octet-j"):
                j                   = i MOD 4
                transformed-octet-i = original-octet-i XOR masking-key-octet-j
            * 
            */
            byte[] unMaskedPayLoad = new byte[frameLen];
            for (int i = 0; i < frameLen; i++) {
                byte maskedByte = in.get();
                unMaskedPayLoad[i] = (byte) (maskedByte ^ mask[i % 4]);
            }
            if(resultBuffer == null){
                resultBuffer = IoBuffer.wrap(unMaskedPayLoad);
                resultBuffer.position(resultBuffer.limit());
                resultBuffer.setAutoExpand(true);
            }
            else{
                resultBuffer.put(unMaskedPayLoad);
            }
        }
        while(in.hasRemaining());
        resultBuffer.flip();
        return resultBuffer;
    }
private static IoBuffer buildWSDataBuffer ( IoBuffer in , IoSession session ) { IoBuffer resultBuffer = null ; do { byte frameInfo = in . get ( ) ; byte opCode = ( byte ) ( frameInfo & 0x0f ) ; if ( opCode == 8 ) { session . close ( true ) ; return resultBuffer ; } int frameLen = ( in . get ( ) & ( byte ) 0x7F ) ; if ( frameLen == 126 ) { frameLen = in . getShort ( ) ; } if ( frameLen + 4 > in . remaining ( ) ) { return null ; } byte mask [ ] = new byte [ 4 ] ; for ( int i = 0 ; i < 4 ; i ++ ) { mask [ i ] = in . get ( ) ; } /*  now un-mask frameLen bytes as per Section 5.3 RFC 6455
                Octet i of the transformed data ("transformed-octet-i") is the XOR of
                octet i of the original data ("original-octet-i") with octet at index
                i modulo 4 of the masking key ("masking-key-octet-j"):

                j                   = i MOD 4
                transformed-octet-i = original-octet-i XOR masking-key-octet-j
            * 
            */ byte [ ] unMaskedPayLoad = new byte [ frameLen ] ; for ( int i = 0 ; i < frameLen ; i ++ ) { byte maskedByte = in . get ( ) ; unMaskedPayLoad [ i ] = ( byte ) ( maskedByte ^ mask [ i % 4 ] ) ; } if ( resultBuffer == null ) { resultBuffer = IoBuffer . wrap ( unMaskedPayLoad ) ; resultBuffer . position ( resultBuffer . limit ( ) ) ; resultBuffer . setAutoExpand ( true ) ; } else { resultBuffer . put ( unMaskedPayLoad ) ; } } while ( in . hasRemaining ( ) ) ; resultBuffer . flip ( ) ; return resultBuffer ; }
all and return one complete decoded buffer.
all and return one complete decoded buffer .
// opCode 8 means close. See RFC 6455 Section 5.2 // return what ever is parsed till now. // Validate if we have enough data in the buffer to completely // parse the WebSocket DataFrame. If not return null.