日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java buffer nio_Java NIO之Buffer(缓冲区)入门

發(fā)布時間:2025/3/20 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java buffer nio_Java NIO之Buffer(缓冲区)入门 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?Java NIO中的緩存區(qū)(Buffer)用于和通道(Channel)進行交互。數(shù)據(jù)是從通道讀入緩沖區(qū),從緩沖區(qū)寫入到通道中的。

?緩沖區(qū)本質(zhì)上是一塊可以寫入數(shù)據(jù),然后可以從中讀取數(shù)據(jù)的內(nèi)存。這塊內(nèi)存被包裝成NIO Buffer對象,并提供了一組方法,用來方便的訪問該塊內(nèi)存。

?Buffer底層使用數(shù)組實現(xiàn)。

1、NIO 數(shù)據(jù)讀取基本操作示意

2、Java NIO中Buffer類型

?Java NIO中,根據(jù)數(shù)據(jù)類型不同(boolean 除外),提供了相應類型的緩沖區(qū):

ByteBuffer

MappedByteBuffer

CharBuffer

DoubleBuffer

FloatBuffer

IntBuffer

LongBuffer

ShortBuffer

3、Buffer的基本用法

?使用Buffer讀寫數(shù)據(jù)一般遵循以下四個步驟:

?寫入數(shù)據(jù)到Buffer;

?調(diào)用flip()方法;

?從Buffer中讀取數(shù)據(jù);

?調(diào)用clear()方法或者compact()方法;

?當向buffer寫入數(shù)據(jù)時,buffer會記錄下寫了多少數(shù)據(jù)。一旦要讀取數(shù)據(jù),需要通過flip()方法將Buffer從寫模式切換到讀模式。在讀模式下,可以讀取之前寫入到buffer的所有數(shù)據(jù)。

?一旦讀完了所有的數(shù)據(jù),就需要清空緩沖區(qū),讓它可以再次被寫入。有兩種方式能清空緩沖區(qū):調(diào)用clear()或compact()方法。clear()方法會清空整個緩沖區(qū)。compact()方法只會清除已經(jīng)讀過的數(shù)據(jù)。任何未讀的數(shù)據(jù)都被移到緩沖區(qū)的起始處,新寫入的數(shù)據(jù)將放到緩沖區(qū)未讀數(shù)據(jù)的后面。

String str = "charBuffer";

CharBuffer charBuffer = CharBuffer.allocate(1024);

charBuffer.put(str);

charBuffer.append("--");

charBuffer.append("hello world");

charBuffer.flip();

//單個讀取buffer中的內(nèi)容

/*while (charBuffer.hasRemaining()) {

System.out.println(charBuffer.get());

}*/

//一次性讀取buffer中的內(nèi)容

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;//可讀取數(shù)據(jù)大小

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()

清除緩沖區(qū),等待寫入。

public final Buffer clear() {

position = 0;

limit = capacity;

mark = -1;

return this;

}

4.2.4、flip()

將緩沖區(qū)由寫模式切換為讀模式。

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()

獲取剩余可讀元素個數(shù)。

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中的內(nèi)容

/*while (charBuffer.hasRemaining()) {

System.out.println(charBuffer.get());

}*/

//一次性讀取buffer中的內(nèi)容

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

*/

總結(jié)

以上是生活随笔為你收集整理的java buffer nio_Java NIO之Buffer(缓冲区)入门的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。