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.yahoo.pulsar.client.impl.MessageImpl.java

License:Apache License

public MessageImpl(String msgId, Map<String, String> properties, byte[] payload) {
    this(msgId, properties, Unpooled.wrappedBuffer(payload));
}

From source file:com.yahoo.pulsar.common.api.DoubleByteBuf.java

License:Apache License

@Override
public DoubleByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
    return getBytes(index, Unpooled.wrappedBuffer(dst), dstIndex, length);
}

From source file:com.yahoo.pulsar.common.api.DoubleByteBuf.java

License:Apache License

@Override
public ByteBuffer nioBuffer(int index, int length) {
    ByteBuffer dst = ByteBuffer.allocate(length);
    ByteBuf b = Unpooled.wrappedBuffer(dst);
    b.writerIndex(0);/*from  w w w  .  j  a va 2s .c  om*/
    getBytes(index, b, length);
    return dst;
}

From source file:com.yahoo.pulsar.common.compression.CommandsTest.java

License:Apache License

@Test
public void testChecksumSendCommand() throws Exception {

    // test checksum in send command
    String producerName = "prod-name";
    int sequenceId = 0;
    ByteBuf data = Unpooled.buffer(1024);
    MessageMetadata messageMetadata = MessageMetadata.newBuilder().setPublishTime(System.currentTimeMillis())
            .setProducerName(producerName).setSequenceId(sequenceId).build();
    int expectedChecksum = computeChecksum(messageMetadata, data);
    ByteBuf clientCommand = Commands.newSend(1, 0, 1, ChecksumType.Crc32c, messageMetadata, data);
    clientCommand.retain();/*from   www .ja  v  a 2 s. com*/
    ByteBuffer inputBytes = clientCommand.nioBuffer();
    ByteBuf receivedBuf = Unpooled.wrappedBuffer(inputBytes);
    receivedBuf.skipBytes(4); //skip [total-size]
    int cmdSize = (int) receivedBuf.readUnsignedInt();
    receivedBuf.readerIndex(8 + cmdSize);
    int startMessagePos = receivedBuf.readerIndex();

    /*** 1. verify checksum and metadataParsing ***/
    boolean hasChecksum = Commands.hasChecksum(receivedBuf);
    int checksum = Commands.readChecksum(receivedBuf).intValue();

    // verify checksum is present
    assertTrue(hasChecksum);
    // verify checksum value
    assertEquals(expectedChecksum, checksum);
    MessageMetadata metadata = Commands.parseMessageMetadata(receivedBuf);
    // verify metadata parsing
    assertEquals(metadata.getProducerName(), producerName);

    /** 2. parseMessageMetadata should skip checksum if present **/
    receivedBuf.readerIndex(startMessagePos);
    metadata = Commands.parseMessageMetadata(receivedBuf);
    // verify metadata parsing
    assertEquals(metadata.getProducerName(), producerName);

}

From source file:com.yahoo.pulsar.common.compression.Crc32cChecksumTest.java

License:Apache License

@Test
public void testCrc32c() {
    ByteBuf payload = Unpooled.wrappedBuffer(inputBytes);
    int checksum = Crc32cChecksum.computeChecksum(payload);
    payload.release();/*from w w  w.  j  a va 2 s .c  o m*/
    assertEquals(expectedChecksum, checksum);
}

From source file:com.yahoo.pulsar.common.compression.Crc32cChecksumTest.java

License:Apache License

@Test
public void testCrc32cHardware() {
    if (HARDWARE_CRC32C_HASH == null) {
        return;/*from www.  j a  v  a  2 s  . c  om*/
    }

    ByteBuf payload = Unpooled.wrappedBuffer(inputBytes);

    // compute checksum using sse4.2 hw instruction
    int hw = HARDWARE_CRC32C_HASH.calculate(payload.array(), payload.arrayOffset() + payload.readerIndex(),
            payload.readableBytes());
    assertEquals(hw, expectedChecksum);
}

From source file:com.yahoo.pulsar.common.compression.Crc32cChecksumTest.java

License:Apache License

@Test
public void testCrc32cSoftware() {
    ByteBuf payload = Unpooled.wrappedBuffer(inputBytes);

    // compute checksum using sw algo
    int sw = SOFTWARE_CRC32C_HASH.calculate(payload.array(), payload.arrayOffset() + payload.readerIndex(),
            payload.readableBytes());/*from  ww w  . jav a  2s  .c  o m*/
    assertEquals(sw, expectedChecksum);
}

From source file:com.yahoo.pulsar.common.compression.Crc32cChecksumTest.java

License:Apache License

@Test
public void testCrc32cIncrementalUsingProvider() {

    final byte[] data = "data".getBytes();
    final byte[] doubleData = "datadata".getBytes();
    ByteBuf payload = Unpooled.wrappedBuffer(data);
    ByteBuf doublePayload = Unpooled.wrappedBuffer(doubleData);

    int expectedChecksum = Crc32cChecksum.computeChecksum(doublePayload);

    // (1) heap-memory
    int checksum = Crc32cChecksum.computeChecksum(payload);
    int incrementalChecksum = Crc32cChecksum.resumeChecksum(checksum, payload);
    assertEquals(expectedChecksum, incrementalChecksum);
    payload.release();//from   w ww  .  jav a  2 s.  c o  m
    doublePayload.release();

    // (2) direct-memory
    payload = ByteBufAllocator.DEFAULT.directBuffer(data.length);
    payload.writeBytes(data);
    checksum = Crc32cChecksum.computeChecksum(payload);
    incrementalChecksum = Crc32cChecksum.resumeChecksum(checksum, payload);
    assertEquals(expectedChecksum, incrementalChecksum);
    payload.release();

}

From source file:com.yahoo.pulsar.common.util.protobuf.ByteBufCodedInputStreamTest.java

License:Apache License

@Test
public void testByteBufCondedInputStreamTest() throws IOException {
    ByteBufCodedInputStream inputStream = ByteBufCodedInputStream
            .get(Unpooled.wrappedBuffer("Test-Message".getBytes()));
    assertTrue(inputStream.skipField(WireFormat.WIRETYPE_VARINT));
    assertTrue(inputStream.skipField(WireFormat.WIRETYPE_FIXED64));
    assertFalse(inputStream.skipField(WireFormat.WIRETYPE_END_GROUP));
    inputStream = ByteBufCodedInputStream.get(Unpooled.wrappedBuffer("1000".getBytes()));
    assertTrue(inputStream.skipField(WireFormat.WIRETYPE_FIXED32));
    assertTrue(inputStream.skipField(WireFormat.WIRETYPE_START_GROUP));

    try {/*  www .  j ava  2  s .  co m*/
        assertTrue(inputStream.skipField(-1));
        fail("Should not happend");
    } catch (Exception e) {
        // pass
    }
    try {
        assertTrue(inputStream.skipField(WireFormat.WIRETYPE_LENGTH_DELIMITED));
        fail("Should not happend");
    } catch (Exception e) {
        // pass
    }

    try {
        inputStream.skipRawBytes(-1);
        fail("Should not happend");
    } catch (InvalidProtocolBufferException e) {
        // pass
    }

    try {
        inputStream.skipRawBytes(10);
        fail("Should not happend");
    } catch (InvalidProtocolBufferException e) {
        // pass
    }

}

From source file:com.yeetor.server.HttpServer.java

License:Open Source License

@HttpRouter(uri = "/shot")
public void shot(ChannelHandlerContext ctx, HttpRequest request, HttpResponse response) {
    String uri = request.uri();//from   w w  w . j  ava  2 s  .c om
    String[] args = uri.split("/");
    if (args.length < 1 || args[1].length() == 0) {
        writeErrorHttpResponse(ctx, request, response, HttpResponseStatus.NOT_FOUND);
        return;
    }

    String serialNumber = args[2];
    long startTime = System.currentTimeMillis();
    Minicap cap = new Minicap(serialNumber);
    byte[] data = cap.takeScreenShot();
    long endTime = System.currentTimeMillis();
    logger.info("ScreenShot used" + (endTime - startTime) + "ms");
    response.headers().set(CONTENT_TYPE, "image/jpeg");
    HttpContent content = new DefaultHttpContent(Unpooled.wrappedBuffer(data));
    response.headers().set(CONTENT_LENGTH, content.content().readableBytes());
    writeResponse(ctx, request, response, content);
}