⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bytebuffertest.java

📁 apache 的一个socket框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        Assert.assertEquals(5, buf.position());        Assert.assertEquals(0, buf.get(0));        Assert.assertEquals(3, buf.get(1));        Assert.assertEquals('A', buf.get(2));        Assert.assertEquals('B', buf.get(3));        Assert.assertEquals('C', buf.get(4));        buf.clear();        try {            buf.putPrefixedString("123456789012345", encoder);            Assert.fail();        } catch (BufferOverflowException e) {            // OK        }        // With autoExpand        buf.clear();        buf.setAutoExpand(true);        buf.putPrefixedString("123456789012345", encoder);        Assert.assertEquals(17, buf.position());        Assert.assertEquals(0, buf.get(0));        Assert.assertEquals(15, buf.get(1));        Assert.assertEquals('1', buf.get(2));        Assert.assertEquals('2', buf.get(3));        Assert.assertEquals('3', buf.get(4));        Assert.assertEquals('4', buf.get(5));        Assert.assertEquals('5', buf.get(6));        Assert.assertEquals('6', buf.get(7));        Assert.assertEquals('7', buf.get(8));        Assert.assertEquals('8', buf.get(9));        Assert.assertEquals('9', buf.get(10));        Assert.assertEquals('0', buf.get(11));        Assert.assertEquals('1', buf.get(12));        Assert.assertEquals('2', buf.get(13));        Assert.assertEquals('3', buf.get(14));        Assert.assertEquals('4', buf.get(15));        Assert.assertEquals('5', buf.get(16));    }    public void testPutPrefixedStringWithPrefixLength() throws Exception {        CharsetEncoder encoder = Charset.forName("ISO-8859-1").newEncoder();        ByteBuffer buf = ByteBuffer.allocate(16).sweep().setAutoExpand(true);        buf.putPrefixedString("A", 1, encoder);        Assert.assertEquals(2, buf.position());        Assert.assertEquals(1, buf.get(0));        Assert.assertEquals('A', buf.get(1));        buf.sweep();        buf.putPrefixedString("A", 2, encoder);        Assert.assertEquals(3, buf.position());        Assert.assertEquals(0, buf.get(0));        Assert.assertEquals(1, buf.get(1));        Assert.assertEquals('A', buf.get(2));        buf.sweep();        buf.putPrefixedString("A", 4, encoder);        Assert.assertEquals(5, buf.position());        Assert.assertEquals(0, buf.get(0));        Assert.assertEquals(0, buf.get(1));        Assert.assertEquals(0, buf.get(2));        Assert.assertEquals(1, buf.get(3));        Assert.assertEquals('A', buf.get(4));    }    public void testPutPrefixedStringWithPadding() throws Exception {        CharsetEncoder encoder = Charset.forName("ISO-8859-1").newEncoder();        ByteBuffer buf = ByteBuffer.allocate(16).sweep().setAutoExpand(true);        buf.putPrefixedString("A", 1, 2, (byte) 32, encoder);        Assert.assertEquals(3, buf.position());        Assert.assertEquals(2, buf.get(0));        Assert.assertEquals('A', buf.get(1));        Assert.assertEquals(' ', buf.get(2));        buf.sweep();        buf.putPrefixedString("A", 1, 4, (byte) 32, encoder);        Assert.assertEquals(5, buf.position());        Assert.assertEquals(4, buf.get(0));        Assert.assertEquals('A', buf.get(1));        Assert.assertEquals(' ', buf.get(2));        Assert.assertEquals(' ', buf.get(3));        Assert.assertEquals(' ', buf.get(4));    }    public void testWideUtf8Characters() throws Exception {        Runnable r = new Runnable() {            public void run() {                ByteBuffer buffer = ByteBuffer.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);                    } 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");        }    }    public void testObjectSerialization() throws Exception {        ByteBuffer buf = ByteBuffer.allocate(16);        buf.setAutoExpand(true);        List<Object> o = new ArrayList<Object>();        o.add(new Date());        // Test writing an object.        buf.putObject(o);        // Test reading an object.        buf.clear();        Object o2 = buf.getObject();        Assert.assertEquals(o, o2);        // This assertion is just to make sure that deserialization occurred.        Assert.assertNotSame(o, o2);    }    public void testSweepWithZeros() throws Exception {        ByteBuffer buf = ByteBuffer.allocate(4);        buf.putInt(0xdeadbeef);        buf.clear();        Assert.assertEquals(0xdeadbeef, buf.getInt());        Assert.assertEquals(4, buf.position());        Assert.assertEquals(4, buf.limit());        buf.sweep();        Assert.assertEquals(0, buf.position());        Assert.assertEquals(4, buf.limit());        Assert.assertEquals(0x0, buf.getInt());    }    public void testSweepNonZeros() throws Exception {        ByteBuffer buf = ByteBuffer.allocate(4);        buf.putInt(0xdeadbeef);        buf.clear();        Assert.assertEquals(0xdeadbeef, buf.getInt());        Assert.assertEquals(4, buf.position());        Assert.assertEquals(4, buf.limit());        buf.sweep((byte) 0x45);        Assert.assertEquals(0, buf.position());        Assert.assertEquals(4, buf.limit());        Assert.assertEquals(0x45454545, buf.getInt());    }    public void testWrapNioBuffer() throws Exception {        java.nio.ByteBuffer nioBuf = java.nio.ByteBuffer.allocate(10);        nioBuf.position(3);        nioBuf.limit(7);        ByteBuffer buf = ByteBuffer.wrap(nioBuf);        Assert.assertEquals(3, buf.position());        Assert.assertEquals(7, buf.limit());        Assert.assertEquals(10, buf.capacity());    }    public void testWrapSubArray() throws Exception {        byte[] array = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };        ByteBuffer buf = ByteBuffer.wrap(array, 3, 4);        Assert.assertEquals(3, buf.position());        Assert.assertEquals(7, buf.limit());        Assert.assertEquals(10, buf.capacity());        buf.clear();        Assert.assertEquals(0, buf.position());        Assert.assertEquals(10, buf.limit());        Assert.assertEquals(10, buf.capacity());    }    public void testPoolExpiration() throws Exception {        PooledByteBufferAllocator allocator = (PooledByteBufferAllocator) ByteBuffer                .getAllocator();        // Make a buffer pooled.        ByteBuffer buf = ByteBuffer.allocate(16);        buf.release();        // Let everything flushed.        allocator.setTimeout(1);        Thread.sleep(2000);        // Make sure old buffers are flushed.        Assert.assertNotSame(buf, ByteBuffer.allocate(16));        // Make sure new buffers are not flushed.        allocator.setTimeout(10);        buf = ByteBuffer.allocate(16);        buf.release();        Thread.sleep(2000);        Assert.assertSame(buf.buf(), ByteBuffer.allocate(16).buf());        // Return to the default settings        allocator.setTimeout(60);    }    public void testAllocatorDisposal() throws Exception {        PooledByteBufferAllocator allocator = (PooledByteBufferAllocator) ByteBuffer                .getAllocator();        // dispose() should fail because the allocator is in use.        try {            allocator.dispose();            Assert.fail();        } catch (IllegalStateException e) {            // OK        }        // Change the allocator.        ByteBuffer.setAllocator(new PooledByteBufferAllocator());        // Dispose the old allocator.        allocator.dispose();        // Allocation request to the disposed allocator should fail.        try {            allocator.allocate(16, true);            Assert.fail();        } catch (IllegalStateException e) {            // OK        }    }    public void testDuplicate() throws Exception {        java.nio.ByteBuffer nioBuf;        ByteBuffer original;        ByteBuffer duplicate;        // Test if the buffer is duplicated correctly.        original = ByteBuffer.allocate(16).sweep();        nioBuf = original.buf();        original.position(4);        original.limit(10);        duplicate = original.duplicate();        original.put(4, (byte) 127);        Assert.assertEquals(4, duplicate.position());        Assert.assertEquals(10, duplicate.limit());        Assert.assertEquals(16, duplicate.capacity());        Assert.assertNotSame(original.buf(), duplicate.buf());        Assert.assertEquals(127, duplicate.get(4));        original.release();        duplicate.release();        //// Check if pooled correctly.        original = ByteBuffer.allocate(16);        Assert.assertSame(nioBuf, original.buf());        original.release();        // Try to release duplicate first.        original = ByteBuffer.allocate(16);        duplicate = original.duplicate();        duplicate.release();        original.release();        //// Check if pooled correctly.        original = ByteBuffer.allocate(16);        Assert.assertSame(nioBuf, original.buf());        original.release();        // Test a duplicate of a duplicate.        original = ByteBuffer.allocate(16);        duplicate = original.duplicate();        ByteBuffer anotherDuplicate = duplicate.duplicate();        anotherDuplicate.release();        original.release();        duplicate.release();        try {            duplicate.release();            Assert.fail();        } catch (IllegalStateException e) {            // OK        }        try {            anotherDuplicate.release();            Assert.fail();        } catch (IllegalStateException e) {            // OK        }        //// Check if pooled correctly.        original = ByteBuffer.allocate(16);        Assert.assertSame(nioBuf, original.buf());        original.release();        // Try to expand.        try {            original = ByteBuffer.allocate(16);            duplicate = original.duplicate();            duplicate.setAutoExpand(true);            duplicate.putString("A very very very very looooooong string",                    Charset.forName("ISO-8859-1").newEncoder());            Assert.fail();        } catch (IllegalStateException e) {            // OK        }    }    public void testSlice() throws Exception {        ByteBuffer original;        ByteBuffer slice;        // Test if the buffer is sliced correctly.        original = ByteBuffer.allocate(16).sweep();        original.position(4);        original.limit(10);        slice = original.slice();        original.put(4, (byte) 127);        Assert.assertEquals(0, slice.position());        Assert.assertEquals(6, slice.limit());        Assert.assertEquals(6, slice.capacity());        Assert.assertNotSame(original.buf(), slice.buf());        Assert.assertEquals(127, slice.get(0));        original.release();        slice.release();    }    public void testReadOnlyBuffer() throws Exception {        ByteBuffer original;        ByteBuffer duplicate;        // Test if the buffer is duplicated correctly.        original = ByteBuffer.allocate(16).sweep();        original.position(4);        original.limit(10);        duplicate = original.asReadOnlyBuffer();        original.put(4, (byte) 127);        Assert.assertEquals(4, duplicate.position());        Assert.assertEquals(10, duplicate.limit());        Assert.assertEquals(16, duplicate.capacity());        Assert.assertNotSame(original.buf(), duplicate.buf());        Assert.assertEquals(127, duplicate.get(4));        original.release();        duplicate.release();        // Try to expand.        try {            original = ByteBuffer.allocate(16);            duplicate = original.asReadOnlyBuffer();            duplicate.putString("A very very very very looooooong string",                    Charset.forName("ISO-8859-1").newEncoder());            Assert.fail();        } catch (ReadOnlyBufferException e) {            // OK        }    }    public void testGetUnsigned() throws Exception {        ByteBuffer buf = ByteBuffer.allocate(16);        buf.put((byte) 0xA4);        buf.put((byte) 0xD0);        buf.put((byte) 0xB3);        buf.put((byte) 0xCD);        buf.flip();        buf.order(ByteOrder.LITTLE_ENDIAN);        buf.mark();        Assert.assertEquals(0xA4, buf.getUnsigned());        buf.reset();        Assert.assertEquals(0xD0A4, buf.getUnsignedShort());        buf.reset();        Assert.assertEquals(0xCDB3D0A4L, buf.getUnsignedInt());    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -