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

The following are Jave code examples for showing how to use setByte() 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: SamaGamesAPI   File: ItemUtils.java   Source Code and License Vote up 6 votes
/**
 * Open the given written book
 *
 * @param book Written book
 * @param player Player
 */
public static void openWrittenBook(ItemStack book, Player player)
{
    if (book.getType() != Material.WRITTEN_BOOK)
        return;

    ItemStack previous = player.getInventory().getItemInHand();
    player.getInventory().setItemInHand(book);

    ByteBuf buffer = Unpooled.buffer(256);
    buffer.setByte(0, (byte) 1);
    buffer.writerIndex(1);

    Reflection.sendPacket(player, new PacketPlayOutCustomPayload("MC|BOpen", new PacketDataSerializer(buffer)));

    player.getInventory().setItemInHand(previous);
}
 
Example 2
Project: QDrill   File: DrillStringUtils.java   Source Code and License Vote up 6 votes
/**
 * In-place parsing of a hex encoded binary string.
 *
 * This function does not modify  the {@code readerIndex} and {@code writerIndex}
 * of the byte buffer.
 *
 * @return Index in the byte buffer just after the last written byte.
 */
public static int parseBinaryString(ByteBuf str, int strStart, int strEnd) {
  int length = (strEnd - strStart);
  int dstEnd = strStart;
  for (int i = strStart; i < length ; i++) {
    byte b = str.getByte(i);
    if (b == '\\'
        && length > i+3
        && (str.getByte(i+1) == 'x' || str.getByte(i+1) == 'X')) {
      // ok, take next 2 hex digits.
      byte hd1 = str.getByte(i+2);
      byte hd2 = str.getByte(i+3);
      if (isHexDigit(hd1) && isHexDigit(hd2)) { // [a-fA-F0-9]
        // turn hex ASCII digit -> number
        b = (byte) ((toBinaryFromHex(hd1) << 4) + toBinaryFromHex(hd2));
        i += 3; // skip 3
      }
    }
    str.setByte(dstEnd++, b);
  }
  return dstEnd;
}
 
Example 3
Project: L2J-Global   File: Crypt.java   Source Code and License Vote up 6 votes
@Override
public void encrypt(ByteBuf buf)
{
	if (!_isEnabled)
	{
		_isEnabled = true;
		onPacketSent(buf);
		return;
	}
	
	onPacketSent(buf);
	
	int a = 0;
	while (buf.isReadable())
	{
		final int b = buf.readByte() & 0xFF;
		a = b ^ _outKey[(buf.readerIndex() - 1) & 15] ^ a;
		buf.setByte(buf.readerIndex() - 1, a);
	}
	
	shiftKey(_outKey, buf.writerIndex());
}
 
Example 4
Project: L2J-Global   File: Crypt.java   Source Code and License Vote up 6 votes
@Override
public void decrypt(ByteBuf buf)
{
	if (!_isEnabled)
	{
		onPacketReceive(buf);
		return;
	}
	
	int a = 0;
	while (buf.isReadable())
	{
		final int b = buf.readByte() & 0xFF;
		buf.setByte(buf.readerIndex() - 1, b ^ _inKey[(buf.readerIndex() - 1) & 15] ^ a);
		a = b;
	}
	
	shiftKey(_inKey, buf.writerIndex());
	
	onPacketReceive(buf);
}
 
Example 5
Project: dremio-oss   File: DremioStringUtils.java   Source Code and License Vote up 6 votes
/**
 * Parses a hex encoded binary string and write to an output buffer.
 *
 * This function does not modify  the {@code readerIndex} and {@code writerIndex}
 * of the byte buffer.
 *
 * @return Index in the byte buffer just after the last written byte.
 */
public static int parseBinaryString(ByteBuf str, int strStart, int strEnd, ByteBuf out) {
  int dstEnd = 0;
  for (int i = strStart; i < strEnd; i++) {
    byte b = str.getByte(i);
    if (b == '\\'
        && strEnd > i+3
        && (str.getByte(i+1) == 'x' || str.getByte(i+1) == 'X')) {
      // ok, take next 2 hex digits.
      byte hd1 = str.getByte(i+2);
      byte hd2 = str.getByte(i+3);
      if (isHexDigit(hd1) && isHexDigit(hd2)) { // [a-fA-F0-9]
        // turn hex ASCII digit -> number
        b = (byte) ((toBinaryFromHex(hd1) << 4) + toBinaryFromHex(hd2));
        i += 3; // skip 3
      }
    }
    out.setByte(dstEnd++, b);
  }
  return dstEnd;
}
 
Example 6
Project: dremio-oss   File: DremioStringUtils.java   Source Code and License Vote up 6 votes
public static int parseBinaryStringNoFormat(ByteBuf str, int strStart, int strEnd, ByteBuf out) {
  int dstEnd = 0;

  if(((strStart - strEnd) % 2) != 0){
    throw UserException.functionError().message("Failure parsing hex string, length was not a multiple of two.").build(logger);
  }
  for (int i = strStart; i < strEnd; i+=2) {
    byte b1 = str.getByte(i);
    byte b2 = str.getByte(i+1);
    if(isHexDigit(b1) && isHexDigit(b2)){
      byte finalByte = (byte) ((toBinaryFromHex(b1) << 4) + toBinaryFromHex(b2));
      out.setByte(dstEnd++, finalByte);
    }else{
      throw UserException.functionError().message("Failure parsing hex string, one or more bytes was not a valid hex value.").build(logger);
    }
  }
  return dstEnd;
}
 
Example 7
Project: reactive-pg-client   File: DataType.java   Source Code and License Vote up 5 votes
@Override
public void encodeText(Buffer value, ByteBuf buff) {
  int index = buff.writerIndex();
  buff.setByte(index + 4, '\\');
  buff.setByte(index + 5, 'x');
  // todo : optimize - no need to create an intermediate string here
  int len = buff.setCharSequence(index + 6, printHexBinary(value.getBytes()), StandardCharsets.UTF_8);
  buff.writeInt(2 + len);
  buff.writerIndex(index + 2 + len);
}
 
Example 8
Project: Quavo   File: XOREncryptionEncoder.java   Source Code and License Vote up 5 votes
@Override
protected void encode(ChannelHandlerContext ctx, XOREncryptionResponse msg, ByteBuf out) throws Exception {
	if (msg.getKey() != 0) {
		for (int i = 0; i < out.writerIndex(); i++) {
			out.setByte(i, out.getByte(i) ^ msg.getKey());
		}
	}

	ctx.pipeline().remove(this);
}