Example usage for io.netty.buffer ByteBuf copy

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

Introduction

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

Prototype

public abstract ByteBuf copy(int index, int length);

Source Link

Document

Returns a copy of this buffer's sub-region.

Usage

From source file:ru.jts_dev.authserver.util.Encoder.java

License:Open Source License

public ByteBuf validateChecksum(ByteBuf buf) {
    if (buf.readableBytes() % 4 != 0 || buf.readableBytes() <= 4) {
        throw new IndexOutOfBoundsException("ByteBuf size must be multiply of 4 and more, that 4");
    }//from   ww w. j av a2  s  .  c  o  m

    long checksum = 0;
    long check;
    int i;

    for (i = 0; i < buf.readableBytes() - 4; i += 4) {
        check = buf.getInt(i);

        checksum ^= check;
    }

    check = buf.getInt(i);

    if (check != checksum) {
        throw new InvalidParameterException("Wrong checksum");
    }

    return buf.copy(0, buf.readableBytes() - 4);
}