java nio之Buffer

 

jdk1.4提供了新的io包,其中包括BufferBuffer是一种用于特定的基本类型数据的容器。 缓冲区是特定的基本类型元素的线性、有限序列。。我认为BufferJava nio框架的数据结构。所以只有对数据结构足够熟悉,才能在操作的时候不容易出错。

缓冲区的基本属性除其内容外还包括容量、限制、位置和标记位,它们分别是:

positionlimitcapacityMark四个属性的操作。在读写数据的操作中,这四个变量起着关键作用,真是稍不留神,就会谬以千里。幸好开发者为我们准备好了一些常用的方法,例如flip(),而且还准备了许多exception来欢迎一堆乱来的家伙。对这是个变量具体的操作自己看api doc就好了。

 

java.nio Buffer 的子类

 ByteBuffer           字节缓冲区。

 CharBuffer           字符缓冲区。

 DoubleBuffer           double 缓冲区。

 FloatBuffer           float 缓冲区。

 IntBuffer           int 缓冲区。

 LongBuffer           long 缓冲区。

 MappedByteBuffer           直接字节缓冲区,其内容是文件的内存映射区域。

 ShortBuffer           short 缓冲区。

 

此类的每个子类都定义了两种 get put 操作:

相对 操作读取或写入从当前位置开始的一个或多个元素,然后将该位置增加传输的元素的数量。如果请求的传输超过了限制,则相对 get 操作将抛出 BufferUnderflowException,相对 put 操作将抛出 BufferOverflowException;但无论哪种情况,都不传输数据。

绝对 操作采用显式元素索引,不会影响位置。如果索引参数超过了该限制,则绝对 get put 操作将抛出 IndexOutOfBoundsException

当然,通过适当信道的 I/O 操作还可以将数据传输到缓冲区或从中传出数据(是传入还是传出,通常与当前位置有关)。

下面的持有标记、位置、限制和容量值的关系:

0 <= 标记 <= 位置 <= 限制 <= 容量

 

返回 Buffer java.nio 中的方法

 Buffer Buffer.clear()           清除此缓冲区。

 Buffer Buffer.flip()           反转此缓冲区。

 Buffer Buffer.limit(int newLimit)           设置此缓冲区的限制。

 Buffer Buffer.mark()           在此缓冲区的位置设置其标记。

 Buffer Buffer.position(int newPosition)           设置此缓冲区的位置。

 Buffer Buffer.reset()       将此缓冲区的位置重新设置成以前标记的位置。

 Buffer Buffer.rewind()           重绕此缓冲区。

下面让我们写个简单的例子程序来对Buffer的使用更加深刻一点:

 

  1. import java.nio.IntBuffer;
  2. public class BufferTest {
  3.     
  4.     private IntBuffer intBuf = null;
  5.     
  6.     public BufferTest() {
  7.         intBuf = IntBuffer.allocate(10);
  8.     }
  9.     
  10.     private void startTest() {
  11.         setIntBuf(intBuf);
  12.         printIntBuf(intBuf);
  13.     }
  14.     
  15.     private void setIntBuf(IntBuffer byteBuf) {
  16.         for(int i = 0; i < byteBuf.capacity(); i++) {
  17.             byteBuf.put(i);
  18.         }
  19.     }
  20.     
  21.     private void printIntBuf(IntBuffer intBuf) {
  22.         for(int i = 0; i < intBuf.capacity(); i++) {
  23.             System.out.print(intBuf.get(i) + " ");
  24.         }
  25.     }
  26.     
  27.     public static void main(String[] args) {
  28.         BufferTest t1 = new BufferTest();
  29.         t1.startTest();
  30.     }

                }

以上程序实现的是初始化一个intBuffer然后一次放入10int整数,输出其intBuffer的内容输出结果:0 1 2 3 4 5 6 7 8 9

然后我练习用Buffer几个方法去取其中任意的几个数:

 

  1. import java.nio.IntBuffer;
  2. public class BufferTest {
  3.     
  4.     private IntBuffer intBuf = null;
  5.     
  6.     public BufferTest() {
  7.         intBuf = IntBuffer.allocate(10);
  8.         intBuf.position(3);
  9.     }
  10.     
  11.     private void startTest() {
  12.         setIntBuf(intBuf);
  13.         printIntBuf(intBuf);
  14.     }
  15.     
  16.     private void setIntBuf(IntBuffer byteBuf) {
  17.         for(int i = 0; i < byteBuf.capacity() - 3; i++) {
  18.             byteBuf.put(i);
  19.         }
  20.     }
  21.     
  22.     private void printIntBuf(IntBuffer intBuf) {
  23.         for(int i = 0; i < intBuf.capacity(); i++) {
  24.             System.out.println();
  25.             System.out.print(intBuf.get(i) + " ");
  26.         }
  27.     }
  28.     
  29.     public static void main(String[] args) {
  30.         BufferTest t1 = new BufferTest();
  31.         t1.startTest();

                }            

                }

输出结果:0 0 0 0 1 2 3 4 5 6

从以上的例程我们可以看的出,不管是读取还是写入内容,都是从position,也就是说position相当于一个标识位,正如我们所熟悉的LinkedList中的标志位每当我们读取或是放入内容的时候标识位自动向后移动一位!

 

  1. import java.nio.IntBuffer;
  2. public class BufferTest {
  3.     
  4.     private IntBuffer intBuf = null;
  5.     
  6.     public BufferTest() {
  7.         intBuf = IntBuffer.allocate(10);
  8.         intBuf.position(3);
  9.         intBuf.limit(7);
  10.     }
  11.     
  12.     private void startTest() {
  13.         setIntBuf(intBuf);
  14.         printIntBuf(intBuf);
  15.     }
  16.     
  17.     private void setIntBuf(IntBuffer byteBuf) {
  18.         for(int i = 0; i < byteBuf.limit() - 3; i++) {
  19.             byteBuf.put(i);
  20.         }
  21.     }
  22.     
  23.     private void printIntBuf(IntBuffer intBuf) {
  24.         for(int i = 0; i < intBuf.limit(); i++) {
  25.             System.out.println();
  26.             System.out.print(intBuf.get(i) + " ");
  27.         }
  28.     }
  29.     
  30.     public static void main(String[] args) {
  31.         BufferTest t1 = new BufferTest();
  32.         t1.startTest();

    }}

输出结果:0 0 0 0 1 2 3

可以看的出不管是在读取或是放置内容的时候都在根据当前位去操作,一直到limited(),如果超出这俩个限制的时候就会抛出IllegalArgumentException异常。

 

上面已经介绍了limitposition那么mark又是干什么用的!!!!

 

mark

一个临时存放的位置下标。调用mark()会将mark设为当前的position的值,以后调用reset()会将position属性设置为mark的值。mark的值总是小于等于position的值,如果将position的值设的比mark小,当前的mark值会被抛弃掉。

好了,Buffer就介绍到这里!

 

下次介绍Duplicating buffer

 

 

你可能感兴趣的:(java,数据结构,exception,String,buffer,float)