/** * Test the addition of mixed order buffers */ @Test public void testAddMixedOrderBuffers() { ByteBuffer bb1 = ByteBuffer.allocate(5); bb1.order(ByteOrder.LITTLE_ENDIAN); bb1.put("012".getBytes()); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocateDirect(5); bb1.order(ByteOrder.BIG_ENDIAN); bb2.put("3456".getBytes()); bb2.flip(); IoBuffer ioBuffer = IoBuffer.newInstance(); ioBuffer.add(bb1, bb2); }
/** * Test the addition of mixed type buffers */ @Test public void testAddMixedTypeBuffers() { ByteBuffer bb1 = ByteBuffer.allocate(5); bb1.put("012".getBytes()); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocateDirect(5); bb2.put("3456".getBytes()); bb2.flip(); IoBuffer ioBuffer = IoBuffer.newInstance(); ioBuffer.add(bb1, bb2); }
/** * Test the position method over an emptyIoBuffer */ @Test public void testPositionIntEmptyBuffer() { IoBuffer ioBuffer = IoBuffer.newInstance(); ioBuffer.position(0); }
@Test public void testEquals() { String h = "Hello"; String w = "World"; IoBuffer hw1b = IoBuffer.wrap((h + w).getBytes()); IoBuffer wh1b = IoBuffer.wrap((w + h).getBytes()); IoBuffer hw2b = IoBuffer.newInstance(); hw2b.add(ByteBuffer.wrap(h.getBytes())); hw2b.add(ByteBuffer.wrap(w.getBytes())); assertEquals(hw2b, hw1b); Assert.assertThat(wh1b, is(not(hw1b))); } }
@Test public void testGet() { ByteBuffer bb1 = ByteBuffer.allocate(5); bb1.put("012".getBytes()); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocate(5); bb2.put("345".getBytes()); bb2.flip(); ByteBuffer bb3 = ByteBuffer.allocate(5); bb3.put("6789".getBytes()); bb3.flip(); IoBuffer ioBuffer = IoBuffer.newInstance(); ioBuffer.add(bb1, bb2).add(bb3); ioBuffer.position(2); ioBuffer.limit(8); byte block[] = new byte[6]; ioBuffer.get(block); byte seg[] = "234567".getBytes(); for (int i = 0; i < 6; i++) { assertEquals(seg[i], block[i]); } }
@Test public void testPut() { ByteBuffer bb1 = ByteBuffer.allocate(5); bb1.put("012".getBytes()); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocate(5); bb2.put("345".getBytes()); bb2.flip(); ByteBuffer bb3 = ByteBuffer.allocate(5); bb3.put("6789".getBytes()); bb3.flip(); IoBuffer ioBuffer = IoBuffer.newInstance(); ioBuffer.add(bb1, bb2).add(bb3); byte seq[] = "abcdefghij".getBytes(); ioBuffer.position(2); ioBuffer.put(seq, 3, 3); ioBuffer.rewind(); byte expected[] = "01def56789".getBytes(); for (int i = 0; i < 6; i++) { assertEquals(expected[i], ioBuffer.get(i)); } }
/** * Test the position method over an emptyIoBuffer */ @Test public void testPositionEmptyBuffer() { IoBuffer ioBuffer = IoBuffer.newInstance(); assertEquals(0, ioBuffer.position()); }
/** * Test the array method for a IoBuffer containing one empty ByteBuffer */ @Test public void testArrayEmptyByteBuffer() { IoBuffer ioBuffer = IoBuffer.newInstance(); byte[] array = ioBuffer.array(); assertNotNull(array); assertEquals(0, array.length); assertTrue(Arrays.equals(new byte[] {}, array)); }
@Test public void testCompact() { ByteBuffer bb1 = ByteBuffer.allocate(5); bb1.put("012".getBytes()); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocate(5); bb2.put("345".getBytes()); bb2.flip(); ByteBuffer bb3 = ByteBuffer.allocate(5); bb3.put("6789".getBytes()); bb3.flip(); IoBuffer ioBuffer = IoBuffer.newInstance(); ioBuffer.add(bb1, bb2).add(bb3); ioBuffer.position(2); ioBuffer.limit(8); ioBuffer.compact(); assertEquals(ioBuffer.capacity(), ioBuffer.limit()); assertEquals(6, ioBuffer.position()); byte seg[] = "234567".getBytes(); for (int i = 0; i < 6; i++) { assertEquals(seg[i], ioBuffer.get(i)); } }
@Test public void testSlice() { ByteBuffer bb1 = ByteBuffer.allocate(5); bb1.put("012".getBytes()); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocate(5); bb2.put("345".getBytes()); bb2.flip(); ByteBuffer bb3 = ByteBuffer.allocate(5); bb3.put("6789".getBytes()); bb3.flip(); IoBuffer ioBuffer = IoBuffer.newInstance(); ioBuffer.add(bb1, bb2).add(bb3); ioBuffer.position(2); ioBuffer.limit(8); IoBuffer slice = ioBuffer.slice(); assertEquals(6, slice.remaining()); assertEquals(0, slice.position()); assertEquals(6, slice.limit()); byte seg[] = "234567".getBytes(); for (int i = 0; i < 6; i++) { assertEquals(seg[i], slice.get(i)); } }
/** * Test the addition of 3 heap buffers, one being empty */ @Test public void testAddHeapBuffersOneEmpty() { ByteBuffer bb1 = ByteBuffer.allocate(5); bb1.put("012".getBytes()); bb1.flip(); ByteBuffer bb2 = ByteBuffer.allocate(0); ByteBuffer bb3 = ByteBuffer.allocate(5); bb3.put("3456".getBytes()); bb3.flip(); IoBuffer ioBuffer = IoBuffer.newInstance(); ioBuffer.add(bb1, bb2).add(bb3); assertEquals(0, ioBuffer.position()); assertEquals(7, ioBuffer.limit()); assertEquals(7, ioBuffer.capacity()); for (int i = 0; i < 7; i++) { assertTrue(ioBuffer.hasRemaining()); assertEquals("0123456".charAt(i), ioBuffer.get()); } try { ioBuffer.get(); fail(); } catch (BufferUnderflowException bufe) { assertTrue(true); } }
bb3.flip(); IoBuffer ioBuffer = IoBuffer.newInstance(); ioBuffer.add(bb1, bb2).add(bb3);