Example usage for io.netty.buffer ByteBuf readInt

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

Introduction

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

Prototype

public abstract int readInt();

Source Link

Document

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

Usage

From source file:com.specialeffect.messages.PickBlockMessage.java

License:Open Source License

@Override
public void fromBytes(ByteBuf buf) {
    entityId = buf.readInt();
}

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  a v  a  2  s  .  c o m

        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.FlushAllRequestTest.java

License:Apache License

@Test
public void testBuffer() throws Exception {
    int delay = 12;
    FlushAllRequest req = new FlushAllRequest(delay, OPAQUE);
    MemcacheEncoder memcacheEncoder = new MemcacheEncoder();
    List<Object> out = Lists.newArrayList();
    memcacheEncoder.encode(ctx, req, out);
    ByteBuf b = (ByteBuf) out.get(0);

    assertHeader(b, OpCode.FLUSH, 0, 4, 4, req.getOpaque(), 0);
    assertEquals(delay, b.readInt());
    assertEOM(b);/*from w w  w.  jav a 2  s  .c o m*/
}

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

License:Apache License

@Test
public void testBufferTtl() throws Exception {
    GetRequest get = new GetRequest(KEY, OpCode.GET, 123, OPAQUE);

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

    assertHeader(b, OpCode.GET, KEY.length(), 4, KEY.length() + 4, get.getOpaque(), 0);
    assertExpiration(b.readInt());
    assertString(KEY, b);//from  w w  w  .  ja  v a2  s  .c o m
    assertEOM(b);
}

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());//from ww  w .j  a  v  a  2  s.  c o  m
    assertEquals(3L, b.readLong());
    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 www.  j av  a  2  s. c  o m*/
    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.folsom.client.binary.SetRequestTest.java

License:Apache License

private void verifySetRequest(long cas) throws Exception {
    SetRequest req = new SetRequest(OpCode.ADD, KEY, TRANSCODER.encode(VALUE), 1000, cas, OPAQUE);

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

    assertHeader(b, OpCode.ADD, KEY.length(), 8, KEY.length() + 8 + VALUE.length(), req.getOpaque(), cas);
    assertZeros(b, 4);//from  w ww.j  av a  2 s  . c o m
    assertExpiration(b.readInt());
    assertString(KEY, b);
    assertString(VALUE, b);
    assertEOM(b);
}

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

License:Apache License

@Test
public void testBufferNoCas() throws Exception {
    TouchRequest req = new TouchRequest(KEY, 123, OPAQUE);
    MemcacheEncoder memcacheEncoder = new MemcacheEncoder();
    List<Object> out = Lists.newArrayList();
    memcacheEncoder.encode(ctx, req, out);
    ByteBuf b = (ByteBuf) out.get(0);

    assertHeader(b, OpCode.TOUCH, KEY.length(), 4, KEY.length() + 4, req.getOpaque(), 0);
    assertExpiration(b.readInt());
    assertString(KEY, b);//  w w  w  .j a v a  2s.c o m
    assertEOM(b);
}

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

License:Apache License

@Test
public void testBufferTtl() throws Exception {
    GetRequest get = new GetRequest(KEY, OpCode.GET, 123, OPAQUE);
    MemcacheEncoder memcacheEncoder = new MemcacheEncoder();
    List<Object> out = Lists.newArrayList();
    memcacheEncoder.encode(ctx, get, out);
    ByteBuf b = (ByteBuf) out.get(0);

    assertHeader(b, OpCode.GET, KEY.length(), 4, KEY.length() + 4, get.getOpaque(), 0);
    assertExpiration(b.readInt());
    assertString(KEY, b);/*from  w w  w.  j  ava  2  s. co  m*/
    assertEOM(b);
}

From source file:com.spotify.netty4.handler.codec.zmtp.benchmarks.CustomReqRepBenchmark.java

License:Apache License

private static int readStatusCode(final ByteBuf data, final int size) {
    if (size != 4) {
        throw new IllegalArgumentException();
    }//w ww . j a  va 2 s. co m
    return data.readInt();
}