📄 iobuffertest.java
字号:
buf.clear(); assertEquals('E', buf.get(0)); assertEquals('F', buf.get(1)); assertEquals('C', buf.get(2)); // C may not be overwritten // UTF-16: We specify byte order to omit BOM. encoder = Charset.forName("UTF-16BE").newEncoder(); buf.clear(); buf.putString("ABC", encoder); assertEquals(6, buf.position()); buf.clear(); assertEquals(0, buf.get(0)); assertEquals('A', buf.get(1)); assertEquals(0, buf.get(2)); assertEquals('B', buf.get(3)); assertEquals(0, buf.get(4)); assertEquals('C', buf.get(5)); buf.putString("D", 10, encoder); assertEquals(10, buf.position()); buf.clear(); assertEquals(0, buf.get(0)); assertEquals('D', buf.get(1)); assertEquals(0, buf.get(2)); assertEquals(0, buf.get(3)); buf.putString("EFG", 4, encoder); assertEquals(4, buf.position()); buf.clear(); assertEquals(0, buf.get(0)); assertEquals('E', buf.get(1)); assertEquals(0, buf.get(2)); assertEquals('F', buf.get(3)); assertEquals(0, buf.get(4)); // C may not be overwritten assertEquals('C', buf.get(5)); // C may not be overwritten // Test putting an emptry string buf.putString("", encoder); assertEquals(0, buf.position()); buf.putString("", 4, encoder); assertEquals(4, buf.position()); assertEquals(0, buf.get(0)); assertEquals(0, buf.get(1)); } @Test public void testGetPrefixedString() throws Exception { IoBuffer buf = IoBuffer.allocate(16); CharsetEncoder encoder; CharsetDecoder decoder; encoder = Charset.forName("ISO-8859-1").newEncoder(); decoder = Charset.forName("ISO-8859-1").newDecoder(); buf.putShort((short) 3); buf.putString("ABCD", encoder); buf.clear(); assertEquals("ABC", buf.getPrefixedString(decoder)); } @Test public void testPutPrefixedString() throws Exception { CharsetEncoder encoder; IoBuffer buf = IoBuffer.allocate(16); buf.fillAndReset(buf.remaining()); encoder = Charset.forName("ISO-8859-1").newEncoder(); // Without autoExpand buf.putPrefixedString("ABC", encoder); assertEquals(5, buf.position()); assertEquals(0, buf.get(0)); assertEquals(3, buf.get(1)); assertEquals('A', buf.get(2)); assertEquals('B', buf.get(3)); assertEquals('C', buf.get(4)); buf.clear(); try { buf.putPrefixedString("123456789012345", encoder); fail(); } catch (BufferOverflowException e) { // Expected an Exception, signifies test success assertTrue(true); } // With autoExpand buf.clear(); buf.setAutoExpand(true); buf.putPrefixedString("123456789012345", encoder); assertEquals(17, buf.position()); assertEquals(0, buf.get(0)); assertEquals(15, buf.get(1)); assertEquals('1', buf.get(2)); assertEquals('2', buf.get(3)); assertEquals('3', buf.get(4)); assertEquals('4', buf.get(5)); assertEquals('5', buf.get(6)); assertEquals('6', buf.get(7)); assertEquals('7', buf.get(8)); assertEquals('8', buf.get(9)); assertEquals('9', buf.get(10)); assertEquals('0', buf.get(11)); assertEquals('1', buf.get(12)); assertEquals('2', buf.get(13)); assertEquals('3', buf.get(14)); assertEquals('4', buf.get(15)); assertEquals('5', buf.get(16)); } @Test public void testPutPrefixedStringWithPrefixLength() throws Exception { CharsetEncoder encoder = Charset.forName("ISO-8859-1").newEncoder(); IoBuffer buf = IoBuffer.allocate(16).sweep().setAutoExpand(true); buf.putPrefixedString("A", 1, encoder); assertEquals(2, buf.position()); assertEquals(1, buf.get(0)); assertEquals('A', buf.get(1)); buf.sweep(); buf.putPrefixedString("A", 2, encoder); assertEquals(3, buf.position()); assertEquals(0, buf.get(0)); assertEquals(1, buf.get(1)); assertEquals('A', buf.get(2)); buf.sweep(); buf.putPrefixedString("A", 4, encoder); assertEquals(5, buf.position()); assertEquals(0, buf.get(0)); assertEquals(0, buf.get(1)); assertEquals(0, buf.get(2)); assertEquals(1, buf.get(3)); assertEquals('A', buf.get(4)); } @Test public void testPutPrefixedStringWithPadding() throws Exception { CharsetEncoder encoder = Charset.forName("ISO-8859-1").newEncoder(); IoBuffer buf = IoBuffer.allocate(16).sweep().setAutoExpand(true); buf.putPrefixedString("A", 1, 2, (byte) 32, encoder); assertEquals(3, buf.position()); assertEquals(2, buf.get(0)); assertEquals('A', buf.get(1)); assertEquals(' ', buf.get(2)); buf.sweep(); buf.putPrefixedString("A", 1, 4, (byte) 32, encoder); assertEquals(5, buf.position()); assertEquals(4, buf.get(0)); assertEquals('A', buf.get(1)); assertEquals(' ', buf.get(2)); assertEquals(' ', buf.get(3)); assertEquals(' ', buf.get(4)); } @Test public void testWideUtf8Characters() throws Exception { Runnable r = new Runnable() { public void run() { IoBuffer buffer = IoBuffer.allocate(1); buffer.setAutoExpand(true); Charset charset = Charset.forName("UTF-8"); CharsetEncoder encoder = charset.newEncoder(); for (int i = 0; i < 5; i++) { try { buffer.putString("\u89d2", encoder); buffer.putPrefixedString("\u89d2", encoder); } catch (CharacterCodingException e) { fail(e.getMessage()); } } } }; Thread t = new Thread(r); t.setDaemon(true); t.start(); for (int i = 0; i < 50; i++) { Thread.sleep(100); if (!t.isAlive()) { break; } } if (t.isAlive()) { t.interrupt(); fail("Went into endless loop trying to encode character"); } } @Test public void testObjectSerialization() throws Exception { IoBuffer buf = IoBuffer.allocate(16); buf.setAutoExpand(true); List<Object> o = new ArrayList<Object>(); o.add(new Date()); o.add(long.class); // Test writing an object. buf.putObject(o); // Test reading an object. buf.clear(); Object o2 = buf.getObject(); assertEquals(o, o2); // This assertion is just to make sure that deserialization occurred. assertNotSame(o, o2); } @Test public void testInheritedObjectSerialization() throws Exception { IoBuffer buf = IoBuffer.allocate(16); buf.setAutoExpand(true); Bar expected = new Bar(); expected.setFooValue(0x12345678); expected.setBarValue(0x90ABCDEF); // Test writing an object. buf.putObject(expected); // Test reading an object. buf.clear(); Bar actual = (Bar) buf.getObject(); assertSame(Bar.class, actual.getClass()); assertEquals(expected.getFooValue(), actual.getFooValue()); assertEquals(expected.getBarValue(), actual.getBarValue()); // This assertion is just to make sure that deserialization occurred. assertNotSame(expected, actual); } @Test public void testSweepWithZeros() throws Exception { IoBuffer buf = IoBuffer.allocate(4); buf.putInt(0xdeadbeef); buf.clear(); assertEquals(0xdeadbeef, buf.getInt()); assertEquals(4, buf.position()); assertEquals(4, buf.limit()); buf.sweep(); assertEquals(0, buf.position()); assertEquals(4, buf.limit()); assertEquals(0x0, buf.getInt()); } @Test public void testSweepNonZeros() throws Exception { IoBuffer buf = IoBuffer.allocate(4); buf.putInt(0xdeadbeef); buf.clear(); assertEquals(0xdeadbeef, buf.getInt()); assertEquals(4, buf.position()); assertEquals(4, buf.limit()); buf.sweep((byte) 0x45); assertEquals(0, buf.position()); assertEquals(4, buf.limit()); assertEquals(0x45454545, buf.getInt()); } @Test public void testWrapNioBuffer() throws Exception { ByteBuffer nioBuf = ByteBuffer.allocate(10); nioBuf.position(3); nioBuf.limit(7); IoBuffer buf = IoBuffer.wrap(nioBuf); assertEquals(3, buf.position()); assertEquals(7, buf.limit()); assertEquals(10, buf.capacity()); } @Test public void testWrapSubArray() throws Exception { byte[] array = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; IoBuffer buf = IoBuffer.wrap(array, 3, 4); assertEquals(3, buf.position()); assertEquals(7, buf.limit()); assertEquals(10, buf.capacity()); buf.clear(); assertEquals(0, buf.position()); assertEquals(10, buf.limit()); assertEquals(10, buf.capacity()); } @Test public void testDuplicate() throws Exception { IoBuffer original; IoBuffer duplicate; // Test if the buffer is duplicated correctly. original = IoBuffer.allocate(16).sweep(); original.position(4); original.limit(10); duplicate = original.duplicate(); original.put(4, (byte) 127); assertEquals(4, duplicate.position()); assertEquals(10, duplicate.limit()); assertEquals(16, duplicate.capacity()); assertNotSame(original.buf(), duplicate.buf()); assertSame(original.buf().array(), duplicate.buf().array()); assertEquals(127, duplicate.get(4)); // Test a duplicate of a duplicate. original = IoBuffer.allocate(16); duplicate = original.duplicate().duplicate(); assertNotSame(original.buf(), duplicate.buf()); assertSame(original.buf().array(), duplicate.buf().array()); // Try to expand. original = IoBuffer.allocate(16); original.setAutoExpand(true); duplicate = original.duplicate(); assertFalse(original.isAutoExpand()); try { original.setAutoExpand(true); fail("Derived buffers and their parent can't be expanded"); } catch (IllegalStateException e) { // Expected an Exception, signifies test success assertTrue(true); } try { duplicate.setAutoExpand(true); fail("Derived buffers and their parent can't be expanded"); } catch (IllegalStateException e) { // Expected an Exception, signifies test success assertTrue(true); } } @Test public void testSlice() throws Exception { IoBuffer original; IoBuffer slice; // Test if the buffer is sliced correctly. original = IoBuffer.allocate(16).sweep(); original.position(4); original.limit(10); slice = original.slice(); original.put(4, (byte) 127); assertEquals(0, slice.position()); assertEquals(6, slice.limit()); assertEquals(6, slice.capacity()); assertNotSame(original.buf(), slice.buf()); assertEquals(127, slice.get(0)); } @Test public void testReadOnlyBuffer() throws Exception { IoBuffer original; IoBuffer duplicate; // Test if the buffer is duplicated correctly. original = IoBuffer.allocate(16).sweep(); original.position(4); original.limit(10); duplicate = original.asReadOnlyBuffer(); original.put(4, (byte) 127); assertEquals(4, duplicate.position()); assertEquals(10, duplicate.limit()); assertEquals(16, duplicate.capacity()); assertNotSame(original.buf(), duplicate.buf()); assertEquals(127, duplicate.get(4)); // Try to expand.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -