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

    }
}

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


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值