Ejemplo n.º 1
0
  @Test
  public void testCompact() throws Exception {
    DynamicByteBuffer readBuffer = DynamicByteBuffer.allocate(512);

    byte[] bytes = new byte[521];
    DynamicByteBuffer data = DynamicByteBuffer.wrap(bytes);
    readBuffer.putBuffer(data);
    readBuffer.limit(data.position());
    int numberOfBytesToBeCopiedDuringCompact = readBuffer.remaining();
    readBuffer.compact(); // compact the read buffer
    Assert.assertEquals(numberOfBytesToBeCopiedDuringCompact, readBuffer.position());

    byte[] bytes1 = new byte[10];
    DynamicByteBuffer data1 = DynamicByteBuffer.wrap(bytes1);

    readBuffer.putBuffer(data1);
    readBuffer.limit(data1.position());

    numberOfBytesToBeCopiedDuringCompact = readBuffer.remaining();
    readBuffer.compact(); // compact the read buffer
    Assert.assertEquals(numberOfBytesToBeCopiedDuringCompact, readBuffer.position());

    readBuffer.position(10);
    numberOfBytesToBeCopiedDuringCompact = readBuffer.remaining();
    readBuffer.compact();
    Assert.assertEquals(numberOfBytesToBeCopiedDuringCompact, readBuffer.position());
  }
Ejemplo n.º 2
0
  @Test
  public void testGetAndSetUnsignedInt() throws Exception {
    DynamicByteBuffer buf = new DynamicByteBuffer();
    long MAX_UNSIGNED_INT = 4294967295L;

    buf.putUnsignedInt(MAX_UNSIGNED_INT);
    buf.putUnsignedInt(1);
    buf.putUnsignedInt(0);
    buf.putUnsignedInt(-1);
    buf.putUnsignedInt(MAX_UNSIGNED_INT + 1);

    assertEquals(buf.position(), 20);

    buf.flip();

    assertEquals(buf.position(), 0);
    assertEquals(buf.limit(), 20);

    assertEquals("test 1", MAX_UNSIGNED_INT, buf.getUnsignedInt());
    assertEquals("test 2", 1L, buf.getUnsignedInt());
    assertEquals("test 3", 0L, buf.getUnsignedInt());
    assertEquals("test 4", MAX_UNSIGNED_INT, buf.getUnsignedInt());
    assertEquals("test 5", 0L, buf.getUnsignedInt());

    assertEquals(buf.position(), 20);

    buf.putUnsignedIntAt(0, 121);
    buf.putUnsignedIntAt(4, 4000000000L);
    assertEquals(121L, buf.getUnsignedIntAt(0));
    assertEquals(4000000000L, buf.getUnsignedIntAt(4));

    assertEquals(buf.position(), 20);
  }
Ejemplo n.º 3
0
  /*
   * Short Tests
   */
  @Test
  public void testGetAndSetShort() throws Exception {
    DynamicByteBuffer buf = new DynamicByteBuffer();
    buf.putShort((short) 0);
    buf.putShort((short) 1);
    buf.putShort((short) 32767);
    buf.putShort((short) -32768);
    buf.flip();

    assertEquals(buf.position(), 0);
    assertEquals(buf.limit(), 8);

    assertEquals((short) 0, buf.getShort());
    assertEquals((short) 1, buf.getShort());
    assertEquals((short) 32767, buf.getShort());
    assertEquals((short) -32768, buf.getShort());

    assertEquals(buf.position(), 8);

    buf.putShortAt(0, (short) 121);
    buf.putShortAt(2, (short) 1000);
    assertEquals((short) 121, buf.getShortAt(0));
    assertEquals((short) 1000, buf.getShortAt(2));

    assertEquals(buf.position(), 8);
  }
Ejemplo n.º 4
0
  @Test
  public void testGetAndSetUnsigned() throws Exception {
    DynamicByteBuffer buf = new DynamicByteBuffer();
    buf.putUnsigned(0);
    buf.putUnsigned(255);
    buf.putUnsigned(256);
    buf.putUnsigned(-1);
    buf.flip();

    assertEquals(buf.position(), 0);
    assertEquals(buf.limit(), 4);

    assertEquals(0, buf.getUnsigned());
    assertEquals(255, buf.getUnsigned());
    assertEquals(0, buf.getUnsigned());
    assertEquals(255, buf.getUnsigned());

    assertEquals(buf.position(), 4);

    buf.putUnsignedAt(0, 121);
    buf.putUnsignedAt(1, 122);
    assertEquals(121, buf.getUnsignedAt(0));
    assertEquals(122, buf.getUnsignedAt(1));

    assertEquals(buf.position(), 4);
  }
Ejemplo n.º 5
0
 @Test
 public void testByteBufferAllocate() {
   /* Invariant: 0 <= mark <= position <= limit <= capacity */
   DynamicByteBuffer buf = DynamicByteBuffer.allocate(0);
   // assertEquals(0, buf.mark());
   assertEquals(0, buf.position());
   assertEquals(0, buf.limit());
   assertEquals(0, buf.capacity());
 }