Пример #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());
  }
Пример #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());
  }
Пример #3
0
 @Test
 public void testGetAndSetSegment() throws Exception {
   // tests reallocation of buffer automatically.
   byte[] source = new byte[] {(byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5, (byte) 6};
   DynamicByteBuffer buf = DynamicByteBuffer.allocate(2);
   buf.put(source, 1, 4); // put 2,3,4,5 into buffer
   buf.flip();
   assertEquals(4, buf.remaining());
   byte[] dest = new byte[3];
   buf.get(dest, 1, 2); // get 3,4 into dest, start position at 1
   assertEquals((byte) 0, dest[0]);
   assertEquals((byte) 2, dest[1]);
   assertEquals((byte) 3, dest[2]);
 }