コード例 #1
0
  /**
   * Verify that the computed checksum is the same whether the buffer is backed by an array or not
   * when the checksum is computed for only a region of the buffer (native heap buffer version).
   */
  public void test_checksum05_direct() {

    byte[] data = new byte[100];
    r.nextBytes(data);

    Adler32 adler32 = new Adler32();
    adler32.update(data, 20, 100 - 10 - 20);
    final int expectedChecksum = (int) adler32.getValue();

    assertEquals(expectedChecksum, chk.checksum(ByteBuffer.wrap(data), 20, data.length - 10));

    ByteBuffer direct = ByteBuffer.allocateDirect(data.length);
    direct.put(data);
    assertEquals(expectedChecksum, chk.checksum(direct, 20, data.length - 10));
  }
コード例 #2
0
  /** Test verifies that the mark, position and limit are unchanged by the checksum operation. */
  public void test_checksum03() {

    byte[] data = new byte[100];
    r.nextBytes(data);

    ByteBuffer buf = ByteBuffer.wrap(data);
    // set the limit.
    buf.limit(20);
    /*
     * set the mark (we have to choose a mark less than the position we will
     * set and test below or the mark will be discarded when we set the
     * position).
     */
    buf.position(9);
    buf.mark();
    // set the position.
    buf.position(12);
    chk.checksum(buf, 0, data.length);
    // verify limit unchanged.
    assertEquals(20, buf.limit());
    // verify position unchanged.
    assertEquals(12, buf.position());
    // reset the buffer to the mark and verify the mark was not changed.
    buf.reset();
    assertEquals(9, buf.position());
  }
コード例 #3
0
  /** Test verifies that only the specified region of the buffer is used to compute the checksum. */
  public void test_checksum02() {

    byte[] data = new byte[100];
    r.nextBytes(data);

    Adler32 adler32 = new Adler32();
    adler32.update(data, 10, 90);
    final int expectedChecksum = (int) adler32.getValue();

    assertEquals(expectedChecksum, chk.checksum(ByteBuffer.wrap(data), 10, data.length));
  }