Example usage for io.netty.buffer ByteBuf readLong

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

Introduction

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

Prototype

public abstract long readLong();

Source Link

Document

Gets a 64-bit integer at the current readerIndex and increases the readerIndex by 8 in this buffer.

Usage

From source file:com.smithsmodding.smithscore.client.events.gui.ContainerGuiClosedEvent.java

/**
 * Function used by the instance created on the receiving side to reset its state from the sending side stored
 * by it in the Buffer before it is being fired on the NetworkRelayBus.
 *
 * @param pMessageBuffer The ByteBuffer from the IMessage used to Synchronize the implementing events.
 *///from ww  w.  j  a  v  a 2 s .  c  om
@Override
public void readFromMessageBuffer(ByteBuf pMessageBuffer) {
    playerID = new UUID(pMessageBuffer.readLong(), pMessageBuffer.readLong());
    containerID = ByteBufUtils.readUTF8String(pMessageBuffer);
}

From source file:com.smithsmodding.smithscore.util.common.NetworkHelper.java

/**
 * Method used to easily read a UUID from a MessageBuffer
 *
 * @param buffer The buffer to read the UUID from.
 * @return The next UUID stored in the buffer.
 *//*from  ww w  .j a va  2  s. co m*/
public static UUID readUUID(ByteBuf buffer) {
    return new UUID(buffer.readLong(), buffer.readLong());
}

From source file:com.spotify.folsom.client.binary.BinaryMemcacheDecoder.java

License:Apache License

@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf buf, final List<Object> out)
        throws Exception {
    for (int i = 0; i < BATCH_SIZE; i++) {
        if (buf.readableBytes() < 24) {
            return;
        }//from   w  w  w. j  av  a 2  s .com

        buf.markReaderIndex();

        final int magicNumber = buf.readUnsignedByte(); // byte 0
        if (magicNumber != 0x81) {
            throw fail(buf, String.format("Invalid magic number: 0x%2x", magicNumber));
        }

        final int opcode = buf.readByte(); // byte 1
        final int keyLength = buf.readUnsignedShort(); // byte 2-3
        final int extrasLength = buf.readUnsignedByte(); // byte 4
        buf.skipBytes(1);
        final int statusCode = buf.readUnsignedShort(); // byte 6-7

        final MemcacheStatus status = MemcacheStatus.fromInt(statusCode);

        final int totalLength = buf.readInt(); // byte 8-11
        final int opaque = buf.readInt();

        final long cas = buf.readLong();

        if (buf.readableBytes() < totalLength) {
            buf.resetReaderIndex();
            return;
        }

        buf.skipBytes(extrasLength);

        buf.skipBytes(keyLength);

        final int valueLength = totalLength - keyLength - extrasLength;
        byte[] valueBytes;
        if (valueLength == 0) {
            valueBytes = NO_BYTES;
        } else {
            valueBytes = new byte[valueLength];
        }

        buf.readBytes(valueBytes);

        replies.add(new ResponsePacket((byte) opcode, status, opaque, cas, valueBytes));
        if ((opaque & 0xFF) == 0) {
            out.add(replies);
            replies = new BinaryResponse();
        }
    }
}

From source file:com.spotify.folsom.client.binary.IncrRequestTest.java

License:Apache License

@Test
public void testBuffer() throws Exception {
    IncrRequest req = new IncrRequest(KEY, OpCode.INCREMENT, 2, 3, 1000, OPAQUE);

    MemcacheEncoder memcacheEncoder = new MemcacheEncoder();
    List<Object> out = Lists.newArrayList();
    memcacheEncoder.encode(ctx, req, out);
    ByteBuf b = (ByteBuf) out.get(0);

    assertHeader(b, OpCode.INCREMENT, KEY.length(), 20, KEY.length() + 20, req.getOpaque(), 0);
    assertEquals(2L, b.readLong());
    assertEquals(3L, b.readLong());/*from ww w . ja  v a  2s .c  o m*/
    assertExpiration(b.readInt());
    assertString(KEY, b);
    assertEOM(b);
}

From source file:com.spotify.folsom.client.binary.RequestTestTemplate.java

License:Apache License

protected void assertHeader(final ByteBuf b, final int opcode, final int keyLength, final int extrasLength,
        final int totalLength, final int opaque, final long cas) {
    assertByte(0x80, b.readByte());/*from   w  w  w  .j  a v a  2 s . c om*/
    assertByte(opcode, b.readByte());
    assertShort(keyLength, b.readShort());
    assertShort(extrasLength, b.readByte());
    assertZeros(b, 3);
    assertEquals(totalLength, b.readInt());
    assertEquals(opaque, b.readInt());
    assertEquals(cas, b.readLong());
}

From source file:com.spotify.heroic.consumer.collectd.CollectdParser.java

License:Apache License

public static Iterator<CollectdSample> parse(final ByteBuf frame) {
    if (frame.readableBytes() < 4) {
        throw new RuntimeException("frame to short");
    }/*from  w  w w .j ava  2s .c  om*/

    frame.order(ByteOrder.BIG_ENDIAN);

    return new Iterator<CollectdSample>() {
        private Decoded decoded = new Decoded();

        @Override
        public boolean hasNext() {
            return frame.readableBytes() > 0;
        }

        @Override
        public CollectdSample next() {
            while (true) {
                final int type = frame.readUnsignedShort();
                final int size = frame.readUnsignedShort();

                switch (type) {
                case HOST:
                    decoded.host = parseString(frame, size);
                    break;
                case TIME:
                    decoded.time = frame.readLong();
                    break;
                case TIME_HR:
                    decoded.time = (long) (((double) frame.readLong()) / FACTOR_HR);
                    break;
                case PLUGIN:
                    decoded.plugin = parseString(frame, size);
                    break;
                case PLUGIN_INSTANCE:
                    decoded.pluginInstance = parseString(frame, size);
                    break;
                case TYPE:
                    decoded.type = parseString(frame, size);
                    break;
                case TYPE_INSTANCE:
                    decoded.typeInstance = parseString(frame, size);
                    break;
                case INTERVAL:
                    decoded.interval = frame.readLong();
                    break;
                case INTERVAL_HR:
                    decoded.interval = (long) (((double) frame.readLong()) / FACTOR_HR);
                    break;
                case MESSAGE:
                    decoded.message = parseString(frame, size);
                    break;
                case SEVERITY:
                    decoded.severity = frame.readLong();
                    break;
                case VALUES:
                    return decoded.toSample(parseValues(frame, size));
                default:
                    log.warn("unhandled type: " + type);
                    break;
                }
            }
        }
    };
}

From source file:com.spotify.heroic.consumer.collectd.CollectdParser.java

License:Apache License

public static List<CollectdValue> parseValues(final ByteBuf frame, final int size) {
    final int n = frame.readUnsignedShort();

    final List<Integer> types = ImmutableList.copyOf(IntStream.range(0, n).map(i -> {
        return frame.readByte();
    }).iterator());/*from  w  w  w.j a  va 2  s.  c  o  m*/

    final ImmutableList.Builder<CollectdValue> values = ImmutableList.builder();

    for (final int type : types) {
        switch (type) {
        case CollectdSample.COUNTER:
            final long c = frame.readLong();

            if (c < 0) {
                throw new IllegalArgumentException("value too large for signed type");
            }

            values.add(new Counter(c));
            break;
        case CollectdSample.GAUGE:
            frame.order(ByteOrder.LITTLE_ENDIAN);
            values.add(new CollectdValue.Gauge(frame.readDouble()));
            frame.order(ByteOrder.BIG_ENDIAN);
            break;
        case CollectdSample.DERIVE:
            values.add(new CollectdValue.Derive(frame.readLong()));
            break;
        case CollectdSample.ABSOLUTE:
            final long a = frame.readLong();

            if (a < 0) {
                throw new IllegalArgumentException("value too large for signed type");
            }

            values.add(new CollectdValue.Absolute(a));
            break;
        default:
            throw new IllegalArgumentException("invalid sample type: " + type);
        }
    }

    return values.build();
}

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;
    }/* ww  w  .  jav a 2  s  .c om*/
    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   ww w .j  a v a 2 s. c  o  m*/
 * @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.netty4.handler.codec.zmtp.benchmarks.CustomReqRepBenchmark.java

License:Apache License

private static MessageId readId(final ByteBuf data, final int size) {
    if (size != 16) {
        throw new IllegalArgumentException();
    }/*w w w  . j  a va 2  s . c om*/
    final long seq = data.readLong();
    final long timestamp = data.readLong();
    return MessageId.from(seq, timestamp);
}