nio.Buffer小记
完全用数组来理解。。@ _ @
看了下Buffer的源码 居然跟想象的差不多也 ^^,的确是用数组存储,不过米有找到get和put的源码……
用数组来理解几个参数……:
- capacity:容量,相当于数组大小
- limit:有效个数,相当于字符串数组中'\0'的位置
- position:当前位置,遍历字符串数组时循环变量i的位置^^
- mark:标记,用于记忆位,与mark()、reset()方法搭配使用。
继续用数组来理解几个方法。。假设数据数组为data吧。。
- get():得到当前位置的值 同时指针往后移位。如果操作时position==limit则会抛出异常
-
int value = data[position];
-
++position;
-
return value;
-
- get(int i) :得到i位置的值,如果i==limit则会抛出异常。
-
if (i < capacity) return data[i];
-
- put(int value) :存储到当前位置,如果操作时position==limit则会抛出异常
-
data[position] = value;
-
++position;
-
- put(int i, int value) :改变i位置的值,如果i==limit则会抛出异常。
-
data[i] = value;
-
- clear():从头开始读,0~capacity范围均可更改。
-
limit = capacity;
-
position = 0;
-
mark = -1;
-
- flip() :锁定了原position~capacity区间的值不可更改,操作只限于0~position位置。一般在读取之前使用。
-
limit = position;
-
position = 0;
-
mark = -1;
-
- rewind():从行首开始读,直接把指针挪到0位置。一般用于重新读入。
-
-
position = 0;
-
mark = -1;
-
- hasRemaining():判断是否到达末尾
-
return position < limit;
-
- remaining():获得剩余有效元素个数
-
return limit - position;
-
- mark():标记当前位置
-
mark = position;
-
- reset():还原当前位置到标记位置。当mark为-1时会抛出异常。
-
position = mark;
-
附ByteBuffer的测试类,要注意position(int i)时mark大于新pos的话,mark会被清空:
-
package org.yoyo.nio.socket;
-
-
import java.nio.ByteBuffer;
-
-
import junit.framework.TestCase;
-
-
public class Test extends TestCase {
-
-
private static ByteBuffer buffer = ByteBuffer.allocate(10);
-
-
public void testPut() {
-
buffer.put((byte) 1);
-
assertEquals(10, buffer.capacity());
-
assertEquals(10, buffer.limit());
-
assertEquals(1, buffer.position());
-
}
-
-
public void testPutIndex() {
-
buffer.put(5, (byte) 3);
-
assertEquals(10, buffer.capacity());
-
assertEquals(10, buffer.limit());
-
assertEquals(1, buffer.position());
-
}
-
-
public void testPosition() {
-
buffer.position(5);
-
assertEquals(10, buffer.capacity());
-
assertEquals(10, buffer.limit());
-
assertEquals(5, buffer.position());
-
}
-
-
public void testMarkAndReset() {
-
buffer.mark();
-
assertEquals(10, buffer.capacity());
-
assertEquals(10, buffer.limit());
-
assertEquals(5, buffer.position());
-
-
buffer.position(7);
-
assertEquals(10, buffer.capacity());
-
assertEquals(10, buffer.limit());
-
assertEquals(7, buffer.position());
-
-
buffer.reset();
-
assertEquals(10, buffer.capacity());
-
assertEquals(10, buffer.limit());
-
assertEquals(5, buffer.position());
-
}
-
-
public void testFlip() {
-
buffer.flip();
-
assertEquals(10, buffer.capacity());
-
assertEquals(5, buffer.limit());
-
assertEquals(0, buffer.position());
-
}
-
-
public void testGet() {
-
assertEquals((byte) 1, buffer.get());
-
assertEquals(10, buffer.capacity());
-
assertEquals(5, buffer.limit());
-
assertEquals(1, buffer.position());
-
}
-
-
public void testRewind() {
-
buffer.rewind();
-
assertEquals(10, buffer.capacity());
-
assertEquals(5, buffer.limit());
-
assertEquals(0, buffer.position());
-
}
-
-
public void testClear() {
-
buffer.clear();
-
assertEquals(10, buffer.capacity());
-
assertEquals(10, buffer.limit());
-
assertEquals(0, buffer.position());
-
}
-
-
public void testGetIndex() {
-
assertEquals((byte) 3, buffer.get(5));
-
assertEquals(10, buffer.capacity());
-
assertEquals(10, buffer.limit());
-
assertEquals(0, buffer.position());
-
}
-
-
}