Java NIO Demo

Buffer相關操作, position limit capacity mark

package nio; 

import org.junit.Test;
import java.nio.ByteBuffer;
/**
* @program: netty_hello
* @description: test buffer
* @author: dengbin
* @create: 2018-11-23 15:01
*
*
* put 存數據
* get 取數據
*
* capacity 容量,表示緩衝區中最大存儲數據的容量, 一旦聲明,不可改變
* limit 界限,表示緩衝區中可以操作數據的大小, limit後的數據不能進行讀寫
* position 位置,表示緩衝區中正在操作數據的位置
* mark 標記位置,用於還原,和reset結合使用
*
* position <= limit <= capacity
*
*
**/
public class TestBuffer {
@Test
public void test1(){
//分配一個指定大小的緩衝區
ByteBuffer buf = ByteBuffer.allocate(1024);
//分配緩衝區後的結果 0 1024 1024
System.out.println("__________________allocate");
System.out.println("postition:" + buf.position());
System.out.println("limit:" + buf.limit());
System.out.println("capacity:" + buf.capacity());
//put後的結果 5, 1024, 1024
String str = "abcde";
buf.put(str.getBytes());
System.out.println("__________________put");
System.out.println("postition:" + buf.position());
System.out.println("limit:" + buf.limit());
System.out.println("capacity:" + buf.capacity());

//flip後的結果 0, 5, 1024
buf.flip();
System.out.println("__________________flip");
System.out.println("postition:" + buf.position());
System.out.println("limit:" + buf.limit());
System.out.println("capacity:" + buf.capacity());
//get後的結果 5, 5, 1024
byte[] dst = new byte[buf.limit()];
buf.get(dst);
System.out.println(new String(dst, 0, dst.length));
System.out.println("__________________get");
System.out.println("postition:" + buf.position());
System.out.println("limit:" + buf.limit());
System.out.println("capacity:" + buf.capacity());
//rewind 還原,復位 mark為-1, position為0
//0, 5, 1024
buf.rewind();
System.out.println("__________________rewind");
System.out.println("postition:" + buf.position());
System.out.println("limit:" + buf.limit());
System.out.println("capacity:" + buf.capacity());
//mark標記為0, get後position為1,reset恢復,position為0
buf.mark();
buf.get();
System.out.println("__________________before reset");
System.out.println("postition:" + buf.position());
System.out.println("limit:" + buf.limit());
System.out.println("capacity:" + buf.capacity());
buf.reset();
System.out.println("__________________reset");
System.out.println("postition:" + buf.position());
System.out.println("limit:" + buf.limit());
System.out.println("capacity:" + buf.capacity());
//清空, 假清空 數據仍然存在 0, 1024, 1024
buf.clear();
System.out.println("__________________clear");
System.out.println("postition:" + buf.position());
System.out.println("limit:" + buf.limit());
System.out.println("capacity:" + buf.capacity());
}
}
Java NIO Demo

NIO小demo,服務端監聽,客戶端連接併發送消息,服務端顯示

package nio;
import org.junit.Test;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Date;
import java.util.Iterator;
/**
* @program: netty_hello
* @description: test
* @author: dengbin
* @create: 2018-11-23 14:28
**/
public class NIODemo {
@Test
public void client() throws IOException {
//獲取通道
SocketChannel client = SocketChannel.open(new InetSocketAddress("127.0.0.1", 8888));
//切換為非阻塞模式
client.configureBlocking(false);
//分配指定大小的緩衝區
ByteBuffer buf = ByteBuffer.allocate(1024);
//發送數據給服務端
//存入數據
buf.put(new Date().toString().getBytes());
//切換模式。進行讀取,這個是從buf中進行讀取,因為要把buf中的數據拿出來寫入到通道
buf.flip();
//向通道寫入
client.write(buf);
//清空緩衝區
buf.clear();
//關閉通道

client.close();
}
@Test
public void server() throws IOException {
//獲取通道
ServerSocketChannel server = ServerSocketChannel.open();
//設置非阻塞
server.configureBlocking(false);
//綁定端口
server.bind(new InetSocketAddress(8888));
//獲取選擇器
Selector selector = Selector.open();
//向選擇器註冊連接事件
server.register(selector, SelectionKey.OP_ACCEPT);
//有事件就緒
while(selector.select() > 0){
//獲取迭代器。遍歷事件
Iterator<selectionkey> it = selector.selectedKeys().iterator();
while(it.hasNext()){
SelectionKey key = it.next();
//如果是連接就緒。則獲取客戶端的連接,並註冊讀就緒事件
if(key.isAcceptable()){
SocketChannel client = server.accept();
client.configureBlocking(false);
client.register(selector, SelectionKey.OP_READ);
}else if(key.isReadable()){
//讀就緒事件,進行數據處理
SocketChannel client = (SocketChannel) key.channel();
ByteBuffer buf = ByteBuffer.allocate(1024);
int len = 0;
while((len = client.read(buf)) > 0){
//切換為讀模式
buf.flip();
System.out.println(new String(buf.array(), 0, len));
buf.clear();
}
}
it.remove();
}
}
}
}

/<selectionkey>
Java NIO Demo


分享到:


相關文章: