Example usage for io.netty.buffer ByteBuf compareTo

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

Introduction

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

Prototype

@Override
public abstract int compareTo(ByteBuf buffer);

Source Link

Document

Compares the content of the specified buffer to the content of this buffer.

Usage

From source file:org.apache.bookkeeper.proto.checksum.DigestManager.java

License:Apache License

private void verifyDigest(long entryId, ByteBuf dataReceived, boolean skipEntryIdCheck)
        throws BKDigestMatchException {

    if ((METADATA_LENGTH + macCodeLength) > dataReceived.readableBytes()) {
        logger.error(//from  ww w  . j  a v a  2  s .  c om
                "Data received is smaller than the minimum for this digest type. "
                        + " Either the packet it corrupt, or the wrong digest is configured. "
                        + " Digest type: {}, Packet Length: {}",
                this.getClass().getName(), dataReceived.readableBytes());
        throw new BKDigestMatchException();
    }
    update(dataReceived.slice(0, METADATA_LENGTH));

    int offset = METADATA_LENGTH + macCodeLength;
    update(dataReceived.slice(offset, dataReceived.readableBytes() - offset));

    ByteBuf digest = allocator.buffer(macCodeLength);
    populateValueAndReset(digest);

    try {
        if (digest.compareTo(dataReceived.slice(METADATA_LENGTH, macCodeLength)) != 0) {
            logger.error("Mac mismatch for ledger-id: " + ledgerId + ", entry-id: " + entryId);
            throw new BKDigestMatchException();
        }
    } finally {
        digest.release();
    }

    long actualLedgerId = dataReceived.readLong();
    long actualEntryId = dataReceived.readLong();

    if (actualLedgerId != ledgerId) {
        logger.error("Ledger-id mismatch in authenticated message, expected: " + ledgerId + " , actual: "
                + actualLedgerId);
        throw new BKDigestMatchException();
    }

    if (!skipEntryIdCheck && actualEntryId != entryId) {
        logger.error("Entry-id mismatch in authenticated message, expected: " + entryId + " , actual: "
                + actualEntryId);
        throw new BKDigestMatchException();
    }

}

From source file:org.apache.bookkeeper.proto.checksum.DigestManager.java

License:Apache License

public long verifyDigestAndReturnLac(ByteBuf dataReceived) throws BKDigestMatchException {
    if ((LAC_METADATA_LENGTH + macCodeLength) > dataReceived.readableBytes()) {
        logger.error(/*from   ww w. j  av  a  2 s. c  o  m*/
                "Data received is smaller than the minimum for this digest type."
                        + " Either the packet it corrupt, or the wrong digest is configured. "
                        + " Digest type: {}, Packet Length: {}",
                this.getClass().getName(), dataReceived.readableBytes());
        throw new BKDigestMatchException();
    }

    update(dataReceived.slice(0, LAC_METADATA_LENGTH));

    ByteBuf digest = allocator.buffer(macCodeLength);
    try {
        populateValueAndReset(digest);

        if (digest.compareTo(dataReceived.slice(LAC_METADATA_LENGTH, macCodeLength)) != 0) {
            logger.error("Mac mismatch for ledger-id LAC: " + ledgerId);
            throw new BKDigestMatchException();
        }
    } finally {
        digest.release();
    }

    long actualLedgerId = dataReceived.readLong();
    long lac = dataReceived.readLong();
    if (actualLedgerId != ledgerId) {
        logger.error("Ledger-id mismatch in authenticated message, expected: " + ledgerId + " , actual: "
                + actualLedgerId);
        throw new BKDigestMatchException();
    }
    return lac;
}

From source file:org.opendaylight.openflowjava.protocol.impl.core.OFDatagramPacketHandlerTest.java

License:Open Source License

/**
 * Test {@link OFDatagramPacketHandler}//from   ww  w .j a v a  2 s . c om
 */
@Test
public void test() {
    OFDatagramPacketHandler handler = new OFDatagramPacketHandler(switchConnHandler);
    byte version = EncodeConstants.OF13_VERSION_ID;
    ByteBuf messageBuffer = ByteBufUtils.hexStringToByteBuf("04 02 00 08 01 02 03 04");
    InetSocketAddress recipientISA = InetSocketAddress.createUnresolved("localhost", 9876);
    InetSocketAddress senderISA = InetSocketAddress.createUnresolved("192.168.15.24", 21021);
    DatagramPacket datagramPacket = new DatagramPacket(messageBuffer, recipientISA, senderISA);
    UdpConnectionMap.addConnection(datagramPacket.sender(), consumerMock);
    List<Object> outList = new ArrayList<>();
    try {
        handler.decode(ctxMock, datagramPacket, outList);
    } catch (Exception e) {
        Assert.fail("Wrong - Unexcepted exception occurred");
    }
    VersionMessageUdpWrapper versionUdpWrapper = (VersionMessageUdpWrapper) outList.get(0);
    Assert.assertEquals("Wrong - incorrect version has been decoded", version, versionUdpWrapper.getVersion());
    Assert.assertEquals("Wrong - sender addresses are different", senderISA, versionUdpWrapper.getAddress());
    messageBuffer.readerIndex(1);
    Assert.assertEquals("Wrong - undecoded part of input ByteBuff is differnt to output", 0,
            messageBuffer.compareTo(versionUdpWrapper.getMessageBuffer()));
}