Example #1
0
  /**
   * Puts {@code numBits} into the buffer with the value {@code value}.
   *
   * @param numBits The number of bits to put into the buffer.
   * @param value The value.
   * @throws IllegalStateException if the builder is not in bit access mode.
   * @throws IllegalArgumentException if the number of bits is not between 1 and 31 inclusive.
   */
  public void putBits(int numBits, int value) {
    if (numBits <= 0 || numBits > 32) {
      throw new IllegalArgumentException("Number of bits must be between 1 and 31 inclusive");
    }

    checkBitAccess();

    int bytePos = bitIndex >> 3;
    int bitOffset = 8 - (bitIndex & 7);
    bitIndex += numBits;

    int requiredSpace = bytePos - buffer.writerIndex() + 1;
    requiredSpace += (numBits + 7) / 8;
    buffer.ensureWritable(requiredSpace);

    for (; numBits > bitOffset; bitOffset = 8) {
      int tmp = buffer.getByte(bytePos);
      tmp &= ~BITMASKS[bitOffset];
      tmp |= (value >> (numBits - bitOffset)) & BITMASKS[bitOffset];
      buffer.setByte(bytePos++, tmp);
      numBits -= bitOffset;
    }
    if (numBits == bitOffset) {
      int tmp = buffer.getByte(bytePos);
      tmp &= ~BITMASKS[bitOffset];
      tmp |= value & BITMASKS[bitOffset];
      buffer.setByte(bytePos, tmp);
    } else {
      int tmp = buffer.getByte(bytePos);
      tmp &= ~(BITMASKS[numBits] << (bitOffset - numBits));
      tmp |= (value & BITMASKS[numBits]) << (bitOffset - numBits);
      buffer.setByte(bytePos, tmp);
    }
  }
  @Override
  protected void encode(ChannelHandlerContext ctx, MessageBuilder msg, ByteBuf out)
      throws Exception {

    // Generate a new encryption key using ISAAC, encode the message and
    // write it to the client.
    ByteBuf internal = msg.buffer();
    int value = internal.getByte(0) + encryptor.getKey();
    internal.setByte(0, value);
    out.writeBytes(internal);
  }
 /**
  * In-place parsing of a hex encoded binary string.
  *
  * <p>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;
 }
 private void unmask(ByteBuf frame) {
   for (int i = frame.readerIndex(); i < frame.writerIndex(); i++) {
     frame.setByte(i, frame.getByte(i) ^ maskingKey.getByte(i % 4));
   }
 }
Example #5
0
 @Override
 public ByteBuf setByte(int index, int value) {
   return buf.setByte(index, value);
 }
Example #6
0
 /**
  * Sets the {@code byte} at position {@code pos} in the Buffer to the value {@code b}.
  *
  * <p>The buffer will expand as necessary to accommodate any value written.
  */
 public Buffer setByte(int pos, byte b) {
   ensureWritable(pos, 1);
   buffer.setByte(pos, b);
   return this;
 }
 @Override
 public ByteBuf setByte(int var1, int var2) {
   return a.setByte(var1, var2);
 }