Example usage for io.netty.buffer Unpooled wrappedBuffer

List of usage examples for io.netty.buffer Unpooled wrappedBuffer

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled wrappedBuffer.

Prototype

public static ByteBuf wrappedBuffer(ByteBuffer... buffers) 

Source Link

Document

Creates a new big-endian composite buffer which wraps the slices of the specified NIO buffers without copying them.

Usage

From source file:com.whizzosoftware.wzwave.frame.transaction.SendDataTransactionTest.java

License:Open Source License

@Test
public void testApplicationCommandReceivedBeforeSendDataCallback() {
    byte[] b = new byte[] { 0x01, 0x0D, 0x00, 0x13, 0x03, 0x06, 0x60, 0x0D, 0x00, 0x01, 0x20, 0x02, 0x05, 0x1B,
            (byte) 0xB4 };
    SendData startFrame = new SendData(Unpooled.wrappedBuffer(b));
    SendDataTransaction t = new SendDataTransaction(startFrame, true, true);
    assertFalse(t.isComplete());/*ww  w  .  j ava 2 s  .  co m*/

    assertTrue(t.addFrame(new ACK()));
    assertFalse(t.isComplete());
    assertFalse(t.hasError());

    b = new byte[] { 0x01, 0x04, 0x01, 0x13, 0x01, (byte) 0xE8 };
    assertTrue(t.addFrame(new SendData(Unpooled.wrappedBuffer(b))));
    assertFalse(t.isComplete());
    assertFalse(t.hasError());

    assertFalse(t.addFrame(new ApplicationCommand(Unpooled.wrappedBuffer(new byte[] { 0x01, 0x0D, 0x00, 0x04,
            0x00, 0x03, 0x07, 0x60, 0x0D, 0x01, 0x00, 0x20, 0x03, (byte) 0xFF, 0x42 }))));
    assertFalse(t.isComplete());
    assertFalse(t.hasError());

    b = new byte[] { 0x01, 0x05, 0x00, 0x13, 0x1B, 0x00, (byte) 0xF3 };
    assertTrue(t.addFrame(new SendData(Unpooled.wrappedBuffer(b))));
    assertTrue(t.isComplete());
    assertFalse(t.hasError());

}

From source file:com.whizzosoftware.wzwave.frame.transaction.SendDataTransactionTest.java

License:Open Source License

@Test
public void testTransactionWithListeningNodeNoACKCallback() {
    SendData startFrame = new SendData(Unpooled.wrappedBuffer(
            new byte[] { 0x01, 0x09, 0x00, 0x13, 0x06, 0x02, 0x00, 0x00, 0x25, 0x0a, (byte) 0xce }));
    SendDataTransaction t = new SendDataTransaction(startFrame, true, true);
    assertFalse(t.isComplete());/*from w w  w .j a va 2 s  . c o  m*/
    assertFalse(t.hasError());

    assertTrue(t.addFrame(new ACK()));
    assertFalse(t.isComplete());
    assertFalse(t.hasError());

    t.addFrame(new SendData(Unpooled.wrappedBuffer(new byte[] { 0x01, 0x04, 0x01, 0x13, 0x01, (byte) 0xe8 })));
    assertFalse(t.isComplete());
    assertFalse(t.hasError());

    t.addFrame(new SendData(
            Unpooled.wrappedBuffer(new byte[] { 0x01, 0x05, 0x00, 0x13, 0x0a, 0x01, (byte) 0xe2 })));
    assertTrue(t.isComplete());
    assertTrue(t.hasError());
    assertTrue(t.shouldRetry());
}

From source file:com.whizzosoftware.wzwave.frame.transaction.SendDataTransactionTest.java

License:Open Source License

@Test
public void testTransactionWithNonListeningNodeNoACKCallback() {
    SendData startFrame = new SendData(Unpooled.wrappedBuffer(
            new byte[] { 0x01, 0x09, 0x00, 0x13, 0x06, 0x02, 0x00, 0x00, 0x25, 0x0a, (byte) 0xce }));
    SendDataTransaction t = new SendDataTransaction(startFrame, false, true);
    assertFalse(t.isComplete());/*from  www.j  a  v a2s.c o  m*/
    assertFalse(t.hasError());

    assertTrue(t.addFrame(new ACK()));
    assertFalse(t.isComplete());
    assertFalse(t.hasError());

    t.addFrame(new SendData(Unpooled.wrappedBuffer(new byte[] { 0x01, 0x04, 0x01, 0x13, 0x01, (byte) 0xe8 })));
    assertFalse(t.isComplete());
    assertFalse(t.hasError());

    t.addFrame(new SendData(
            Unpooled.wrappedBuffer(new byte[] { 0x01, 0x05, 0x00, 0x13, 0x0a, 0x01, (byte) 0xe2 })));
    assertTrue(t.isComplete());
    assertTrue(t.hasError());
    assertFalse(t.shouldRetry());
}

From source file:com.whizzosoftware.wzwave.frame.transaction.SendDataTransactionTest.java

License:Open Source License

@Test
public void testTransactionWithFailureCallback() {
    SendData startFrame = new SendData(Unpooled.wrappedBuffer(
            new byte[] { 0x01, 0x09, 0x00, 0x13, 0x06, 0x02, 0x00, 0x00, 0x25, 0x0a, (byte) 0xce }));
    SendDataTransaction t = new SendDataTransaction(startFrame, true, true);
    assertFalse(t.isComplete());//from   w w  w.  j av  a2 s.c  o m
    assertFalse(t.hasError());

    assertTrue(t.addFrame(new ACK()));
    assertFalse(t.isComplete());
    assertFalse(t.hasError());

    t.addFrame(new SendData(Unpooled.wrappedBuffer(new byte[] { 0x01, 0x04, 0x01, 0x13, 0x01, (byte) 0xe8 })));
    assertFalse(t.isComplete());
    assertFalse(t.hasError());

    t.addFrame(new SendData(
            Unpooled.wrappedBuffer(new byte[] { 0x01, 0x05, 0x00, 0x13, 0x0a, 0x02, (byte) 0xe2 })));
    assertTrue(t.isComplete());
    assertTrue(t.hasError());
    assertTrue(t.shouldRetry());
}

From source file:com.whizzosoftware.wzwave.frame.VersionTest.java

License:Open Source License

@Test
public void testResponseConstructor() {
    byte[] b = new byte[] { 0x01, 0x10, 0x01, 0x15, 0x5a, 0x2d, 0x57, 0x61, 0x76, 0x65, 0x20, 0x32, 0x2e, 0x37,
            0x38, 0x00, 0x01, (byte) 0x9b };
    ByteBuf buffer = Unpooled.wrappedBuffer(b);
    Version v = new Version(buffer);
    assertEquals(1, buffer.readableBytes());
    assertEquals("Z-Wave 2.78", v.getLibraryVersion());
    assertEquals(0x01, v.getLibraryType());
}

From source file:com.yahoo.pulsar.broker.service.PersistentTopicTest.java

License:Apache License

@Test
public void testPublishMessage() throws Exception {

    PersistentTopic topic = new PersistentTopic(successTopicName, ledgerMock, brokerService);
    /*/*  ww w.jav a  2  s.co m*/
     * MessageMetadata.Builder messageMetadata = MessageMetadata.newBuilder();
     * messageMetadata.setPublishTime(System.currentTimeMillis()); messageMetadata.setProducerName("producer-name");
     * messageMetadata.setSequenceId(1);
     */
    ByteBuf payload = Unpooled.wrappedBuffer("content".getBytes());

    final CountDownLatch latch = new CountDownLatch(1);

    topic.publishMessage(payload, (exception, ledgerId, entryId) -> {
        latch.countDown();
    });

    assertTrue(latch.await(1, TimeUnit.SECONDS));
}

From source file:com.yahoo.pulsar.broker.service.PersistentTopicTest.java

License:Apache License

@Test
public void testPublishMessageMLFailure() throws Exception {
    final String successTopicName = "persistent://prop/use/ns-abc/successTopic";

    final ManagedLedger ledgerMock = mock(ManagedLedger.class);
    doReturn(new ArrayList<Object>()).when(ledgerMock).getCursors();

    PersistentTopic topic = new PersistentTopic(successTopicName, ledgerMock, brokerService);

    MessageMetadata.Builder messageMetadata = MessageMetadata.newBuilder();
    messageMetadata.setPublishTime(System.currentTimeMillis());
    messageMetadata.setProducerName("prod-name");
    messageMetadata.setSequenceId(1);// w w w. j  a  v  a2  s .  c om

    ByteBuf payload = Unpooled.wrappedBuffer("content".getBytes());
    final CountDownLatch latch = new CountDownLatch(1);

    // override asyncAddEntry callback to return error
    doAnswer(new Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            ((AddEntryCallback) invocationOnMock.getArguments()[1]).addFailed(
                    new ManagedLedgerException("Managed ledger failure"), invocationOnMock.getArguments()[2]);
            return null;
        }
    }).when(ledgerMock).asyncAddEntry(any(ByteBuf.class), any(AddEntryCallback.class), anyObject());

    topic.publishMessage(payload, (exception, ledgerId, entryId) -> {
        if (exception == null) {
            fail("publish should have failed");
        } else {
            latch.countDown();
        }
    });

    assertTrue(latch.await(1, TimeUnit.SECONDS));
}

From source file:com.yahoo.pulsar.checksum.utils.Crc32cChecksumTest.java

License:Apache License

@Test
public void testCrc32c() {

    ByteBuf payload = Unpooled.wrappedBuffer(ByteBuffer.wrap(inputBytes, 0, inputBytes.length));
    int checksum = Crc32cChecksum.computeChecksum(payload);
    payload.release();/*  ww w  .j  ava 2 s  .  co m*/
    Assert.assertEquals(expectedChecksum, checksum);
}

From source file:com.yahoo.pulsar.checksum.utils.Crc32cChecksumTest.java

License:Apache License

@Test
public void testCrc32cDirectMemory() {
    ByteBuffer bytes = ByteBuffer.allocateDirect(4);
    bytes.put(inputBytes);/* w ww.  ja va  2s .  c om*/
    bytes.flip();
    ByteBuf payload = Unpooled.wrappedBuffer(bytes);
    int checksum = Crc32cChecksum.computeChecksum(payload);
    payload.release();
    Assert.assertEquals(expectedChecksum, checksum);
}

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

License:Apache License

static MessageImpl create(MessageMetadata.Builder msgMetadataBuilder, ByteBuffer payload) {
    MessageImpl msg = RECYCLER.get();// ww w . j a  va2s  .co m
    msg.msgMetadataBuilder = msgMetadataBuilder;
    msg.messageId = null;
    msg.cnx = null;
    msg.payload = Unpooled.wrappedBuffer(payload);
    msg.properties = Collections.emptyMap();
    return msg;
}