前面介绍了缓冲区4个核心技术点:capacity、limit、position和mark,根据这4个技术点,可以设计出以下7个实验。
1)缓冲区的capacity不能为负数,缓冲区的limit不能为负数,缓冲区的position不能为负数。
2)position不能大于其limit。
3)limit不能大于其capacity。
4)如果定义了mark,则在将position或limit调整为小于该mark的值时,该mark被丢弃。
5)如果未定义mark,那么调用reset()方法将导致抛出InvalidMarkException异常。
6)如果position大于新的limit,则position的值就是新limit的值。
7)当limit和position值一样时,在指定的position写入数据时会出现异常,因为此位置是被限制的。
验证:缓冲区的capacity不能为负数,缓冲区的limit不能为负数,缓冲区的position不能为负数。
首先测试一下“缓冲区的capacity不能为负数”,需要使用allocate()方法开辟出指定空间大小的缓冲区,示例代码如下:
public class Test1_1 { public static void main(String[] args) { try { ByteBuffer bytebuffer = ByteBuffer.allocate(-1); } catch (IllegalArgumentException e) { System.out.println("ByteBuffer容量capacity大小不能为负数"); } } }
allocate(int capacity)方法分配一个新的缓冲区。
程序运行结果如下:
ByteBuffer容量capacity大小不能为负数
然后测试一下“缓冲区的limit不能为负数”,示例代码如下:
public class Test1_2 { public static void main(String[] args) { byte[] byteArray = new byte[] { 1, 2, 3 }; ByteBuffer bytebuffer = ByteBuffer.wrap(byteArray); try { bytebuffer = (ByteBuffer) bytebuffer.limit(-1); } catch (IllegalArgumentException e) { System.out.println("ByteBuffer限制limit大小不能为负数"); } } }
程序运行结果如下:
ByteBuffer限制limit大小不能为负数
最后测试一下“缓冲区的position不能为负数”,示例代码如下:
public class Test1_3 { public static void main(String[] args) { byte[] byteArray = new byte[] { 1, 2, 3 }; ByteBuffer bytebuffer = ByteBuffer.wrap(byteArray); try { bytebuffer = (ByteBuffer) bytebuffer.position(-1); } catch (IllegalArgumentException e) { System.out.println("ByteBuffer位置position大小不能为负数"); } } }
程序运行结果如下:
ByteBuffer位置position大小不能为负数
验证:position不能大于其limit。
示例代码如下:
public class Test2 { public static void main(String[] args) { byte[] byteArray = new byte[] { 1, 2, 3 }; ByteBuffer bytebuffer = ByteBuffer.wrap(byteArray); bytebuffer.limit(2); try { bytebuffer.position(3); } catch (IllegalArgumentException e) { System.out.println("ByteBuffer的position位置不能大于其limit限制"); } } }
程序运行结果如下:
ByteBuffer的position位置不能大于其limit限制
验证:limit不能大于其capacity。
示例代码如下:
public class Test3 { public static void main(String[] args) { byte[] byteArray = new byte[] { 1, 2, 3 }; ByteBuffer bytebuffer = ByteBuffer.wrap(byteArray); try { bytebuffer.limit(100); } catch (IllegalArgumentException e) { System.out.println("ByteBuffer的limit不能大于其capacity容量"); } } }
程序运行结果如下:
ByteBuffer的limit不能大于其capacity容量
验证:如果定义了mark,则在将position或limit调整为小于该mark的值时,该mark被丢弃。
在此处将第4条拆分成4点来分别进行验证。
1)如果定义了mark,则在将position调整为不小于该mark的值时,该mark不丢弃。
2)如果定义了mark,则在将position调整为小于该mark的值时,该mark被丢弃。
3)如果定义了mark,则在将limit调整为不小于该mark的值时,该mark不丢弃。
4)如果定义了mark,则在将limit调整为小于该mark的值时,该mark被丢弃。
首先验证一下“如果定义了mark,则在将position调整为不小于该mark的值时,该mark不丢弃”,示例代码如下:
public class Test4_1 { public static void main(String[] args) { byte[] byteArray = new byte[] { 1, 2, 3 }; ByteBuffer bytebuffer = ByteBuffer.wrap(byteArray); bytebuffer.position(1); bytebuffer.mark(); System.out.println("bytebuffer在" + bytebuffer.position() + "位置设置 mark标记"); bytebuffer.position(2); bytebuffer.reset(); System.out.println(); System.out.println("bytebuffer回到" + bytebuffer.position() + "位置"); } }
程序运行结果如下:
bytebuffer在1位置设置mark标记 bytebuffer回到1位置
然后验证一下“如果定义了mark,则在将position调整为小于该mark的值时,该mark将被丢弃”,示例代码如下:
public class Test4_2 { public static void main(String[] args) { byte[] byteArray = new byte[] { 1, 2, 3 }; ByteBuffer bytebuffer = ByteBuffer.wrap(byteArray); bytebuffer.position(2); bytebuffer.mark(); bytebuffer.position(1); try { bytebuffer.reset(); } catch (InvalidMarkException e) { System.out.println("bytebuffer的mark标记无效"); } } }
程序运行结果如下:
bytebuffer的mark标记无效
接着验证一下“如果定义了mark,则在将limit调整为不小于该mark的值时,该mark不丢弃”,示例代码如下:
public class Test4_3 { public static void main(String[] args) { byte[] byteArray = new byte[] { 1, 2, 3 }; ByteBuffer byteBuffer = ByteBuffer.wrap(byteArray); System.out.println("A byteBuffer position=" + byteBuffer.position() + " limit=" + byteBuffer.limit()); System.out.println(); byteBuffer.position(2); byteBuffer.mark(); System.out.println("B byteBuffer position=" + byteBuffer.position() + " limit=" + byteBuffer.limit()); byteBuffer.position(3); byteBuffer.limit(3); System.out.println(); System.out.println("C byteBuffer position=" + byteBuffer.position() + " limit=" + byteBuffer.limit()); byteBuffer.reset(); System.out.println(); System.out.println("D byteBuffer position=" + byteBuffer.position() + " limit=" + byteBuffer.limit()); } }
程序运行结果如下:
A byteBuffer position=0 limit=3 B byteBuffer position=2 limit=3 C byteBuffer position=3 limit=3 D byteBuffer position=2 limit=3
最后验证一下“如果定义了mark,则在将limit调整为小于该mark的值时,该mark被丢弃”,示例代码如下:
public class Test4_4 { public static void main(String[] args) { byte[] byteArray = new byte[] { 1, 2, 3 }; ByteBuffer byteBuffer = ByteBuffer.wrap(byteArray); System.out.println("A byteBuffer position=" + byteBuffer.position() + " limit=" + byteBuffer.limit()); System.out.println(); byteBuffer.position(2); byteBuffer.mark(); System.out.println("B byteBuffer position=" + byteBuffer.position() + " limit=" + byteBuffer.limit()); byteBuffer.limit(1); System.out.println(); System.out.println("C byteBuffer position=" + byteBuffer.position() + " limit=" + byteBuffer.limit()); System.out.println(); try { byteBuffer.reset(); } catch (InvalidMarkException e) { System.out.println("byteBuffer mark丢失"); } } }
程序运行结果如下:
A byteBuffer position=0 limit=3 B byteBuffer position=2 limit=3 C byteBuffer position=1 limit=1 byteBuffer mark丢失
总结:limit和position不能小于mark,如果小于则mark丢弃。
验证:如果未定义mark,那么调用reset()方法将导致抛出InvalidMarkException异常。
示例代码如下:
public class Test5 { public static void main(String[] args) { byte[] byteArray = new byte[] { 1, 2, 3 }; ByteBuffer bytebuffer = ByteBuffer.wrap(byteArray); try { bytebuffer.reset(); } catch (InvalidMarkException e) { System.out.println("bytebuffer的mark标记无效"); } } }
程序运行结果如下:
bytebuffer的mark标记无效
验证:如果position大于新的limit,则position的值就是新limit的值。
示例代码如下:
public class Test6 { public static void main(String[] args) { byte[] byteArray = new byte[] { 1, 2, 3 }; ByteBuffer bytebuffer = ByteBuffer.wrap(byteArray); bytebuffer.position(3); System.out.println("bytebuffer limit(2)之前的位置:" + bytebuffer.position()); bytebuffer.limit(2); System.out.println(); System.out.println("bytebuffer limit(2)之后的位置:" + bytebuffer.position()); } }
程序运行结果如下:
bytebuffer limit(2)之前的位置:3 bytebuffer limit(2)之后的位置:2
验证:当limit和position值一样时,在指定的position写入数据时会出现异常,因为此位置是被限制的。
示例代码如下:
public class Test7 { public static void main(String[] args) { char[] charArray = new char[] { 'a', 'b', 'c', 'd' }; CharBuffer charBuffer = CharBuffer.wrap(charArray); System.out.println("A capacity()=" + charBuffer.capacity() + " limit()=" + charBuffer.limit() + " position()=" + charBuffer.position()); System.out.println(); charBuffer.position(1); charBuffer.limit(1); charBuffer.put("z"); } }
程序运行结果如下:
A capacity()=4 limit()=4 position()=0 Exception in thread "main" java.nio.BufferOverflowException at java.nio.CharBuffer.put(CharBuffer.java:922) at java.nio.CharBuffer.put(CharBuffer.java:950) at BufferAPITest.Details.Test7.main(Test7.java:15)