Пример #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 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());
 }
Пример #3
0
 @Test
 public void testGetAndSetBytes() throws Exception {
   DynamicByteBuffer buf = DynamicByteBuffer.allocate(2);
   buf.put((byte) 1);
   buf.put((byte) 2);
   buf.flip();
   assertEquals((byte) 1, buf.get());
   assertEquals((byte) 2, buf.get());
   buf.flip();
   assertEquals((byte) 1, buf.getAt(0));
   assertEquals((byte) 2, buf.getAt(1));
   buf.flip();
 }
Пример #4
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]);
 }
Пример #5
0
 @Test
 public void testGetAndSetShorts() throws Exception {
   // tests reallocation of buffer automatically.
   DynamicByteBuffer buf = DynamicByteBuffer.allocate(2);
   buf.putShort((short) 1);
   buf.putShort((short) 2);
   buf.putShort((short) 3);
   buf.putShort((short) 4);
   buf.putShort((short) 5);
   buf.flip();
   assertEquals((short) 1, buf.getShort());
   assertEquals((short) 1, buf.getShortAt(0));
   assertEquals((short) 2, buf.getShortAt(2));
   assertEquals((short) 3, buf.getShortAt(4));
   assertEquals((short) 2, buf.getShort());
   assertEquals((short) 3, buf.getShort());
   assertEquals((short) 4, buf.getShort());
   assertEquals((short) 5, buf.getShort());
 }
Пример #6
0
 @Test
 public void testGetAndSetVarious() throws Exception {
   // tests reallocation of buffer automatically.
   DynamicByteBuffer buf = DynamicByteBuffer.allocate(2);
   buf.put((byte) 1);
   buf.putShort((short) 2);
   buf.putInt(3);
   buf.putLong(4);
   String test = "this";
   buf.putString(test, Charset.defaultCharset());
   buf.put((byte) 0);
   buf.putPrefixedString(1, "abcde", Charset.defaultCharset());
   buf.flip();
   assertEquals((byte) 1, buf.get());
   assertEquals((short) 2, buf.getShort());
   assertEquals(3, buf.getInt());
   assertEquals(4, buf.getLong());
   assertEquals(test, buf.getString(Charset.defaultCharset()));
   assertEquals("abcde", buf.getPrefixedString(1, Charset.defaultCharset()));
 }
Пример #7
0
 @Test
 public void testGetAndSetStringsInASCII() throws Exception {
   // tests reallocation of buffer automatically.
   DynamicByteBuffer buf = DynamicByteBuffer.allocate(2);
   String test = "this";
   String test1 = "is";
   String test2 = "a";
   String test3 = "story";
   Charset cs = Charset.forName("ASCII");
   buf.putString(test, cs);
   buf.put((byte) 0);
   buf.putPrefixedString(2, "abcde", cs);
   buf.putPrefixedString(2, test1, cs);
   buf.putPrefixedString(2, test2, cs);
   buf.putPrefixedString(2, test3, cs);
   buf.flip();
   assertEquals(test, buf.getString(cs));
   assertEquals("abcde", buf.getPrefixedString(2, cs));
   assertEquals(test1, buf.getPrefixedString(2, cs));
   assertEquals(test2, buf.getPrefixedString(2, cs));
   assertEquals(test3, buf.getPrefixedString(2, cs));
 }
Пример #8
0
 @Test
 public void testGetAndSetPrefixStringsInUTF16() throws Exception {
   // tests reallocation of buffer automatically.
   DynamicByteBuffer buf = DynamicByteBuffer.allocate(2);
   Charset cs = Charset.forName("UTF-16");
   String test = "this";
   String test1 = "is";
   String test2 = "a";
   String test3 = "story";
   buf.putPrefixedString(2, test, cs);
   buf.putPrefixedString(2, test1, cs);
   buf.putPrefixedString(2, test2, cs);
   buf.putPrefixedString(2, test3, cs);
   buf.put((byte) 0);
   buf.putInt(100);
   buf.flip();
   assertEquals(test, buf.getPrefixedString(2, cs));
   assertEquals(test1, buf.getPrefixedString(2, cs));
   assertEquals(test2, buf.getPrefixedString(2, cs));
   assertEquals(test3, buf.getPrefixedString(2, cs));
   assertEquals((byte) 0, buf.get());
   assertEquals(100, buf.getInt());
 }
Пример #9
0
 @Test(expected = BufferUnderflowException.class)
 public void testGetAndSetGetLongException3() {
   DynamicByteBuffer buf = DynamicByteBuffer.allocate(0);
   buf.getLong();
 }
Пример #10
0
 @Test(expected = BufferOverflowException.class)
 public void testGetAndSetPutLongException() {
   DynamicByteBuffer buf = DynamicByteBuffer.allocate(0);
   buf.setAutoExpand(false);
   buf.putLong(121L);
 }
Пример #11
0
 @Test(expected = BufferUnderflowException.class)
 public void testGetAndSetGetMediumIntException3() {
   DynamicByteBuffer buf = DynamicByteBuffer.allocate(0);
   buf.getUnsignedMediumInt();
 }
Пример #12
0
 @Test(expected = BufferOverflowException.class)
 public void testGetAndSetPutUnsignedIntException() throws Exception {
   DynamicByteBuffer buf = DynamicByteBuffer.allocate(0);
   buf.setAutoExpand(false);
   buf.putUnsignedInt(121);
 }