@Override
    protected void intercept(@NonNull HttpResponseChain chain, @NonNull ByteBuffer buffer,
                             int index) throws IOException {
        if (index == 0) {
            HttpResponse response = chain.response();
            if (mLog == null) {
                mLog = new NetBareXLog(response.protocol(), response.ip(), response.port());
            }
            // HTTP2 is forces to use SSL connection.
            if (response.isHttps()) {
                if (buffer.hasRemaining() && Bytes.indexOf(buffer.array(),
                        Http2.CONNECTION_PREFACE) == buffer.position()) {
                    mLog.i("Receive a connection preface from remote server.");
                    response.session().protocol = HttpProtocol.HTTP_2;
                    if (buffer.remaining() == Http2.CONNECTION_PREFACE.length) {
                        // Skip preface frame data.
                        mCallback.onResponse(response, buffer);
                        return;
                    } else {
                        ByteBuffer prefaceBuffer = ByteBuffer.allocate(Http2.CONNECTION_PREFACE.length);
                        prefaceBuffer.put(Http2.CONNECTION_PREFACE);
                        prefaceBuffer.flip();
                        mCallback.onResponse(response, prefaceBuffer);
                        // The remaining data continues.
                        buffer.position(buffer.position() + Http2.CONNECTION_PREFACE.length);
                    }
                }
            }
        }
        if (buffer.hasRemaining()) {
            chain.process(buffer);
        }
    }
Verifies the HTTP packet and determines whether it is a HTTP2 protocol packets.

@author Megatron King
@since 2019/1/5 14:02
netbare-core/src/main/java/com/github/megatronking/netbare/http/Http2SniffInterceptor.java