java.nio.Buffer
A buffer is a linear, finite sequence of elements of a specific primitive type. Aside from its content, the essential properties of a buffer
are its capacity, limit, and position:
一。 三个位置
- A buffer's position is the index of the next element to be read or written
- A buffer's limit is the index of the first element that should not be read or written
- A buffer's capacity is the number of elements it contains
在其子类中都有一个static allocate(int size)方法,用来分配对应类型的缓冲区。The new buffer's position will be zero, its limit will be its capacity
二。 两个模式
三。 五个操作
3.1 clear()
一旦读完Buffer中的数据,需要让Buffer准备好再次被写入,clear会恢复状态值,但不会擦除数据。
makes a buffer ready for a new sequence of channel-read or relative put operations
/** Clears this buffer. The position is set to zero, the limit is set to the capacity, and the mark is discarded. Invoke this method before using a sequence of channel-read or put operations to fill this buffer. For example: buf.clear(); // Prepare buffer for reading in.read(buf); // Read data This method does not actually erase the data in the buffer, but it is named as if it did because it will most often be used in situations in which that might as well be the case. **/ public final Buffer clear() { position = 0; limit = capacity; mark = -1; return this; }
1 FileChannel in = new FileInputStream("data.txt").getChannel(), 2 out = new FileOutputStream("data2.txt").getChannel(); 3 4 ByteBuffer buffer = ByteBuffer.allocate(BSIZE); 5 while(in.read(buffer) != -1) { 6 buffer.flip(); // Prepare for writing 7 out.write(buffer); 8 buffer.clear(); // Prepare for reading 9 }
3.2 flip()
Buffer有两种模式,写模式和读模式,flip后Buffer从写模式变成读模式。
makes a buffer ready for a new sequence of channel-write or relative get operations:
public final Buffer flip() { limit = position; position = 0; mark = -1; return this; }
1 // Read the file: 2 FileChannel fc = new FileInputStream("data.txt").getChannel(); 3 ByteBuffer buff = ByteBuffer.allocate(BSIZE); 4 fc.read(buff); 5 buff.flip(); 6 while(buff.hasRemaining()) 7 System.out.print((char)buff.get()); 8 }
3.3 rewind()
重置position为0,从头读写数据。
makes a buffer ready for re-reading the data that it already contains:
/** Flips this buffer. The limit is set to the current position and then the position is set to zero. If the mark is defined then it is discarded. After a sequence of channel-read or put operations, invoke this method to prepare for a sequence of channel-write or relative get operations. For example: buf.put(magic); // Prepend header in.read(buf); // Read data into rest of buffer buf.flip(); // Flip buffer out.write(buf); // Write header + data to channel This method is often used in conjunction with the compact method when transferring data from one place to another. **/ public final Buffer rewind() { position = 0; mark = -1; return this; }
3.4 mark()
把当前的position赋值给mark
public final Buffer mark() { mark = position; return this; }
3.5 reset()
把mark值还原给position
public final Buffer reset() { int m = mark; if (m < 0) throw new InvalidMarkException(); position = m; return this; }
(1)A buffer's mark is the index to which its position will be reset when the reset
method is invoked.
(2)The mark is not always defined, but when it is defined it is never negative and is never greater than the position.
(3)If the mark is defined then it is discarded when the position or the limit is adjusted to a value smaller than the mark.
(4)If the mark is not defined then invoking the reset
method causes an InvalidMarkException
to be thrown.
四.四个位置大小
0 <= mark <= position <= limit <= capacity
五.总结
Buffer类用来提供为基本数据类型(primitive) 的数据提供一个容器。
Buffer既可以是直接缓冲区可以是非直接缓冲区。
参考
ByteBuffer使用参考:主机字节序、网络字节序