Exemplo 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());
  }
Exemplo n.º 2
0
  @Test
  public void testBuffers() throws Exception {
    // tests reallocation of buffer automatically.
    String test1 = "Test1";
    String test2 = "Test2";
    DynamicByteBuffer buf1 = DynamicByteBuffer.wrap(test1.getBytes());
    DynamicByteBuffer buf2 = DynamicByteBuffer.wrap(test2.getBytes());
    buf1.get();
    buf1.get();

    int p = buf1.position();
    buf1.skip(buf1.remaining());
    buf1.putBuffer(buf2);
    buf1.flip();
    buf1.position(p);
    buf1 = buf1.slice();

    // TODO: should test actual contents of these buffers
    assertEquals(8, buf1.remaining());
  }