Example usage for io.netty.buffer ByteBuf getInt

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

Introduction

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

Prototype

public abstract int getInt(int index);

Source Link

Document

Gets a 32-bit integer at the specified absolute index in this buffer.

Usage

From source file:com.twitter.http2.HttpFrameDecoderTest.java

License:Apache License

private void setReservedBits(ByteBuf frame) {
    frame.setInt(5, frame.getInt(5) | 0x80000000);
}

From source file:common.xandayn.personalizedrecipes_old.common.network.packet.client.ClientRemoveRecipeFromServer.java

License:Open Source License

@Override
public void toBytes(ByteBuf buf) {
    position = buf.getInt(0);
    removeAll = buf.getBoolean(1);
}

From source file:common.xandayn.personalizedrecipes_old.common.network.packet.server.ServerRemoveRecipePacket.java

License:Open Source License

@Override
public void toBytes(ByteBuf buf) {
    this.position = buf.getInt(0);
}

From source file:eu.stratosphere.runtime.io.network.netty.OutboundEnvelopeEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Envelope env, ByteBuf out) throws Exception {
    // --------------------------------------------------------------------
    // (1) header (48 bytes)
    // --------------------------------------------------------------------
    out.writeInt(MAGIC_NUMBER); // 4 bytes

    if (out.getInt(out.writerIndex() - 4) != MAGIC_NUMBER) {
        throw new RuntimeException();
    }//from  ww  w. j a  v  a  2s .co  m

    out.writeInt(env.getSequenceNumber()); // 4 bytes
    env.getJobID().writeTo(out); // 16 bytes
    env.getSource().writeTo(out); // 16 bytes
    out.writeInt(env.getEventsSerialized() != null ? env.getEventsSerialized().remaining() : 0); // 4 bytes
    out.writeInt(env.getBuffer() != null ? env.getBuffer().size() : 0); // 4 bytes
    // --------------------------------------------------------------------
    // (2) events (var length)
    // --------------------------------------------------------------------
    if (env.getEventsSerialized() != null) {
        out.writeBytes(env.getEventsSerialized());
    }

    // --------------------------------------------------------------------
    // (3) buffer (var length)
    // --------------------------------------------------------------------
    if (env.getBuffer() != null) {
        Buffer buffer = env.getBuffer();
        out.writeBytes(buffer.getMemorySegment().wrap(0, buffer.size()));

        // Recycle the buffer from OUR buffer pool after everything has been
        // copied to Nettys buffer space.
        buffer.recycleBuffer();
    }
}

From source file:io.airlift.drift.transport.netty.HeaderMessageEncoding.java

License:Apache License

@Override
public OptionalInt extractResponseSequenceId(ByteBuf buffer) {
    if (buffer.readableBytes() < HEADER_SEQUENCE_ID_OFFSET + Integer.BYTES) {
        return OptionalInt.empty();
    }/*  ww  w  .j ava 2s . co  m*/
    return OptionalInt.of(buffer.getInt(buffer.readerIndex() + HEADER_SEQUENCE_ID_OFFSET));
}

From source file:io.airlift.drift.transport.netty.server.ThriftProtocolDetection.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext context, ByteBuf in, List<Object> out) {
    // minimum bytes required to detect framing
    if (in.readableBytes() < 4) {
        return;/*  ww  w.  ja  v a  2 s.c om*/
    }

    int magic = in.getInt(in.readerIndex());

    // HTTP not supported
    if (magic == HTTP_POST_MAGIC) {
        in.clear();
        context.close();
        return;
    }

    // Unframed transport magic starts with the high byte set, whereas framed and header
    // both start with the frame size which must be a positive int
    if ((magic & UNFRAMED_MESSAGE_MASK) == UNFRAMED_MESSAGE_FLAG) {
        Optional<Protocol> protocol = detectProtocol(magic);
        if (!protocol.isPresent()) {
            in.clear();
            context.close();
            return;
        }

        switchToTransport(context, UNFRAMED, protocol);
        return;
    }

    // The second int is used to determine if the transport is framed or header
    if (in.readableBytes() < 8) {
        return;
    }

    int magic2 = in.getInt(in.readerIndex() + 4);
    if ((magic2 & HEADER_MAGIC_MASK) == HEADER_MAGIC) {
        switchToTransport(context, HEADER, Optional.empty());
        return;
    }

    Optional<Protocol> protocol = detectProtocol(magic2);
    if (!protocol.isPresent()) {
        in.clear();
        context.close();
        return;
    }

    switchToTransport(context, FRAMED, protocol);
}

From source file:io.hydramq.core.type.ConversionContext.java

License:Open Source License

public Command read(ByteBuf buffer) {
    if (!readable(buffer.getInt(0))) {
        return null;
    }//from ww  w.  ja v  a  2s.  c  o  m
    return read(buffer.readInt(), buffer);
}

From source file:io.hydramq.core.type.ConversionContext.java

License:Open Source License

public boolean readable(ByteBuf buffer) {
    return readable(buffer.getInt(0));
}

From source file:io.hydramq.core.type.converters.PartitionInfoResponseConverterTest.java

License:Open Source License

@Test
public void testRoundTripMarshall() throws Exception {
    ConversionContext context = ConversionContext.topicProtocol();
    PartitionInfoResponse input = new PartitionInfoResponse(4, new PartitionInfo(9, 100000));
    ByteBuf buffer = Unpooled.buffer();
    context.write(PartitionInfoResponse.class, input, buffer);
    assertThat(buffer.getInt(0), is(204));
    PartitionInfoResponse output = context.read(PartitionInfoResponse.class, buffer);
    assertThat(output.getPartitionInfo().head(), is(input.getPartitionInfo().head()));
    assertThat(output.getPartitionInfo().tail(), is(input.getPartitionInfo().tail()));
}

From source file:io.hydramq.network.server.ProtocolSelector.java

License:Open Source License

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
    ByteBuf buffer = (ByteBuf) msg;
    for (ProtocolHandler protocolHandler : protocols) {
        if (protocolHandler.accept(buffer)) {
            logger.info("Loading {} protocol handler", protocolHandler);
            ctx.pipeline().addLast("commandEncoder",
                    new CommandEncoder(protocolHandler.getConversionContext()));
            ctx.pipeline().addLast("commandDecoder",
                    new CommandDecoder(protocolHandler.getConversionContext()));
            ctx.pipeline().addLast("protocol", protocolHandler);
            ctx.pipeline().addLast("responseHandler", completableFutureHandler);
            ctx.pipeline().remove(this);
            ctx.pipeline().fireChannelActive();
            ctx.fireChannelRead(msg);/*from  ww w  .ja va2 s.  c o  m*/
            return;
        }
    }
    logger.error("No acceptable protocols found to handle client with protocol id of " + buffer.getInt(0));
    ctx.writeAndFlush(new Error(buffer.getInt(1), 5)).addListener(ChannelFutureListener.CLOSE);
}