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:DescriptorTester.java

License:Open Source License

public void sender(ByteBuf buf) {

    DatagramSocket socket = null;
    try {// ww  w.j  a  v  a2 s  . c  o  m
        socket = new DatagramSocket();
    } catch (SocketException e) {
        e.printStackTrace();
    }
    InetAddress address = null;
    try {
        address = InetAddress.getLoopbackAddress();
        System.out.println(address);
    } catch (Exception e) {
        e.printStackTrace();
    }

    {
        System.out.printf("Capacity of ByteBuf  %d", buf.writableBytes());
        System.out.printf("Writer Index %d", buf.writerIndex());
        int packet_size = buf.writerIndex();
        System.out.printf("packetsize %d", packet_size);
        byte[] array = buf.array();
        DatagramPacket packet = new DatagramPacket(array, packet_size, address, 5246);
        try {
            socket.send(packet);
            System.out.printf("\nSending Capwap Message  ");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

From source file:TestDescriptor.java

License:Open Source License

public void sender(ByteBuf buf) {

    DatagramSocket socket = null;
    try {/*from   ww w  . j  a  v a 2 s.  c  o m*/
        socket = new DatagramSocket();
    } catch (SocketException e) {
        e.printStackTrace();
    }
    InetAddress address = null;
    try {
        address = InetAddress.getLoopbackAddress();
        System.out.println(address);
    } catch (Exception e) {
        e.printStackTrace();
    }

    {
        System.out.printf("Capacity of ByteBuf  %d", buf.writableBytes());
        System.out.printf("Writer Index %d", buf.writerIndex());
        int packet_size = buf.writerIndex();
        System.out.printf("packetsize %d", packet_size);
        byte[] array = buf.array();
        DatagramPacket packet = new DatagramPacket(array, packet_size, address, 5246);
        try {
            socket.send(packet);
            System.out.printf("\nSending Capwap Message  ");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:WtpTestClient.java

License:Open Source License

public void sender(ByteBuf buf) {

    DatagramSocket socket = null;
    try {//from  w  w  w. ja  v  a  2  s .  c  o m
        socket = new DatagramSocket();
    } catch (SocketException e) {
        e.printStackTrace();
    }
    InetAddress address = null;
    try {
        address = InetAddress.getLoopbackAddress();
        System.out.println(address);
    } catch (Exception e) {
        e.printStackTrace();
    }

    {
        int packet_size = buf.writerIndex();
        byte[] array = buf.array();
        int port = 5246;
        DatagramPacket packet = new DatagramPacket(array, packet_size, address, port);
        try {
            socket.send(packet);
            System.out.printf("\nSending Capwap Message of size->%d to %s:%d ", packet_size, address.toString(),
                    port);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

From source file:UdpClient.java

License:Open Source License

public static void main(String args[]) {

    byte[] buf = new byte[256];
    DatagramSocket socket = null;
    try {//  w ww  .j a va  2  s.c  o  m
        socket = new DatagramSocket();
    } catch (SocketException e) {
        e.printStackTrace();
    }
    InetAddress address = null;
    try {
        address = InetAddress.getLoopbackAddress();
        System.out.println(address);
    } catch (Exception e) {
        e.printStackTrace();
    }
    buf = "hello".getBytes();
    ByteBuf nett_buf = Unpooled.buffer();

    InetSocketAddress socketAddress = new InetSocketAddress(address, 5246);
    Scanner sc = new Scanner(System.in);
    {
        System.out.println("\nEnter Capwap Message ID");
        //int longIn = 4294967294;
        int longIn = 0XFFFFFFFE;
        nett_buf.writeInt(longIn);
        buf = nett_buf.array();

        DatagramPacket packet = new DatagramPacket(buf, nett_buf.writerIndex(), address, 5246);
        try {
            socket.send(packet);
            System.out.printf("\nSending Capwap Message %x ", longIn);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:CapwapMsgSender.java

License:Open Source License

public static void main(String args[]) {

    DatagramSocket socket = null;
    try {/*from ww  w  .j  a v  a2s  . com*/
        socket = new DatagramSocket();
    } catch (SocketException e) {
        e.printStackTrace();
    }
    InetAddress address = null;
    try {
        address = InetAddress.getLoopbackAddress();
        System.out.println(address);
    } catch (Exception e) {
        e.printStackTrace();
    }
    //ByteBuf nett_buf = Unpooled.buffer();
    ByteBuf buf = null;

    Scanner sc = new Scanner(System.in);
    while (true) {
        System.out.println("\nEnter Capwap Message ID");
        int capwapMsg = sc.nextInt();
        System.out.printf("Capacity of ByteBuf  %d", buf.writableBytes());
        System.out.printf("Writer Index %d", buf.writerIndex());
        int packet_size = buf.writerIndex();
        byte[] array = buf.array();
        DatagramPacket packet = new DatagramPacket(array, packet_size, address, 5246);
        try {
            socket.send(packet);
            System.out.printf("\nSending Capwap Message %s ", ODLCapwapConsts.msgTypetoString(capwapMsg));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:alluxio.worker.block.UnderFileSystemBlockReader.java

License:Apache License

/**
 * This interface is supposed to be used for sequence block reads.
 *
 * @param buf the byte buffer//from   ww  w  . ja  va2s  .c o m
 * @return the number of bytes read, -1 if it reaches EOF and none was read
 * @throws IOException if any I/O errors occur when reading the block
 */
@Override
public int transferTo(ByteBuf buf) throws IOException {
    Preconditions.checkState(!mClosed);
    if (mUnderFileSystemInputStream == null) {
        return -1;
    }
    if (mBlockMeta.getBlockSize() <= mInStreamPos) {
        return -1;
    }
    // Make a copy of the state to keep track of what we have read in this transferTo call.
    ByteBuf bufCopy = null;
    if (mBlockWriter != null) {
        bufCopy = buf.duplicate();
        bufCopy.readerIndex(bufCopy.writerIndex());
    }
    int bytesToRead = (int) Math.min((long) buf.writableBytes(), mBlockMeta.getBlockSize() - mInStreamPos);
    int bytesRead = buf.writeBytes(mUnderFileSystemInputStream, bytesToRead);

    if (bytesRead <= 0) {
        return bytesRead;
    }

    mInStreamPos += bytesRead;

    if (mBlockWriter != null) {
        bufCopy.writerIndex(buf.writerIndex());
        while (bufCopy.readableBytes() > 0) {
            mBlockWriter.transferFrom(bufCopy);
        }
    }

    return bytesRead;
}

From source file:at.yawk.dbus.protocol.object.ArrayObject.java

@Override
public void serialize(AlignableByteBuf buf) {
    ByteBuf tempBuffer = serializeValues(ArrayObject.allocateBufferForWrite(buf.getBuffer()));

    buf.alignWrite(4);//from  w w  w.j  a  va2  s .co  m
    buf.getBuffer().writeInt(tempBuffer.writerIndex());
    if (tempBuffer.isReadable()) {
        buf.getBuffer().writeBytes(tempBuffer);
    }
    tempBuffer.release();
}

From source file:books.netty.protocol.netty.codec.MarshallingEncoder.java

License:Apache License

protected void encode(Object msg, ByteBuf out) throws Exception {
    try {//from   www.  ja v  a2 s .  c om
        int lengthPos = out.writerIndex();
        out.writeBytes(LENGTH_PLACEHOLDER);
        ChannelBufferByteOutput output = new ChannelBufferByteOutput(out);
        marshaller.start(output);
        marshaller.writeObject(msg);
        marshaller.finish();
        out.setInt(lengthPos, out.writerIndex() - lengthPos - 4);
    } finally {
        marshaller.close();
    }
}

From source file:cloudeventbus.codec.Decoder.java

License:Open Source License

/**
 * Returns the number of bytes between the readerIndex of the haystack and
 * the first needle found in the haystack.  -1 is returned if no needle is
 * found in the haystack.//from w w w  .j av a 2  s  .  c om
 * <p/>
 * Copied from {@link io.netty.handler.codec.DelimiterBasedFrameDecoder}.
 */
private int indexOf(ByteBuf haystack, byte[] needle) {
    for (int i = haystack.readerIndex(); i < haystack.writerIndex(); i++) {
        int haystackIndex = i;
        int needleIndex;
        for (needleIndex = 0; needleIndex < needle.length; needleIndex++) {
            if (haystack.getByte(haystackIndex) != needle[needleIndex]) {
                break;
            } else {
                haystackIndex++;
                if (haystackIndex == haystack.writerIndex() && needleIndex != needle.length - 1) {
                    return -1;
                }
            }
        }

        if (needleIndex == needle.length) {
            // Found the needle from the haystack!
            return i - haystack.readerIndex();
        }
    }
    return -1;
}

From source file:com.allanbank.mongodb.netty.ByteToMessageDecoderTest.java

License:Apache License

/**
 * Test method for// w  ww .j a v a2 s.  c o  m
 * {@link ByteToMessageDecoder#decode(ChannelHandlerContext, ByteBuf)}.
 *
 * @throws Exception
 *             On a test failure.
 */
@Test
public void testDecodeInCompleteFrame() throws Exception {
    final Random rand = new Random(System.currentTimeMillis());
    final Message msg = new KillCursors(new long[] { rand.nextLong() }, ReadPreference.PRIMARY);
    final int msgId = rand.nextInt() & 0xFFFFFF;

    final ByteBuf buffer = ourAllocator.buffer();

    final ByteBufOutputStream out = new ByteBufOutputStream(buffer);
    final BsonOutputStream bout = new BsonOutputStream(out);

    msg.write(msgId, bout);

    final ChannelHandlerContext mockContext = createMock(ChannelHandlerContext.class);

    replay(mockContext);

    final ByteToMessageDecoder decoder = new ByteToMessageDecoder(new StringDecoderCache());

    final Object result = decoder.decode(mockContext, buffer.slice(0, buffer.writerIndex() - 1));
    assertThat(result, nullValue());

    verify(mockContext);
}