Example usage for io.netty.buffer ByteBufUtil equals

List of usage examples for io.netty.buffer ByteBufUtil equals

Introduction

In this page you can find the example usage for io.netty.buffer ByteBufUtil equals.

Prototype

public static boolean equals(ByteBuf a, int aStartIndex, ByteBuf b, int bStartIndex, int length) 

Source Link

Document

Returns true if and only if the two specified buffers are identical to each other for length bytes starting at aStartIndex index for the a buffer and bStartIndex index for the b buffer.

Usage

From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyFrontendHandler.java

License:Apache License

private boolean readClientPrefaceString(ByteBuf in) throws Http2Exception {
    ByteBuf clientPrefaceString = Http2CodecUtil.connectionPrefaceBuf();
    int prefaceRemaining = clientPrefaceString.readableBytes();
    int bytesRead = min(in.readableBytes(), prefaceRemaining);

    // If the input so far doesn't match the preface, break the connection.
    if (bytesRead == 0 || !ByteBufUtil.equals(in, in.readerIndex(), clientPrefaceString,
            clientPrefaceString.readerIndex(), bytesRead)) {
        String receivedBytes = hexDump(in, in.readerIndex(),
                min(in.readableBytes(), clientPrefaceString.readableBytes()));
        throw connectionError(PROTOCOL_ERROR,
                "HTTP/2 client preface string missing or corrupt. " + "Hex dump for received bytes: %s",
                receivedBytes);/*from w  w w.j a  v  a2s .c o  m*/
    }
    in.skipBytes(bytesRead);
    clientPrefaceString.skipBytes(bytesRead);

    if (!clientPrefaceString.isReadable()) {
        // Entire preface has been read.
        clientPrefaceString.release();
        return true;
    }
    return false;
}

From source file:org.apache.hadoop.hdfs.server.datanode.web.PortUnificationServerHandler.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (in.readableBytes() < MAGIC_HEADER_LENGTH) {
        return;/*  w  ww  .  j  a va  2 s. com*/
    }
    if (ByteBufUtil.equals(in, 0, HTTP2_CLIENT_CONNECTION_PREFACE, 0, MAGIC_HEADER_LENGTH)) {
        configureHttp2(ctx);
    } else {
        configureHttp1(ctx);
    }
    ctx.pipeline().remove(this);
}

From source file:org.apache.qpid.jms.transports.netty.NettyWsTransportTest.java

License:Apache License

@Test(timeout = 20000)
public void testConnectionReceivesFragmentedData() throws Exception {
    final int FRAME_SIZE = 5317;

    ByteBuf sendBuffer = Unpooled.buffer(FRAME_SIZE);
    for (int i = 0; i < FRAME_SIZE; ++i) {
        sendBuffer.writeByte('A' + (i % 10));
    }//  w  ww  .j av a2s. com

    try (NettyEchoServer server = createEchoServer(createServerOptions())) {
        server.setMaxFrameSize(FRAME_SIZE);
        // Server should fragment the data as it goes through
        server.setFragmentWrites(true);
        server.start();

        int port = server.getServerPort();
        URI serverLocation = new URI("tcp://localhost:" + port);

        List<Transport> transports = new ArrayList<Transport>();

        TransportOptions createClientOptions = createClientOptions();
        createClientOptions.setTraceBytes(true);

        NettyTransportListener wsListener = new NettyTransportListener(true);

        Transport transport = createTransport(serverLocation, wsListener, createClientOptions);
        try {
            transport.setMaxFrameSize(FRAME_SIZE);
            transport.connect(null);
            transports.add(transport);
            transport.send(sendBuffer.copy());
        } catch (Exception e) {
            fail("Should have connected to the server at " + serverLocation + " but got exception: " + e);
        }

        assertTrue(Wait.waitFor(new Wait.Condition() {
            @Override
            public boolean isSatisfied() throws Exception {
                LOG.debug("Checking completion: read {} expecting {}", bytesRead.get(), FRAME_SIZE);
                return bytesRead.get() == FRAME_SIZE || !transport.isConnected();
            }
        }, 10000, 50));

        assertTrue("Connection failed while receiving.", transport.isConnected());

        transport.close();

        assertEquals("Expected 2 data packets due to seperate websocket frames", 2, data.size());

        ByteBuf receivedBuffer = Unpooled.buffer(FRAME_SIZE);
        for (ByteBuf buf : data) {
            buf.readBytes(receivedBuffer, buf.readableBytes());
        }

        assertEquals("Unexpected data length", FRAME_SIZE, receivedBuffer.readableBytes());
        assertTrue("Unexpected data", ByteBufUtil.equals(sendBuffer, 0, receivedBuffer, 0, FRAME_SIZE));
    } finally {
        for (ByteBuf buf : data) {
            buf.release();
        }
    }

    assertTrue(exceptions.isEmpty());
}