java buffer nio_Java NIO之Buffer(缓冲区)入门
?Java NIO中的緩存區(Buffer)用于和通道(Channel)進行交互。數據是從通道讀入緩沖區,從緩沖區寫入到通道中的。
?緩沖區本質上是一塊可以寫入數據,然后可以從中讀取數據的內存。這塊內存被包裝成NIO Buffer對象,并提供了一組方法,用來方便的訪問該塊內存。
?Buffer底層使用數組實現。
1、NIO 數據讀取基本操作示意
2、Java NIO中Buffer類型
?Java NIO中,根據數據類型不同(boolean 除外),提供了相應類型的緩沖區:
ByteBuffer
MappedByteBuffer
CharBuffer
DoubleBuffer
FloatBuffer
IntBuffer
LongBuffer
ShortBuffer
3、Buffer的基本用法
?使用Buffer讀寫數據一般遵循以下四個步驟:
?寫入數據到Buffer;
?調用flip()方法;
?從Buffer中讀取數據;
?調用clear()方法或者compact()方法;
?當向buffer寫入數據時,buffer會記錄下寫了多少數據。一旦要讀取數據,需要通過flip()方法將Buffer從寫模式切換到讀模式。在讀模式下,可以讀取之前寫入到buffer的所有數據。
?一旦讀完了所有的數據,就需要清空緩沖區,讓它可以再次被寫入。有兩種方式能清空緩沖區:調用clear()或compact()方法。clear()方法會清空整個緩沖區。compact()方法只會清除已經讀過的數據。任何未讀的數據都被移到緩沖區的起始處,新寫入的數據將放到緩沖區未讀數據的后面。
String str = "charBuffer";
CharBuffer charBuffer = CharBuffer.allocate(1024);
charBuffer.put(str);
charBuffer.append("--");
charBuffer.append("hello world");
charBuffer.flip();
//單個讀取buffer中的內容
/*while (charBuffer.hasRemaining()) {
System.out.println(charBuffer.get());
}*/
//一次性讀取buffer中的內容
char[] dst = new char[charBuffer.limit()];
charBuffer.get(dst);
System.out.println(new String(dst));
4、Buffer屬性與方法詳解
4.1、基本屬性
public abstract class Buffer {
// Invariants: mark <= position <= limit <= capacity
private int mark = -1;//標記位置
private int position = 0;//當前游標位置
private int limit;//可讀取數據大小
private int capacity;//buffer容量大小
...
}
4.2、常用方法
4.2.1、mark()
標記當前位置,配合reset使用。
public final Buffer mark() {
mark = position;
return this;
}
4.2.2、reset()
重置游標為標記位。
public final Buffer reset() {
int m = mark;
if (m < 0)
throw new InvalidMarkException();
position = m;
return this;
}
4.2.3、clear()
清除緩沖區,等待寫入。
public final Buffer clear() {
position = 0;
limit = capacity;
mark = -1;
return this;
}
4.2.4、flip()
將緩沖區由寫模式切換為讀模式。
public final Buffer flip() {
limit = position;
position = 0;
mark = -1;
return this;
}
4.2.5、rewind()
重置buffer,等待寫入。
public final Buffer rewind() {
position = 0;
mark = -1;
return this;
}
4.2.6、remaining()
獲取剩余可讀元素個數。
public final int remaining() {
return limit - position;
}
4.2.7、hasRemaining()
判斷是否還有可讀元素。
public final boolean hasRemaining() {
return position < limit;
}
5、Demo
String str = "charBuffer";
CharBuffer charBuffer = CharBuffer.allocate(1024);
charBuffer.put(str);
charBuffer.mark();
System.out.println("-------------mark-------------");
charBuffer.append("--");
charBuffer.append("hello world");
System.out.println("position = " + charBuffer.position());
System.out.println("limit = " + charBuffer.limit());
System.out.println("capacity = " + charBuffer.capacity());
charBuffer.reset();
System.out.println("-------------reset-------------");
System.out.println("position = " + charBuffer.position());
System.out.println("limit = " + charBuffer.limit());
System.out.println("capacity = " + charBuffer.capacity());
charBuffer.flip();
System.out.println("-------------flip-------------");
System.out.println("position = " + charBuffer.position());
System.out.println("limit = " + charBuffer.limit());
System.out.println("capacity = " + charBuffer.capacity());
//單個讀取buffer中的內容
/*while (charBuffer.hasRemaining()) {
System.out.println(charBuffer.get());
}*/
//一次性讀取buffer中的內容
char[] dst = new char[charBuffer.limit()-3];
charBuffer.get(dst);
System.out.println("dst>>>>>>>>>" + new String(dst));
charBuffer.compact();
System.out.println("-------------compact-------------");
System.out.println("position = " + charBuffer.position());
System.out.println("limit = " + charBuffer.limit());
System.out.println("capacity = " + charBuffer.capacity());
charBuffer.append("--");
charBuffer.append("hello world");
charBuffer.append("--");
charBuffer.append("hello world");
charBuffer.append("--");
charBuffer.append("hello world");
charBuffer.flip();
char[] dst2 = new char[charBuffer.limit()];
charBuffer.get(dst2);
System.out.println(new String(dst2));
/*
-------------mark-------------
position = 23
limit = 1024
capacity = 1024
-------------reset-------------
position = 10
limit = 1024
capacity = 1024
-------------flip-------------
position = 0
limit = 10
capacity = 1024
dst>>>>>>>>>charBuf
-------------compact-------------
position = 3
limit = 1024
capacity = 1024
fer--hello world--hello world--hello world
Process finished with exit code 0
*/
總結
以上是生活随笔為你收集整理的java buffer nio_Java NIO之Buffer(缓冲区)入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springboot获取原生js请求_s
- 下一篇: java获取目录中最后被更改的文件_如何