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

📄 iobuffertest.java

📁 mina是以Java实现的一个开源的网络程序框架
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* *  Licensed to the Apache Software Foundation (ASF) under one *  or more contributor license agreements.  See the NOTICE file *  distributed with this work for additional information *  regarding copyright ownership.  The ASF licenses this file *  to you under the Apache License, Version 2.0 (the *  "License"); you may not use this file except in compliance *  with the License.  You may obtain a copy of the License at * *    http://www.apache.org/licenses/LICENSE-2.0 * *  Unless required by applicable law or agreed to in writing, *  software distributed under the License is distributed on an *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *  KIND, either express or implied.  See the License for the *  specific language governing permissions and limitations *  under the License. * */package org.apache.mina.core;import java.nio.BufferOverflowException;import java.nio.ByteBuffer;import java.nio.ByteOrder;import java.nio.ReadOnlyBufferException;import java.nio.charset.CharacterCodingException;import java.nio.charset.Charset;import java.nio.charset.CharsetDecoder;import java.nio.charset.CharsetEncoder;import java.util.ArrayList;import java.util.Date;import java.util.EnumSet;import java.util.List;import org.apache.mina.core.buffer.IoBuffer;import org.apache.mina.util.Bar;import org.junit.After;import org.junit.Before;import org.junit.Test;import static org.junit.Assert.assertFalse;import static org.junit.Assert.assertTrue;import static org.junit.Assert.assertEquals;import static org.junit.Assert.assertSame;import static org.junit.Assert.assertNotSame;import static org.junit.Assert.fail;/** * Tests {@link IoBuffer}. * * @author The Apache MINA Project (dev@mina.apache.org) * @version $Rev: 754874 $, $Date: 2009-03-16 12:04:01 +0100 (Mon, 16 Mar 2009) $ */public class IoBufferTest {    /* public static void main(String[] args) {        junit.textui.TestRunner.run(IoBufferTest.class);    } */    @Before    public void setUp() throws Exception {    }    @After    public void tearDown() throws Exception {    }    @Test    public void testAllocate() throws Exception {        for (int i = 10; i < 1048576 * 2; i = i * 11 / 10) // increase by 10%        {            IoBuffer buf = IoBuffer.allocate(i);            assertEquals(0, buf.position());            assertEquals(buf.capacity(), buf.remaining());            assertTrue(buf.capacity() >= i);            assertTrue(buf.capacity() < i * 2);        }    }    @Test    public void testAutoExpand() throws Exception {        IoBuffer buf = IoBuffer.allocate(1);        buf.put((byte) 0);        try {            buf.put((byte) 0);            fail("Buffer can't auto expand, with autoExpand property set at false");        } catch (BufferOverflowException e) {            // Expected Exception as auto expand property is false            assertTrue(true);        }        buf.setAutoExpand(true);        buf.put((byte) 0);        assertEquals(2, buf.position());        assertEquals(2, buf.limit());        assertEquals(2, buf.capacity());        buf.setAutoExpand(false);        try {            buf.put(3, (byte) 0);            fail("Buffer can't auto expand, with autoExpand property set at false");        } catch (IndexOutOfBoundsException e) {            // Expected Exception as auto expand property is false            assertTrue(true);        }        buf.setAutoExpand(true);        buf.put(3, (byte) 0);        assertEquals(2, buf.position());        assertEquals(4, buf.limit());        assertEquals(4, buf.capacity());        // Make sure the buffer is doubled up.        buf = IoBuffer.allocate(1).setAutoExpand(true);        int lastCapacity = buf.capacity();        for (int i = 0; i < 1048576; i ++) {            buf.put((byte) 0);            if (lastCapacity != buf.capacity()) {                assertEquals(lastCapacity * 2, buf.capacity());                lastCapacity = buf.capacity();            }        }    }    @Test    public void testAutoExpandMark() throws Exception {        IoBuffer buf = IoBuffer.allocate(4).setAutoExpand(true);        buf.put((byte) 0);        buf.put((byte) 0);        buf.put((byte) 0);        // Position should be 3 when we reset this buffer.        buf.mark();        // Overflow it        buf.put((byte) 0);        buf.put((byte) 0);        assertEquals(5, buf.position());        buf.reset();        assertEquals(3, buf.position());    }    @Test    public void testAutoShrink() throws Exception {        IoBuffer buf = IoBuffer.allocate(8).setAutoShrink(true);        // Make sure the buffer doesn't shrink too much (less than the initial        // capacity.)        buf.sweep((byte) 1);        buf.fill(7);        buf.compact();        assertEquals(8, buf.capacity());        assertEquals(1, buf.position());        assertEquals(8, buf.limit());        buf.clear();        assertEquals(1, buf.get());        // Expand the buffer.        buf.capacity(32).clear();        assertEquals(32, buf.capacity());        // Make sure the buffer shrinks when only 1/4 is being used.        buf.sweep((byte) 1);        buf.fill(24);        buf.compact();        assertEquals(16, buf.capacity());        assertEquals(8, buf.position());        assertEquals(16, buf.limit());        buf.clear();        for (int i = 0; i < 8; i ++) {            assertEquals(1, buf.get());        }        // Expand the buffer.        buf.capacity(32).clear();        assertEquals(32, buf.capacity());        // Make sure the buffer shrinks when only 1/8 is being used.        buf.sweep((byte) 1);        buf.fill(28);        buf.compact();        assertEquals(8, buf.capacity());        assertEquals(4, buf.position());        assertEquals(8, buf.limit());        buf.clear();        for (int i = 0; i < 4; i ++) {            assertEquals(1, buf.get());        }        // Expand the buffer.        buf.capacity(32).clear();        assertEquals(32, buf.capacity());        // Make sure the buffer shrinks when 0 byte is being used.        buf.fill(32);        buf.compact();        assertEquals(8, buf.capacity());        assertEquals(0, buf.position());        assertEquals(8, buf.limit());        // Expand the buffer.        buf.capacity(32).clear();        assertEquals(32, buf.capacity());        // Make sure the buffer doesn't shrink when more than 1/4 is being used.        buf.sweep((byte) 1);        buf.fill(23);        buf.compact();        assertEquals(32, buf.capacity());        assertEquals(9, buf.position());        assertEquals(32, buf.limit());        buf.clear();        for (int i = 0; i < 9; i ++) {            assertEquals(1, buf.get());        }    }    @Test    public void testGetString() throws Exception {        IoBuffer buf = IoBuffer.allocate(16);        CharsetDecoder decoder;        Charset charset = Charset.forName("UTF-8");        buf.clear();        buf.putString("hello", charset.newEncoder());        buf.put((byte) 0);        buf.flip();        assertEquals("hello", buf.getString(charset.newDecoder()));        buf.clear();        buf.putString("hello", charset.newEncoder());        buf.flip();        assertEquals("hello", buf.getString(charset.newDecoder()));        decoder = Charset.forName("ISO-8859-1").newDecoder();        buf.clear();        buf.put((byte) 'A');        buf.put((byte) 'B');        buf.put((byte) 'C');        buf.put((byte) 0);        buf.position(0);        assertEquals("ABC", buf.getString(decoder));        assertEquals(4, buf.position());        buf.position(0);        buf.limit(1);        assertEquals("A", buf.getString(decoder));        assertEquals(1, buf.position());        buf.clear();        assertEquals("ABC", buf.getString(10, decoder));        assertEquals(10, buf.position());        buf.clear();        assertEquals("A", buf.getString(1, decoder));        assertEquals(1, buf.position());        // Test a trailing garbage        buf.clear();        buf.put((byte) 'A');        buf.put((byte) 'B');        buf.put((byte) 0);        buf.put((byte) 'C');        buf.position(0);        assertEquals("AB", buf.getString(4, decoder));        assertEquals(4, buf.position());        buf.clear();        buf.fillAndReset(buf.limit());        decoder = Charset.forName("UTF-16").newDecoder();        buf.put((byte) 0);        buf.put((byte) 'A');        buf.put((byte) 0);        buf.put((byte) 'B');        buf.put((byte) 0);        buf.put((byte) 'C');        buf.put((byte) 0);        buf.put((byte) 0);        buf.position(0);        assertEquals("ABC", buf.getString(decoder));        assertEquals(8, buf.position());        buf.position(0);        buf.limit(2);        assertEquals("A", buf.getString(decoder));        assertEquals(2, buf.position());        buf.position(0);        buf.limit(3);        assertEquals("A", buf.getString(decoder));        assertEquals(2, buf.position());        buf.clear();        assertEquals("ABC", buf.getString(10, decoder));        assertEquals(10, buf.position());        buf.clear();        assertEquals("A", buf.getString(2, decoder));        assertEquals(2, buf.position());        buf.clear();        try {            buf.getString(1, decoder);            fail();        } catch (IllegalArgumentException e) {            // Expected an Exception, signifies test success            assertTrue(true);        }        // Test getting strings from an empty buffer.        buf.clear();        buf.limit(0);        assertEquals("", buf.getString(decoder));        assertEquals("", buf.getString(2, decoder));        // Test getting strings from non-empty buffer which is filled with 0x00        buf.clear();        buf.putInt(0);        buf.clear();        buf.limit(4);        assertEquals("", buf.getString(decoder));        assertEquals(2, buf.position());        assertEquals(4, buf.limit());        buf.position(0);        assertEquals("", buf.getString(2, decoder));        assertEquals(2, buf.position());        assertEquals(4, buf.limit());    }    @Test    public void testGetStringWithFailure() throws Exception {        String test = "\u30b3\u30e1\u30f3\u30c8\u7de8\u96c6";        IoBuffer buffer = IoBuffer.wrap(test.getBytes("Shift_JIS"));        // Make sure the limit doesn't change when an exception arose.        int oldLimit = buffer.limit();        int oldPos = buffer.position();        try {            buffer.getString(3, Charset.forName("ASCII").newDecoder());            fail();        } catch (Exception e) {            assertEquals(oldLimit, buffer.limit());            assertEquals(oldPos, buffer.position());        }        try {            buffer.getString(Charset.forName("ASCII").newDecoder());            fail();        } catch (Exception e) {            assertEquals(oldLimit, buffer.limit());            assertEquals(oldPos, buffer.position());        }    }    @Test    public void testPutString() throws Exception {        CharsetEncoder encoder;        IoBuffer buf = IoBuffer.allocate(16);        encoder = Charset.forName("ISO-8859-1").newEncoder();        buf.putString("ABC", encoder);        assertEquals(3, buf.position());        buf.clear();        assertEquals('A', buf.get(0));        assertEquals('B', buf.get(1));        assertEquals('C', buf.get(2));        buf.putString("D", 5, encoder);        assertEquals(5, buf.position());        buf.clear();        assertEquals('D', buf.get(0));        assertEquals(0, buf.get(1));        buf.putString("EFG", 2, encoder);        assertEquals(2, buf.position());

⌨️ 快捷键说明

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