Example usage for io.netty.buffer ByteBuf writerIndex

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

Introduction

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

Prototype

public abstract int writerIndex();

Source Link

Document

Returns the writerIndex of this buffer.

Usage

From source file:org.opendaylight.protocol.pcep.spi.TlvUtil.java

License:Open Source License

public static void formatTlv(final int type, final ByteBuf body, final ByteBuf out) {
    out.writeShort(type);//from www  . j ava2  s  .co  m
    out.writeShort(body.writerIndex());
    out.writeBytes(body);
    out.writeZero(getPadding(HEADER_SIZE + body.writerIndex(), PADDED_TO));
}

From source file:org.opendaylight.protocol.pcep.spi.XROSubobjectUtil.java

License:Open Source License

public static void formatSubobject(final int type, final Boolean mandatory, final ByteBuf body,
        final ByteBuf buffer) {
    if (mandatory == null) {
        buffer.writeByte(type);// w  w  w . j  a va  2 s  .c  om
    } else {
        buffer.writeByte(type | (mandatory ? 1 << MANDATORY_BIT : 0));
    }
    buffer.writeByte(body.writerIndex() + HEADER_SIZE);
    buffer.writeBytes(body);
}

From source file:org.opendaylight.protocol.util.ByteArrayTest.java

License:Open Source License

@Test
public void testReadBytes() {
    final ByteBuf buffer = Unpooled.copiedBuffer(this.before);
    buffer.readerIndex(1);//w ww  .  j  a v a 2 s. c om
    assertArrayEquals(new byte[] { 28, 4, 6 }, ByteArray.readBytes(buffer, 3));
    assertEquals(4, buffer.readerIndex());

    assertArrayEquals(new byte[] { 9, 10 }, ByteArray.readAllBytes(buffer));
    assertEquals(buffer.readerIndex(), buffer.writerIndex());
}

From source file:org.opendaylight.protocol.util.ByteArrayTest.java

License:Open Source License

@Test
public void testGetBytes() {
    final ByteBuf buffer = Unpooled.copiedBuffer(this.before);
    buffer.readerIndex(1);/*from   w w  w .  ja  v  a2  s . c o  m*/
    assertArrayEquals(new byte[] { 28, 4, 6 }, ByteArray.getBytes(buffer, 3));
    assertEquals(1, buffer.readerIndex());

    assertArrayEquals(new byte[] { 28, 4, 6, 9, 10 }, ByteArray.getAllBytes(buffer));
    assertNotSame(buffer.readerIndex(), buffer.writerIndex());
}

From source file:org.proton.plug.context.AbstractProtonContextSender.java

License:Apache License

protected int performSend(ProtonJMessage serverMessage, Object context) {
    if (!creditsSemaphore.tryAcquire()) {
        try {//from www .j  av  a 2s  .c  o m
            creditsSemaphore.acquire();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            // nothing to be done here.. we just keep going
            throw new IllegalStateException(e.getMessage(), e);
        }
    }

    //presettle means we can ack the message on the dealer side before we send it, i.e. for browsers
    boolean preSettle = sender.getRemoteSenderSettleMode() == SenderSettleMode.SETTLED;

    //we only need a tag if we are going to ack later
    byte[] tag = preSettle ? new byte[0] : protonSession.getTag();

    ByteBuf nettyBuffer = PooledByteBufAllocator.DEFAULT.heapBuffer(1024);
    try {
        serverMessage.encode(new NettyWritable(nettyBuffer));

        int size = nettyBuffer.writerIndex();

        synchronized (connection.getLock()) {
            final Delivery delivery;
            delivery = sender.delivery(tag, 0, tag.length);
            delivery.setContext(context);

            // this will avoid a copy.. patch provided by Norman using buffer.array()
            sender.send(nettyBuffer.array(), nettyBuffer.arrayOffset() + nettyBuffer.readerIndex(),
                    nettyBuffer.readableBytes());

            if (preSettle) {
                delivery.settle();
            } else {
                sender.advance();
            }
        }

        connection.flush();

        return size;
    } finally {
        nettyBuffer.release();
    }
}

From source file:org.proton.plug.test.invm.ProtonINVMSPI.java

License:Apache License

@Override
public void onTransport(final ByteBuf bytes, final AMQPConnectionContext connection) {
    if (DebugInfo.debug) {
        ByteUtil.debugFrame("InVM->", bytes);
    }//from  w  ww  .j  a v a  2  s .  co m
    final int size = bytes.writerIndex();

    bytes.retain();
    mainExecutor.execute(new Runnable() {
        public void run() {
            try {
                if (DebugInfo.debug) {
                    ByteUtil.debugFrame("InVMDone->", bytes);
                }
                serverConnection.inputBuffer(bytes);
                try {
                    connection.outputDone(size);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } finally {
                bytes.release();
            }
        }
    });
}

From source file:org.proton.plug.test.minimalclient.AMQPClientSPI.java

License:Apache License

@Override
public void onTransport(final ByteBuf bytes, final AMQPConnectionContext connection) {
    if (DebugInfo.debug) {
        ByteUtil.debugFrame("Bytes leaving client", bytes);
    }//from  ww w  .j a va 2  s  . c om

    final int bufferSize = bytes.writerIndex();

    latch.countUp();

    channel.writeAndFlush(bytes).addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            //
            //            connection.outputDone(bufferSize);
            latch.countDown();
        }
    });

    if (connection.isSyncOnFlush()) {
        try {
            if (!latch.await(5, TimeUnit.SECONDS)) {
                // TODO logs
                System.err.println("Flush took longer than 5 seconds!!!");
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    connection.outputDone(bufferSize);

}

From source file:org.proton.plug.test.minimalserver.MinimalConnectionSPI.java

License:Apache License

@Override
public void onTransport(final ByteBuf bytes, final AMQPConnectionContext connection) {
    final int bufferSize = bytes.writerIndex();

    if (DebugInfo.debug) {
        // some debug
        byte[] frame = new byte[bytes.writerIndex()];
        int readerOriginalPos = bytes.readerIndex();

        bytes.getBytes(0, frame);//from   w ww  .jav  a  2 s .  c  o m

        try {
            System.err.println(
                    "Buffer Outgoing: " + "\n" + ByteUtil.formatGroup(ByteUtil.bytesToHex(frame), 4, 16));
        } catch (Exception e) {
            e.printStackTrace();
        }

        bytes.readerIndex(readerOriginalPos);
    }

    latch.countUp();
    // ^^ debug

    channel.writeAndFlush(bytes).addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            latch.countDown();

            //   https://issues.apache.org/jira/browse/PROTON-645
            //            connection.outputDone(bufferSize);
            //            if (connection.capacity() > 0)
            //            {
            //               channel.read();
            //            }
        }
    });

    channel.flush();

    if (connection.isSyncOnFlush()) {
        try {
            if (!latch.await(5, TimeUnit.SECONDS)) {
                // TODO logs
                System.err.println("Flush took longer than 5 seconds!!!");
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
    connection.outputDone(bufferSize);

    //      if (connection.capacity() > 0)
    //      {
    //         channel.read();
    //      }
}

From source file:org.proton.plug.util.ByteUtil.java

License:Apache License

public static void debugFrame(String message, ByteBuf byteIn) {
    int location = byteIn.readerIndex();
    // debugging//  w w w  . ja v a2s  .  c o  m
    byte[] frame = new byte[byteIn.writerIndex()];
    byteIn.readBytes(frame);

    try {
        System.out.println(message + "\n" + ByteUtil.formatGroup(ByteUtil.bytesToHex(frame), 8, 16));
    } catch (Exception e) {
        e.printStackTrace();
    }

    byteIn.readerIndex(location);
}

From source file:org.pumpkindb.Closure.java

License:Mozilla Public License

@Override
public void encode(ByteBuf buffer) {
    ByteBuf buf = Unpooled.buffer();
    encodables.forEach(encodable -> encodable.encode(buf));

    int index = buf.writerIndex();

    buf.capacity(index);// www  . ja  va  2s . c  o m

    new Data(buf.array()).encode(buffer);
}