Example usage for io.netty.buffer ByteBuf readerIndex

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

Introduction

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

Prototype

public abstract int readerIndex();

Source Link

Document

Returns the readerIndex of this buffer.

Usage

From source file:com.l2jmobius.gameserver.network.client.Crypt.java

License:Open Source License

@Override
public void decrypt(ByteBuf buf) {
    if (!_isEnabled) {
        onPacketReceive(buf);//  www.  jav a  2s . c  om
        return;
    }

    int a = 0;
    while (buf.isReadable()) {
        final int b = buf.readByte() & 0xFF;
        buf.setByte(buf.readerIndex() - 1, b ^ _inKey[(buf.readerIndex() - 1) & 15] ^ a);
        a = b;
    }

    shiftKey(_inKey, buf.writerIndex());

    onPacketReceive(buf);
}

From source file:com.linecorp.armeria.client.endpoint.dns.DnsAddressEndpointGroup.java

License:Apache License

@Override
ImmutableSortedSet<Endpoint> onDnsRecords(List<DnsRecord> records, int ttl) throws Exception {
    final ImmutableSortedSet.Builder<Endpoint> builder = ImmutableSortedSet.naturalOrder();
    final boolean hasLoopbackARecords = records.stream().filter(r -> r instanceof DnsRawRecord)
            .map(DnsRawRecord.class::cast).anyMatch(
                    r -> r.type() == DnsRecordType.A && r.content().getByte(r.content().readerIndex()) == 127);

    for (DnsRecord r : records) {
        if (!(r instanceof DnsRawRecord)) {
            continue;
        }/*from w w w .  j  a  v a 2 s .  c  om*/

        final DnsRecordType type = r.type();
        final ByteBuf content = ((ByteBufHolder) r).content();
        final int contentLen = content.readableBytes();

        // Skip invalid records.
        if (type == DnsRecordType.A) {
            if (contentLen != 4) {
                warnInvalidRecord(DnsRecordType.A, content);
                continue;
            }
        } else if (type == DnsRecordType.AAAA) {
            if (contentLen != 16) {
                warnInvalidRecord(DnsRecordType.AAAA, content);
                continue;
            }
        } else {
            continue;
        }

        // Convert the content into an IP address and then into an endpoint.
        final String ipAddr;
        final byte[] addrBytes = new byte[contentLen];
        content.getBytes(content.readerIndex(), addrBytes);

        if (contentLen == 16) {
            // Convert some IPv6 addresses into IPv4 addresses to remove duplicate endpoints.
            if (addrBytes[0] == 0x00 && addrBytes[1] == 0x00 && addrBytes[2] == 0x00 && addrBytes[3] == 0x00
                    && addrBytes[4] == 0x00 && addrBytes[5] == 0x00 && addrBytes[6] == 0x00
                    && addrBytes[7] == 0x00 && addrBytes[8] == 0x00 && addrBytes[9] == 0x00) {

                if (addrBytes[10] == 0x00 && addrBytes[11] == 0x00) {
                    if (addrBytes[12] == 0x00 && addrBytes[13] == 0x00 && addrBytes[14] == 0x00
                            && addrBytes[15] == 0x01) {
                        // Loopback address (::1)
                        if (hasLoopbackARecords) {
                            // Contains an IPv4 loopback address already; skip.
                            continue;
                        } else {
                            ipAddr = "::1";
                        }
                    } else {
                        // IPv4-compatible address.
                        ipAddr = NetUtil.bytesToIpAddress(addrBytes, 12, 4);
                    }
                } else if (addrBytes[10] == -1 && addrBytes[11] == -1) {
                    // IPv4-mapped address.
                    ipAddr = NetUtil.bytesToIpAddress(addrBytes, 12, 4);
                } else {
                    ipAddr = NetUtil.bytesToIpAddress(addrBytes);
                }
            } else {
                ipAddr = NetUtil.bytesToIpAddress(addrBytes);
            }
        } else {
            ipAddr = NetUtil.bytesToIpAddress(addrBytes);
        }

        final Endpoint endpoint = port != 0 ? Endpoint.of(hostname, port) : Endpoint.of(hostname);
        builder.add(endpoint.withIpAddr(ipAddr));
    }

    final ImmutableSortedSet<Endpoint> endpoints = builder.build();
    if (logger().isDebugEnabled()) {
        logger().debug("{} Resolved: {} (TTL: {})", logPrefix(),
                endpoints.stream().map(Endpoint::ipAddr).collect(Collectors.joining(", ")), ttl);
    }

    return endpoints;
}

From source file:com.linecorp.armeria.server.thrift.TByteBufTransport.java

License:Apache License

@Override
public int getBufferPosition() {
    final ByteBuf buf = this.buf;
    if (!buf.hasArray()) {
        return 0;
    } else {/*from ww  w.  j a  v a  2 s. co  m*/
        return buf.arrayOffset() + buf.readerIndex();
    }
}

From source file:com.mastfrog.scamper.codec.MessageCodec.java

License:Open Source License

/**
 * Determine if this codec recognizes this ByteBuf.
 * <p>/*from  w  w w  .j  a v  a 2s .c  om*/
 * This method will move the reader index of the passed ByteBuf <i>forward one byte</i>
 * if it returns true.  If it returns false, the ByteBuf's state will be 
 * unaltered.
 * <p>
 * The default implementation checks if the first byte equals the return
 * value of <code>magicNumber()</code>.  Implementations that delegate
 * between multiple codecs should call this method for each until one
 * accepts it.
 * 
 * @param data
 * @return 
 */
public boolean accept(ByteBuf data) {
    int old = data.readerIndex();
    byte b = data.readByte();
    boolean result = magicNumber() == b;
    if (!result) {
        data.readerIndex(old);
    }
    return result;
}

From source file:com.mastfrog.scamper.compression.AutoCompressCodec.java

License:Open Source License

@Override
public boolean accept(ByteBuf data) {
    int ix = data.readerIndex();
    boolean result = raw.accept(data) || compress.accept(data);
    if (!result) {
        data.readerIndex(ix);// w w  w .  j a  va2s.com
    }
    return result;
}

From source file:com.mastfrog.scamper.compression.CompressingCodec.java

License:Open Source License

static String bb2s(ByteBuf buf) throws IOException {
    int old = buf.readerIndex();
    try (ByteBufInputStream in = new ByteBufInputStream(buf)) {
        return Streams.readString(in);
    } finally {//from  ww w.j  av a2 s  . co m
        buf.readerIndex(old);
    }
}

From source file:com.mastfrog.scamper.password.crypto.EncryptingCodecTest.java

License:Open Source License

private String dataFrom(ByteBuf buf) throws IOException {
    int old = buf.readerIndex();
    try (ByteBufInputStream in = new ByteBufInputStream(buf)) {
        return Streams.readString(in);
    } finally {/*from ww  w.ja  va  2s .c o m*/
        buf.readerIndex(old);
    }
}

From source file:com.mastfrog.scamper.password.crypto.EncryptingCodecTest.java

License:Open Source License

@Test
public void test(MessageCodec codec, MessageTypeRegistry reg) throws Throwable {
    for (int i = 0; i < 10; i++) {
        byte[] add = new byte[i + 1];
        Arrays.fill(add, (byte) 'a');
        String testData = "ABCD 123 Hello world I have some trees in my elbow and you are blue"
                + new String(add);
        ByteBuf toEncode = bufFor(testData);
        assertEquals(testData, dataFrom(toEncode));
        ByteBuf encoded = codec.encode(TYPE, toEncode, fakeChannel());
        String enc = dataFrom(encoded);
        encoded.resetReaderIndex();/*from ww w .  j a  va 2s.c  o  m*/
        System.out.println("ENCODED TO " + enc + " bytes " + enc.length());
        assertEquals(0, encoded.readerIndex());

        MessageTypeAndBuffer result = codec.decode(encoded, fakeChannelContext(), 0);
        assertEquals(TYPE, result.messageType);
        assertEquals(testData, dataFrom(result.buf));
    }
}

From source file:com.mobius.software.mqtt.parser.test.TestParser.java

License:Open Source License

@Test
public void testPositivenext() throws MalformedMessageException {
    ByteBuf buf = Unpooled.buffer(114);
    buf.writeByte(0x82);/*from  w w  w.java  2  s  . co m*/
    buf.writeByte(0x66);
    buf.writeShort(10);
    buf.writeShort(97);
    buf.writeBytes(new byte[96]);
    buf.writeByte(1);
    buf.writeByte(0);
    assertEquals("Invalid next header length", 104, MQParser.next(buf).capacity());
    assertEquals("buffer index was not reset", 0, buf.readerIndex());
}

From source file:com.mobius.software.mqtt.parser.test.TestParser.java

License:Open Source License

@Test
public void testNegativeNextIndexNotReset() throws MalformedMessageException {
    ByteBuf buf = Unpooled.buffer(103);
    buf.writeByte(0x82);/*from  w w w  . j a v a  2s  . c  om*/
    buf.writeByte(0x66); //encoded l=104, actual l=103
    buf.writeShort(10);
    buf.writeShort(97);
    buf.writeBytes(new byte[96]);
    buf.writeByte(1);
    try {
        MQParser.next(buf);
    } catch (MalformedMessageException e) {

    }
    assertEquals("buffer index was not reset", 0, buf.readerIndex());
}