public static int verifyPacket(ByteBuffer buffer) {
        final int position = buffer.position();
        // Get the packet length and wait until we get a packets worth of data to unwrap.
        if (buffer.remaining() < SSL_RECORD_HEADER_LENGTH) {
            NetBareLog.w("No enough ssl/tls packet length: " + buffer.remaining());
            return PACKET_NOT_ENOUGH;
        }
        int packetLength = 0;
        // SSLv3 or TLS - Check ContentType
        boolean tls;
        switch (unsignedByte(buffer, position)) {
            case SSL_CONTENT_TYPE_CHANGE_CIPHER_SPEC:
            case SSL_CONTENT_TYPE_ALERT:
            case SSL_CONTENT_TYPE_HANDSHAKE:
            case SSL_CONTENT_TYPE_APPLICATION_DATA:
            case SSL_CONTENT_TYPE_EXTENSION_HEARTBEAT:
                tls = true;
                break;
            default:
                // SSLv2 or bad data
                tls = false;
        }
        if (tls) {
            // SSLv3 or TLS - Check ProtocolVersion
            int majorVersion = unsignedByte(buffer, position + 1);
            if (majorVersion == 3) {
                // SSLv3 or TLS
                packetLength = unsignedShort(buffer, position + 3) + SSL_RECORD_HEADER_LENGTH;
                if (packetLength <= SSL_RECORD_HEADER_LENGTH) {
                    // Neither SSLv3 or TLSv1 (i.e. SSLv2 or bad data)
                    tls = false;
                }
            } else {
                // Neither SSLv3 or TLSv1 (i.e. SSLv2 or bad data)
                tls = false;
            }
        }
        if (!tls) {
            // SSLv2 or bad data - Check the version
            int headerLength = (unsignedByte(buffer, position) & 0x80) != 0 ? 2 : 3;
            int majorVersion = unsignedByte(buffer, position + headerLength + 1);
            if (majorVersion == 2 || majorVersion == 3) {
                // SSLv2
                packetLength = headerLength == 2 ?
                        (buffer.getShort(position) & 0x7FFF) + 2 : (buffer.getShort(position) & 0x3FFF) + 3;
                if (packetLength <= headerLength) {
                    NetBareLog.w("No enough ssl/tls packet length, packet: " + packetLength +
                            " header: " + headerLength);
                    // No enough data.
                    return PACKET_NOT_ENOUGH;
                }
            } else {
                // Not encrypted
                return PACKET_NOT_ENCRYPTED;
            }
        }
        // Decode SSL data.
        if (packetLength > buffer.remaining()) {
            NetBareLog.w("No enough ssl/tls packet length, packet: " + packetLength +
                    " actual: " + buffer.remaining());
            // Wait until the whole packet can be read.
            return PACKET_NOT_ENOUGH;
        }
        return PACKET_SSL;
    }
Verify a packet to see whether it is a valid SSL packet.

@param buffer Encrypted SSL packet.
@return Verification result, one of {@link #PACKET_NOT_ENOUGH}, {@link #PACKET_NOT_ENCRYPTED},
and {@link #PACKET_SSL}.
netbare-core/src/main/java/com/github/megatronking/netbare/ssl/SSLUtils.java