示例#1
0
  /**
   * Calculates the hash code of the specified buffer. This method is useful when implementing a new
   * buffer type.
   */
  public static int hashCode(ChannelBuffer buffer) {
    final int aLen = buffer.readableBytes();
    final int intCount = aLen >>> 2;
    final int byteCount = aLen & 3;

    int hashCode = 1;
    int arrayIndex = buffer.readerIndex();
    if (buffer.order() == BIG_ENDIAN) {
      for (int i = intCount; i > 0; i--) {
        hashCode = 31 * hashCode + buffer.getInt(arrayIndex);
        arrayIndex += 4;
      }
    } else {
      for (int i = intCount; i > 0; i--) {
        hashCode = 31 * hashCode + swapInt(buffer.getInt(arrayIndex));
        arrayIndex += 4;
      }
    }

    for (int i = byteCount; i > 0; i--) {
      hashCode = 31 * hashCode + buffer.getByte(arrayIndex++);
    }

    if (hashCode == 0) {
      hashCode = 1;
    }

    return hashCode;
  }
  @Test
  public void shouldForwardReadCallsBlindly() throws Exception {
    ChannelBuffer buf = createStrictMock(ChannelBuffer.class);
    expect(buf.readerIndex()).andReturn(0).anyTimes();
    expect(buf.writerIndex()).andReturn(0).anyTimes();
    expect(buf.capacity()).andReturn(0).anyTimes();

    expect(buf.getBytes(1, (GatheringByteChannel) null, 2)).andReturn(3);
    buf.getBytes(4, (OutputStream) null, 5);
    buf.getBytes(6, (byte[]) null, 7, 8);
    buf.getBytes(9, (ChannelBuffer) null, 10, 11);
    buf.getBytes(12, (ByteBuffer) null);
    expect(buf.getByte(13)).andReturn(Byte.valueOf((byte) 14));
    expect(buf.getShort(15)).andReturn(Short.valueOf((short) 16));
    expect(buf.getUnsignedMedium(17)).andReturn(18);
    expect(buf.getInt(19)).andReturn(20);
    expect(buf.getLong(21)).andReturn(22L);

    ByteBuffer bb = ByteBuffer.allocate(100);
    ByteBuffer[] bbs = {ByteBuffer.allocate(101), ByteBuffer.allocate(102)};

    expect(buf.toByteBuffer(23, 24)).andReturn(bb);
    expect(buf.toByteBuffers(25, 26)).andReturn(bbs);
    expect(buf.capacity()).andReturn(27);

    replay(buf);

    ChannelBuffer roBuf = unmodifiableBuffer(buf);
    assertEquals(3, roBuf.getBytes(1, (GatheringByteChannel) null, 2));
    roBuf.getBytes(4, (OutputStream) null, 5);
    roBuf.getBytes(6, (byte[]) null, 7, 8);
    roBuf.getBytes(9, (ChannelBuffer) null, 10, 11);
    roBuf.getBytes(12, (ByteBuffer) null);
    assertEquals((byte) 14, roBuf.getByte(13));
    assertEquals((short) 16, roBuf.getShort(15));
    assertEquals(18, roBuf.getUnsignedMedium(17));
    assertEquals(20, roBuf.getInt(19));
    assertEquals(22L, roBuf.getLong(21));

    ByteBuffer roBB = roBuf.toByteBuffer(23, 24);
    assertEquals(100, roBB.capacity());
    assertTrue(roBB.isReadOnly());

    ByteBuffer[] roBBs = roBuf.toByteBuffers(25, 26);
    assertEquals(2, roBBs.length);
    assertEquals(101, roBBs[0].capacity());
    assertTrue(roBBs[0].isReadOnly());
    assertEquals(102, roBBs[1].capacity());
    assertTrue(roBBs[1].isReadOnly());
    assertEquals(27, roBuf.capacity());

    verify(buf);
  }
示例#3
0
  /**
   * Compares the two specified buffers as described in {@link
   * ChannelBuffer#compareTo(ChannelBuffer)}. This method is useful when implementing a new buffer
   * type.
   */
  public static int compare(ChannelBuffer bufferA, ChannelBuffer bufferB) {
    final int aLen = bufferA.readableBytes();
    final int bLen = bufferB.readableBytes();
    final int minLength = Math.min(aLen, bLen);
    final int uintCount = minLength >>> 2;
    final int byteCount = minLength & 3;

    int aIndex = bufferA.readerIndex();
    int bIndex = bufferB.readerIndex();

    if (bufferA.order() == bufferB.order()) {
      for (int i = uintCount; i > 0; i--) {
        long va = bufferA.getUnsignedInt(aIndex);
        long vb = bufferB.getUnsignedInt(bIndex);
        if (va > vb) {
          return 1;
        } else if (va < vb) {
          return -1;
        }
        aIndex += 4;
        bIndex += 4;
      }
    } else {
      for (int i = uintCount; i > 0; i--) {
        long va = bufferA.getUnsignedInt(aIndex);
        long vb = swapInt(bufferB.getInt(bIndex)) & 0xFFFFFFFFL;
        if (va > vb) {
          return 1;
        } else if (va < vb) {
          return -1;
        }
        aIndex += 4;
        bIndex += 4;
      }
    }

    for (int i = byteCount; i > 0; i--) {
      short va = bufferA.getUnsignedByte(aIndex);
      short vb = bufferB.getUnsignedByte(bIndex);
      if (va > vb) {
        return 1;
      } else if (va < vb) {
        return -1;
      }
      aIndex++;
      bIndex++;
    }

    return aLen - bLen;
  }
示例#4
0
 public int getInt(int index) {
   return buffer.getInt(index);
 }