Example usage for io.netty.buffer ByteBuf unwrap

List of usage examples for io.netty.buffer ByteBuf unwrap

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf unwrap.

Prototype

public abstract ByteBuf unwrap();

Source Link

Document

Return the underlying buffer instance if this buffer is a wrapper of another buffer.

Usage

From source file:com.yahoo.pulsar.client.impl.ProducerImpl.java

License:Apache License

/**
 * Casts input cmd to {@link DoubleByteBuf}
 * //from w w w . ja v a  2s.com
 * Incase if leak-detection level is enabled: pulsar instruments {@link DoubleByteBuf} into LeakAwareByteBuf (type of {@link io.netty.buffer.WrappedByteBuf})
 * So, this method casts input cmd to {@link DoubleByteBuf} else retrieves it from LeakAwareByteBuf.
 * 
 * @param cmd
 * @return DoubleByteBuf or null in case failed to cast input {@link ByteBuf}
 */
private DoubleByteBuf getDoubleByteBuf(ByteBuf cmd) {
    DoubleByteBuf msg = null;
    if (cmd instanceof DoubleByteBuf) {
        msg = (DoubleByteBuf) cmd;
    } else {
        try {
            msg = (DoubleByteBuf) cmd.unwrap();
        } catch (Exception e) {
            log.error("[{}] Failed while casting {} into DoubleByteBuf", producerName, cmd.getClass().getName(),
                    e);
        }
    }
    return msg;
}

From source file:io.vertx.core.datagram.DatagramTest.java

License:Open Source License

@Test
public void testSendReceive() throws Exception {
    waitFor(2);/*from   w  w  w .j  a  v a 2s  .  c o m*/
    Buffer expected = TestUtils.randomBuffer(128);
    CountDownLatch latch = new CountDownLatch(1);
    Context serverContext = vertx.getOrCreateContext();
    serverContext.runOnContext(v -> {
        peer2 = vertx.createDatagramSocket(new DatagramSocketOptions());
        peer2.exceptionHandler(t -> fail(t.getMessage()));
        peer2.handler(packet -> {
            assertSame(serverContext, Vertx.currentContext());
            assertFalse(Thread.holdsLock(peer2));
            Buffer data = packet.data();
            ByteBuf buff = data.getByteBuf();
            while (buff != buff.unwrap() && buff.unwrap() != null) {
                buff = buff.unwrap();
            }
            assertTrue("Was expecting an unpooled buffer instead of " + buff.getClass().getSimpleName(),
                    buff.getClass().getSimpleName().contains("Unpooled"));
            assertEquals(expected, data);
            complete();
        });
        peer2.listen(1234, "127.0.0.1", onSuccess(so -> latch.countDown()));
    });
    awaitLatch(latch);
    Context clientContext = vertx.getOrCreateContext();
    clientContext.runOnContext(v -> {
        peer1 = vertx.createDatagramSocket(new DatagramSocketOptions());
        peer1.send(expected, 1234, "127.0.0.1", onSuccess(s -> {
            assertSame(clientContext, Vertx.currentContext());
            assertFalse(Thread.holdsLock(peer1));
            complete();
        }));
    });
    await();
}

From source file:io.vertx.test.core.DatagramTest.java

License:Open Source License

@Test
public void testSendReceive() {
    peer1 = vertx.createDatagramSocket(new DatagramSocketOptions());
    peer2 = vertx.createDatagramSocket(new DatagramSocketOptions());
    peer2.exceptionHandler(t -> fail(t.getMessage()));
    peer2.listen(1234, "127.0.0.1", ar -> {
        assertTrue(ar.succeeded());//from   w w w .j  av a  2  s. c  o m
        Buffer buffer = TestUtils.randomBuffer(128);
        peer2.handler(packet -> {
            Buffer data = packet.data();
            ByteBuf buff = data.getByteBuf();
            while (buff != buff.unwrap() && buff.unwrap() != null) {
                buff = buff.unwrap();
            }
            assertTrue("Was expecting an unpooled buffer instead of " + buff.getClass().getSimpleName(),
                    buff.getClass().getSimpleName().contains("Unpooled"));
            assertEquals(buffer, data);
            testComplete();
        });
        peer1.send(buffer, 1234, "127.0.0.1", ar2 -> assertTrue(ar2.succeeded()));
    });
    await();
}

From source file:org.apache.activemq.artemis.core.buffers.impl.ChannelBufferWrapper.java

License:Apache License

public static ByteBuf unwrap(ByteBuf buffer) {
    ByteBuf parent;/*from   w w  w.j a  v a 2s.  c o  m*/
    while ((parent = buffer.unwrap()) != null && parent != buffer) // this last part is just in case the semantic
    { // ever changes where unwrap is returning itself
        buffer = parent;
    }

    return buffer;
}