Example usage for io.netty.buffer ByteBufAllocator buffer

List of usage examples for io.netty.buffer ByteBufAllocator buffer

Introduction

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

Prototype

ByteBuf buffer(int initialCapacity);

Source Link

Document

Allocate a ByteBuf with the given initial capacity.

Usage

From source file:shi.cs.MqttEncoder.java

License:Apache License

private static ByteBuf encodeMessageWithOnlySingleByteFixedHeaderAndMessageId(ByteBufAllocator byteBufAllocator,
        MqttMessage message) {// w  w w. j a  v  a  2 s.  c  om
    MqttFixedHeader mqttFixedHeader = message.fixedHeader();
    MqttMessageIdVariableHeader variableHeader = (MqttMessageIdVariableHeader) message.variableHeader();
    int msgId = variableHeader.messageId();

    int variableHeaderBufferSize = 2; // variable part only has a message id
    int fixedHeaderBufferSize = 1 + getVariableLengthInt(variableHeaderBufferSize);
    ByteBuf buf = byteBufAllocator.buffer(fixedHeaderBufferSize + variableHeaderBufferSize);
    buf.writeByte(getFixedHeaderByte1(mqttFixedHeader));
    writeVariableLengthInt(buf, variableHeaderBufferSize);
    buf.writeShort(msgId);

    return buf;
}

From source file:shi.cs.MqttEncoder.java

License:Apache License

private static ByteBuf encodeMessageWithOnlySingleByteFixedHeader(ByteBufAllocator byteBufAllocator,
        MqttMessage message) {//from   w w w  .j  a  va  2s  .  co m
    MqttFixedHeader mqttFixedHeader = message.fixedHeader();
    ByteBuf buf = byteBufAllocator.buffer(2);
    buf.writeByte(getFixedHeaderByte1(mqttFixedHeader));
    buf.writeByte(0);

    return buf;
}

From source file:z.offheap.contrast.netty.ByteBufAllocatorBenchmark.java

License:Open Source License

public void forSize(ByteBufAllocator alloc, int size) {
    long s = System.nanoTime();
    for (int i = 0; i < COUNT; i++) {
        queue.add(alloc.buffer(size));
        queue.removeFirst().release();//from  w  w w . j  av  a  2  s  . c o  m
    }
    long t = System.nanoTime() - s;
    System.out.println("for size " + size + " and " + alloc.getClass().getSimpleName() + " cost: "
            + TimeUnit.NANOSECONDS.toMillis(t) + " millis");

    assertThat(queue.size(), is(0));
}

From source file:z.offheap.contrast.netty.ByteBufAllocatorBenchmark.java

License:Open Source License

public void forAllocator(ByteBufAllocator alloc, int size) {
    System.out.println(alloc.getClass().getSimpleName() + ": ");

    long s = System.nanoTime();
    for (int i = 0; i < COUNT; i++) {
        bufs[i] = (alloc.buffer(size));
    }//from ww w .j a  va2 s  . c om
    long t = System.nanoTime() - s;
    System.out.println(alloc.getClass().getSimpleName() + "[size:" + size + "]" + " allocate " + COUNT
            + " buffers cost: " + TimeUnit.NANOSECONDS.toMillis(t) + " millis");

    s = System.nanoTime();
    for (int i = 0; i < COUNT; i++) {
        bufs[i].release();
    }
    t = System.nanoTime() - s;
    System.out.println(alloc.getClass().getSimpleName() + "[size:" + size + "]" + " free " + COUNT
            + " buffers cost: " + TimeUnit.NANOSECONDS.toMillis(t) + " millis");

    assertThat(bufs[0].isReadable(), is(false));
}