@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(); }
@Test public void testGetAndSetPut() throws Exception { DynamicByteBuffer buf = new DynamicByteBuffer(); byte b1 = (byte) -128; byte b2 = (byte) 127; byte b3 = (byte) 128; byte b4 = (byte) 12; byte b5 = (byte) 13; buf.put(b1); buf.put(b2); buf.put(b3); buf.flip(); assertEquals(b1, buf.get()); assertEquals(b2, buf.get()); assertEquals(b3, buf.get()); buf.putAt(0, b4); buf.putAt(1, b5); assertEquals(b4, buf.getAt(0)); assertEquals(b5, buf.getAt(1)); }
@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]); }
@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())); }
@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()); }
@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()); }