Java Code Examples for io.netty.buffer.ByteBuf.getInt()

The following are Jave code examples for showing how to use getInt() of the io.netty.buffer.ByteBuf class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: angel   File: ServerDenseIntRowTest.java   Source Code and License Vote up 6 votes
@Test
public void testUpdate() throws Exception {
  serverDenseIntRow = new ServerDenseIntRow(rowId, startCol, endCol);
  ByteBuf buf = Unpooled.buffer(16);
  buf.writeInt(0);
  buf.writeInt(1);
  buf.writeInt(-1);
  int newValue0 = buf.getInt(0) + serverDenseIntRow.getData().get(0);
  int newValue1 = buf.getInt(4) + serverDenseIntRow.getData().get(1);
  serverDenseIntRow.update(RowType.T_INT_DENSE, buf, 3);
  assertEquals(serverDenseIntRow.getData().get(0), newValue0, 0.000);
  assertEquals(serverDenseIntRow.getData().get(1), newValue1, 0.000);
  assertEquals(serverDenseIntRow.getData().get(2), -1, 0.000);

  serverDenseIntRow = new ServerDenseIntRow(rowId, startCol, endCol);
  buf = Unpooled.buffer(0);
  buf.writeInt(0);
  buf.writeInt(1);
  buf.writeInt(2);
  buf.writeInt(-2);
  serverDenseIntRow.update(RowType.T_INT_SPARSE, buf, 2);
  assertEquals(serverDenseIntRow.getData().get(0), 1, 0.000);
  assertEquals(serverDenseIntRow.getData().get(1), 0, 0.000);
  assertEquals(serverDenseIntRow.getData().get(2), -2, 0.000);
}
 
Example 2
Project: elasticsearch_my   File: Netty4MessageChannelHandler.java   Source Code and License Vote up 6 votes
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    Transports.assertTransportThread();
    if (!(msg instanceof ByteBuf)) {
        ctx.fireChannelRead(msg);
        return;
    }
    final ByteBuf buffer = (ByteBuf) msg;
    final int remainingMessageSize = buffer.getInt(buffer.readerIndex() - TcpHeader.MESSAGE_LENGTH_SIZE);
    final int expectedReaderIndex = buffer.readerIndex() + remainingMessageSize;
    try {
        InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
        // netty always copies a buffer, either in NioWorker in its read handler, where it copies to a fresh
        // buffer, or in the cumulative buffer, which is cleaned each time so it could be bigger than the actual size
        BytesReference reference = Netty4Utils.toBytesReference(buffer, remainingMessageSize);
        transport.messageReceived(reference, ctx.channel(), profileName, remoteAddress, remainingMessageSize);
    } finally {
        // Set the expected position of the buffer, no matter what happened
        buffer.readerIndex(expectedReaderIndex);
    }
}
 
Example 3
Project: Quavo   File: ByteBufUtils.java   Source Code and License Vote up 6 votes
/**
 * Deciphers the specified {@link ByteBuf} with the given key.
 * 
 * @param buffer The {@link ByteBuf}.
 * @param key The key.
 * @throws IllegalArgumentException if the key is not exactly 4 elements long.
 */
private static void decipherXTEA(ByteBuf buffer, int start, int end, int[] key) {
	if (key.length != 4) {
		throw new IllegalArgumentException();
	}
	int numQuads = (end - start) / 8;
	for (int i = 0; i < numQuads; i++) {
		int sum = 0x9E3779B9 * 32;
		int v0 = buffer.getInt(start + i * 8);
		int v1 = buffer.getInt(start + i * 8 + 4);
		for (int j = 0; j < 32; j++) {
			v1 -= (((v0 << 4) ^ (v0 >>> 5)) + v0) ^ (sum + key[(sum >>> 11) & 3]);
			sum -= 0x9E3779B9;
			v0 -= (((v1 << 4) ^ (v1 >>> 5)) + v1) ^ (sum + key[sum & 3]);
		}
		buffer.setInt(start + i * 8, v0);
		buffer.setInt(start + i * 8 + 4, v1);
	}
}
 
Example 4
Project: proteus-java   File: ProteusMetadata.java   Source Code and License Vote up 5 votes
public static ByteBuf metadata(ByteBuf byteBuf) {
  int offset = VERSION_SIZE + NAMESPACE_ID_SIZE + SERVICE_ID_SIZE + METHOD_ID_SIZE;
  int length = byteBuf.getInt(offset);
  offset += METADATA_LENGTH_SIZE;

  return byteBuf.slice(offset, length);
}
 
Example 5
Project: hekate   File: NetworkProtocolCodec.java   Source Code and License Vote up 4 votes
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    // Check if we have enough bytes in the buffer.
    if (in.readableBytes() < HEADER_LENGTH) {
        // Retry once we have more data.
        return;
    }

    int startIndex = in.readerIndex();

    // Peek length header (note that we are not updating the buffer's read index).
    int length = in.getInt(startIndex);

    // Magic length value:
    //   negative - for internal messages
    //   positive - for user messages
    boolean internalMsg = length < 0;

    // Make sure that length is always positive.
    if (internalMsg) {
        length = -length;
    }

    // Check if we have enough bytes to decode.
    if (in.readableBytes() < length) {
        // Retry once we have more data.
        return;
    }

    // Skip the length header (we already have it).
    in.skipBytes(HEADER_LENGTH);

    // Decode message.
    int msgOffset = startIndex + HEADER_LENGTH;
    int msgLength = length - HEADER_LENGTH;

    Object msg;

    if (internalMsg) {
        // Decode internal message.
        msg = decodeInternal(in, msgOffset, msgLength);
    } else {
        // Decode user-defined message.
        msg = decodeUser(in, msgOffset, msgLength);
    }

    // Advance buffer's read position.
    in.readerIndex(startIndex + length);

    if (msg != null) {
        out.add(msg);
    }
}
 
Example 6
Project: proteus-java   File: ProteusMetadata.java   Source Code and License Vote up 4 votes
public static int namespaceId(ByteBuf byteBuf) {
  int offset = VERSION_SIZE;
  return byteBuf.getInt(offset);
}
 
Example 7
Project: proteus-java   File: ProteusMetadata.java   Source Code and License Vote up 4 votes
public static int serviceId(ByteBuf byteBuf) {
  int offset = VERSION_SIZE + NAMESPACE_ID_SIZE;
  return byteBuf.getInt(offset);
}
 
Example 8
Project: proteus-java   File: ProteusMetadata.java   Source Code and License Vote up 4 votes
public static int methodId(ByteBuf byteBuf) {
  int offset = VERSION_SIZE + NAMESPACE_ID_SIZE + SERVICE_ID_SIZE;
  return byteBuf.getInt(offset);
}
 
Example 9
Project: Pangu   File: KeyMessage.java   Source Code and License Vote up 4 votes
/**
 * Convert from the supplied buffer into your specific message type
 *
 * @param buf
 */
@Override
public void fromBytes(ByteBuf buf) {
    int lengh = buf.getInt(0);
    key = buf.getCharSequence(1, lengh, StandardCharsets.UTF_8).toString();
}
 
Example 10
Project: reactive-pg-client   File: MessageDecoder.java   Source Code and License Vote up 4 votes
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
  while (true) {
    if (in.readableBytes() < 5) {
      break;
    }
    int beginIdx = in.readerIndex();
    byte id = in.getByte(beginIdx);
    int length = in.getInt(beginIdx + 1);
    int endIdx = beginIdx + length + 1;
    final int writerIndex = in.writerIndex();
    if (writerIndex < endIdx) {
      break;
    }
    try {
      in.setIndex(beginIdx + 5, endIdx);
      switch (id) {
        case READY_FOR_QUERY: {
          decodeReadyForQuery(in, out);
          break;
        }
        case DATA_ROW: {
          decodeDataRow(in);
          break;
        }
        case COMMAND_COMPLETE: {
          decodeCommandComplete(in, out);
          break;
        }
        case BIND_COMPLETE: {
          decodeBindComplete(out);
          break;
        }
        default: {
          decodeMessage(id, in ,out);
        }
      }
    } finally {
      in.setIndex(endIdx, writerIndex);
    }
  }
}