Example usage for io.netty.buffer ByteBufUtil swapLong

List of usage examples for io.netty.buffer ByteBufUtil swapLong

Introduction

In this page you can find the example usage for io.netty.buffer ByteBufUtil swapLong.

Prototype

public static long swapLong(long value) 

Source Link

Document

Toggles the endianness of the specified 64-bit long integer.

Usage

From source file:com.spotify.netty.handler.codec.zmtp.ZMTPMessageParser.java

License:Apache License

private boolean parseZMTP2Header(ByteBuf buffer) throws ZMTPMessageParsingException {
    if (buffer.readableBytes() < 2) {
        return false;
    }/*from  w ww. j  av a2  s . c o  m*/
    int flags = buffer.readByte();
    hasMore = (flags & MORE_FLAG) == MORE_FLAG;
    if ((flags & LONG_FLAG) != LONG_FLAG) {
        frameSize = buffer.readByte() & 0xff;
        return true;
    }
    if (buffer.readableBytes() < 8) {
        return false;
    }
    long len;
    if (buffer.order() == BIG_ENDIAN) {
        len = buffer.readLong();
    } else {
        len = ByteBufUtil.swapLong(buffer.readLong());
    }
    if (len > Integer.MAX_VALUE) {
        throw new ZMTPMessageParsingException("Received too large frame: " + len);
    }
    frameSize = (int) len;
    return true;
}

From source file:com.spotify.netty.handler.codec.zmtp.ZMTPUtils.java

License:Apache License

/**
 * Helper to decode a ZMTP/1.0 length field
 *
 * @return length//from   w w w .ja v  a2 s. c om
 * @throws IndexOutOfBoundsException if there is not enough octets to be read.
 */
static public long decodeLength(final ByteBuf in) {
    long size = in.readByte() & 0xFF;
    if (size == 0xFF) {
        if (in.readableBytes() < 8) {
            return -1;
        }
        if (in.order() == BIG_ENDIAN) {
            size = in.readLong();
        } else {
            size = ByteBufUtil.swapLong(in.readLong());
        }
    }

    return size;
}

From source file:com.spotify.netty.handler.codec.zmtp.ZMTPUtils.java

License:Apache License

static void writeLong(final ByteBuf buffer, final long value) {
    if (buffer.order() == BIG_ENDIAN) {
        buffer.writeLong(value);/*from w  ww  .  j av  a  2s.co  m*/
    } else {
        buffer.writeLong(ByteBufUtil.swapLong(value));
    }
}

From source file:org.hawkular.metrics.clients.ptrans.collectd.packet.CollectdPacketDecoder.java

License:Apache License

private Values readValuePartContent(ByteBuf content, int length) {
    int beginIndex = content.readerIndex();
    int total = content.readUnsignedShort();
    List<DataType> dataTypes = new ArrayList<>(total);
    for (int i = 0; i < total; i++) {
        byte sampleTypeId = content.readByte();
        dataTypes.add(DataType.findById(sampleTypeId));
    }/*from w  ww.  j a  v  a 2 s.  c  o  m*/
    List<Number> data = new ArrayList<>(total);
    for (DataType dataType : dataTypes) {
        switch (dataType) {
        case COUNTER:
        case ABSOLUTE:
            byte[] valueBytes = new byte[8];
            content.readBytes(valueBytes);
            data.add(new BigInteger(1, valueBytes));
            break;
        case DERIVE:
            data.add(content.readLong());
            break;
        case GAUGE:
            data.add(Double.longBitsToDouble(ByteBufUtil.swapLong(content.readLong())));
            break;
        default:
            logger.debug("Skipping unknown data type: {}", dataType);
        }
    }
    // Skip any additionnal bytes
    int readCount = content.readerIndex() - beginIndex;
    if (length > readCount) {
        content.skipBytes(readCount - length);
    }
    return new Values(dataTypes, data);
}

From source file:org.hawkular.metrics.clients.ptrans.collectd.packet.PacketDecodingTest.java

License:Apache License

static ByteBuf createValuesPartBuffer(Values values) {
    List<Number> data = values.getData();
    ListIterator<Number> dataIterator = data.listIterator();
    List<DataType> dataTypes = values.getDataTypes();
    ListIterator<DataType> dataTypeIterator = dataTypes.listIterator();

    ByteBuf payloadBuffer = Unpooled.buffer();

    while (dataTypeIterator.hasNext()) {
        payloadBuffer.writeByte(dataTypeIterator.next().getId());
    }/*ww w  . j a  va 2s . c o m*/

    dataTypeIterator = dataTypes.listIterator();
    while (dataIterator.hasNext()) {
        DataType dataType = dataTypeIterator.next();
        Number number = dataIterator.next();
        switch (dataType) {
        case COUNTER:
        case ABSOLUTE:
            BigInteger bigInteger = (BigInteger) number;
            payloadBuffer.writeBytes(bigInteger.toByteArray());
            break;
        case DERIVE:
            payloadBuffer.writeLong((Long) number);
            break;
        case GAUGE:
            payloadBuffer.writeLong(ByteBufUtil.swapLong(Double.doubleToLongBits((Double) number)));
            break;
        default:
            fail("Unknown data type: " + dataType);
        }
    }

    ByteBuf headerBuffer = Unpooled.buffer();
    headerBuffer.writeShort(VALUES.getId());
    headerBuffer.writeShort(6 + payloadBuffer.writerIndex());
    headerBuffer.writeShort(data.size());

    ByteBuf buffer = Unpooled.buffer();
    buffer.writeBytes(headerBuffer.duplicate()).writeBytes(payloadBuffer.duplicate());
    return buffer;
}

From source file:org.rhq.metrics.netty.collectd.packet.CollectdPacketDecoder.java

License:Apache License

private Values readValuePartContent(ByteBuf content, int length) {
    int beginIndex = content.readerIndex();
    int total = content.readUnsignedShort();
    DataType[] dataTypes = new DataType[total];
    for (int i = 0; i < total; i++) {
        byte sampleTypeId = content.readByte();
        dataTypes[i] = DataType.findById(sampleTypeId);
    }//from  w  ww .j a  va  2s  . c o  m
    Number[] data = new Number[total];
    for (int i = 0; i < total; i++) {
        DataType dataType = dataTypes[i];
        switch (dataType) {
        case COUNTER:
        case ABSOLUTE:
            byte[] valueBytes = new byte[8];
            content.readBytes(valueBytes);
            data[i] = new BigInteger(1, valueBytes);
            break;
        case DERIVE:
            data[i] = content.readLong();
            break;
        case GAUGE:
            data[i] = Double.longBitsToDouble(ByteBufUtil.swapLong(content.readLong()));
            break;
        default:
            logger.debug("Skipping unknown data type: {}", dataType);
        }
    }
    // Skip any additionnal bytes
    int readCount = content.readerIndex() - beginIndex;
    if (length > readCount) {
        content.skipBytes(readCount - length);
    }
    return new Values(dataTypes, data);
}

From source file:org.rhq.metrics.netty.collectd.packet.PacketDecodingTest.java

License:Apache License

static ByteBuf createValuesPartBuffer(Values values) {
    Number[] data = values.getData();
    DataType[] dataTypes = values.getDataTypes();
    ByteBuf payloadBuffer = Unpooled.buffer();
    for (int i = 0; i < data.length; i++) {
        payloadBuffer.writeByte(dataTypes[i].getId());
    }//from  ww w  . ja v  a  2 s  .co m
    for (int i = 0; i < data.length; i++) {
        DataType dataType = dataTypes[i];
        switch (dataType) {
        case COUNTER:
        case ABSOLUTE:
            BigInteger bigInteger = (BigInteger) data[i];
            payloadBuffer.writeBytes(bigInteger.toByteArray());
            break;
        case DERIVE:
            payloadBuffer.writeLong((Long) data[i]);
            break;
        case GAUGE:
            payloadBuffer.writeLong(ByteBufUtil.swapLong(Double.doubleToLongBits((Double) data[i])));
            break;
        default:
            fail("Unknown data type: " + dataType);
        }
    }

    ByteBuf headerBuffer = Unpooled.buffer();
    headerBuffer.writeShort(VALUES.getId());
    headerBuffer.writeShort(6 + payloadBuffer.writerIndex());
    headerBuffer.writeShort(data.length);

    ByteBuf buffer = Unpooled.buffer();
    buffer.writeBytes(headerBuffer.duplicate()).writeBytes(payloadBuffer.duplicate());
    return buffer;
}