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 ByteBuf writerIndex(int writerIndex);

Source Link

Document

Sets the writerIndex of this buffer.

Usage

From source file:io.nodyn.smalloc.Smalloc.java

License:Apache License

public static Object sliceOnto(JSObject src, JSObject dest, int start, int end) {
    ByteBuf srcBuf = ((NettyExternalIndexedData) src.getExternalIndexedData()).buffer();
    int len = end - start;
    ByteBuf destBuf = srcBuf.slice(start, len);
    destBuf.writerIndex(0);
    dest.setExternalIndexedData(new NettyExternalIndexedData(destBuf));
    return src;//from ww  w  . j  av  a2s.  c  o  m
}

From source file:io.vertx.proton.impl.ProtonWritableBufferImplTest.java

License:Apache License

@Test
public void testHasRemaining() {
    ByteBuf buffer = Unpooled.buffer(100, 100);
    ProtonWritableBufferImpl writable = new ProtonWritableBufferImpl(buffer);

    assertTrue(writable.hasRemaining());
    writable.put((byte) 0);
    assertTrue(writable.hasRemaining());
    buffer.writerIndex(buffer.maxCapacity());
    assertFalse(writable.hasRemaining());
}

From source file:me.ferrybig.p2pnetwork.Peer.java

public Future<?> pingAddress(Address addr) {
    int packetNumber = pingPacketCounter.getAndIncrement();
    byte[] data = new byte[4];
    ByteBuf wrappedBuffer = Unpooled.wrappedBuffer(data);
    wrappedBuffer.writerIndex(0);
    wrappedBuffer.writeInt(packetNumber);
    assert wrappedBuffer.array() == data;
    Promise<PongPacket> promise = events.newPromise();
    pingListeners.put(packetNumber, promise);
    promise.addListener(e -> pingListeners.remove(packetNumber));
    boolean send = routePacket(addr, new PingPacket(data));
    if (!send) {//from  www .ja  v a 2s.  c  o m
        promise.setFailure(new IllegalArgumentException("Unknown address"));
    }
    return promise;
}

From source file:me.melchor9000.net.resolver.DNSResolverTest.java

License:Open Source License

@Test
public void serializeCorrectly2() throws Exception {
    ByteBuf buff = Unpooled.buffer(160);

    DNSMessage message = new DNSMessage();
    message.setId(0xDEAD);/*  w  w  w  .  ja  v a  2s .  co m*/
    message.setQueryOrResponse(false);
    message.setOpcode((byte) 0);
    message.setAuthoritativeResponse(false);
    message.setTruncated(false);
    message.setRecursionDesired(true);

    buff.writerIndex(99);
    DNSA a = new DNSA(buff);
    DNSAAAA aaaa = new DNSAAAA(buff);
    DNSMX mx = new DNSMX(buff);
    buff.clear();

    a.setAddress(192, 168, 1, 101);
    aaaa.setAddress((Inet6Address) Inet6Address.getByName("fd6b:587e:77a::c85:7e1b:1a5e:7fd1"));
    mx.setExchange("mbp-de-melchor.local");
    mx.setPreference(1);

    DNSResourceRecord recordA = new DNSResourceRecord();
    recordA.setName("mbp-de-melchor.local");
    recordA.setType(1);
    recordA.setClass(1);
    recordA.setTtl(123);
    recordA.setData(a);

    DNSResourceRecord recordAAAA = new DNSResourceRecord();
    recordAAAA.setName("mbp-de-melchor.local");
    recordAAAA.setType("AAAA");
    recordAAAA.setClass(1);
    recordAAAA.setTtl(123);
    recordAAAA.setData(aaaa);

    DNSResourceRecord recordMX = new DNSResourceRecord();
    recordMX.setName("mbp-de-melchor.local");
    recordMX.setType("MX");
    recordMX.setClass(1);
    recordMX.setTtl(123);
    recordMX.setData(mx);

    message.addAnswer(recordA);
    message.addAnswer(recordAAAA);
    message.addAnswer(recordMX);

    assertEquals(12 + (12 + 20 + 4) + (12 + 20 + 16) + (12 + 20 + 24), message.byteBufSize());

    byte data[] = new byte[message.byteBufSize()];
    message.toByteBuf(buff);
    buff.readBytes(data);

    assertEquals(0, buff.readableBytes());
    assertArrayEquals(
            new byte[] { (byte) 0xDE, (byte) 0xAD, 0x01, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,

                    0x0E, 0x6d, 0x62, 0x70, 0x2d, 0x64, 0x65, 0x2d, 0x6d, 0x65, 0x6c, 0x63, 0x68, 0x6f, 0x72,
                    0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x7B,
                    0x00, 0x04, (byte) 192, (byte) 168, 1, 101,

                    0x0E, 0x6d, 0x62, 0x70, 0x2d, 0x64, 0x65, 0x2d, 0x6d, 0x65, 0x6c, 0x63, 0x68, 0x6f, 0x72,
                    0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, 0x1C, 0x00, 0x01, 0x00, 0x00, 0x00, 0x7B,
                    0x00, 0x10, (byte) 0xfd, 0x6b, 0x58, 0x7e, 0x07, 0x7a, 0x00, 0x00, 0x0c, (byte) 0x085, 0x7e,
                    0x1b, 0x1a, 0x5e, 0x7f, (byte) 0xd1,

                    0x0E, 0x6d, 0x62, 0x70, 0x2d, 0x64, 0x65, 0x2d, 0x6d, 0x65, 0x6c, 0x63, 0x68, 0x6f, 0x72,
                    0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, 0x0F, 0x00, 0x01, 0x00, 0x00, 0x00, 0x7B,
                    0x00, 0x18, 0x00, 0x01, 0x0E, 0x6d, 0x62, 0x70, 0x2d, 0x64, 0x65, 0x2d, 0x6d, 0x65, 0x6c,
                    0x63, 0x68, 0x6f, 0x72, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, },
            data);
}

From source file:mysql.client.Session_Old.java

public ByteBuf createSendByteBuf(int size) {
    ByteBuf byteBuf = allocator.heapBuffer(size);
    byteBuf = byteBuf.order(ByteOrder.LITTLE_ENDIAN);
    byteBuf.writerIndex(packetHeaderBuf.length);
    return byteBuf;
}

From source file:net.epsilony.utils.codec.modbus.handler.ModbusMasterResponseDecoderTest.java

License:Open Source License

public static ByteBuf createBuf() {
    ByteBuf buf = Unpooled.buffer(1024);
    int randShift = new Random().nextInt(10) + 7;
    buf.writerIndex(randShift);
    buf.readerIndex(randShift);/*  w w  w. j ava2 s.  co m*/
    return buf;
}

From source file:net.epsilony.utils.codec.modbus.handler.ModbusSlaveRequestDecoderTest.java

License:Open Source License

public static ByteBuf createBuf() {
    ByteBuf buf = Unpooled.buffer();
    int randShift = new Random().nextInt(10) + 7;
    buf.writerIndex(randShift);
    buf.readerIndex(randShift);//from www.  j  a  v  a 2s  .c  o  m
    return buf;
}

From source file:net.epsilony.utils.codec.modbus.reqres.ModbusResponseTest.java

License:Open Source License

@Test
public void testEncode() {
    TestData[] datas = new TestData[] { new TestData(0xFEED, 0x83, 0x81,
            new int[] { 0xFE, 0xED, 0x00, 0x00, 0x00, 0x05, 0x83, 0x81, 0xAB, 0xCD, 0xEF }) };

    ByteBuf buf = Unpooled.buffer();
    int randShift = new Random().nextInt(10) + 7;
    buf.writerIndex(randShift);
    buf.readerIndex(randShift);//ww w  . ja  v  a2 s.  c om

    for (TestData data : datas) {
        ModbusResponse response = data.createResponse();
        response.encode(buf);
        data.assertBuffer(buf);
    }
}

From source file:net.tomp2p.storage.AlternativeCompositeByteBuf.java

License:Apache License

private void copyTo(int index, int length, int componentId, ByteBuf dst) {
    int dstIndex = 0;
    int i = componentId;

    while (length > 0) {
        Component c = components.get(i);
        ByteBuf s = c.buf;// w  ww  .  ja  va 2 s .  com
        int adjustment = c.offset;
        int localLength = Math.min(length, s.readableBytes() - (index - adjustment));
        s.getBytes(index - adjustment, dst, dstIndex, localLength);
        index += localLength;
        dstIndex += localLength;
        length -= localLength;
        i++;
    }

    dst.writerIndex(dst.capacity());
}

From source file:net.tomp2p.storage.AlternativeCompositeByteBuf.java

License:Apache License

public List<ByteBuf> decompose(int offset, int length) {
    checkIndex(offset, length);//  ww w .  j  av a2s .  co  m
    if (length == 0) {
        return Collections.emptyList();
    }

    int componentId = findIndex(offset);
    List<ByteBuf> slice = new ArrayList<ByteBuf>(components.size());

    // The first component
    Component firstC = components.get(componentId);
    ByteBuf first = firstC.buf.duplicate();
    first.readerIndex(offset - firstC.offset);

    ByteBuf buf = first;
    int bytesToSlice = length;
    do {
        int readableBytes = buf.readableBytes();
        if (bytesToSlice <= readableBytes) {
            // Last component
            buf.writerIndex(buf.readerIndex() + bytesToSlice);
            slice.add(buf);
            break;
        } else {
            // Not the last component
            slice.add(buf);
            bytesToSlice -= readableBytes;
            componentId++;

            // Fetch the next component.
            buf = components.get(componentId).buf.duplicate();
        }
    } while (bytesToSlice > 0);

    // Slice all components because only readable bytes are interesting.
    for (int i = 0; i < slice.size(); i++) {
        slice.set(i, slice.get(i).slice());
    }

    return slice;
}