NIO Buffer代码示例

Buffer的代码示例

NIO中buffer

import java.nio.ByteBuffer;

import org.junit.Test;

public class TestBuffer {

    @Test
    public void testBuffer() {
        //创建一个缓冲区
        ByteBuffer buf = ByteBuffer.allocate(1024);
        //打印参数
        System.out.println("-----------------------------------");
        System.out.println(buf.capacity());
        System.out.println(buf.limit());
        System.out.println(buf.position());

        buf.put("hello".getBytes());

        System.out.println("-----------------------------------");
        System.out.println(buf.capacity());
        System.out.println(buf.limit());
        System.out.println(buf.position());

        buf.flip();

        System.out.println("-----------------------------------");
        System.out.println(buf.capacity());
        System.out.println(buf.limit());
        System.out.println(buf.position());

        byte[] dest = new byte[buf.limit()];
//      
//      buf.get(dest);
//      
//      System.out.println(new String(dest));

        buf.get(dest,0,2);
        System.out.println(new String(dest));

        System.out.println(buf.position());

        buf.mark();

        System.out.println("-----------------------------------");

        buf.get(dest,2,2);

        System.out.println(new String(dest));

        System.out.println(buf.position());

        buf.reset();

        System.out.println(buf.position());
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 NIO 服务端代码示例: ```java 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.Iterator; public class NioServer { public static void main(String[] args) throws IOException { // 创建一个 Selector Selector selector = Selector.open(); // 创建一个 ServerSocketChannel,并绑定到指定端口 ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.socket().bind(new InetSocketAddress(8888)); serverSocketChannel.configureBlocking(false); // 将 ServerSocketChannel 注册到 Selector 上,关注 OP_ACCEPT 事件 serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); while (true) { // 阻塞等待事件发生 selector.select(); // 处理事件 Iterator<SelectionKey> iterator = selector.selectedKeys().iterator(); while (iterator.hasNext()) { SelectionKey key = iterator.next(); iterator.remove(); if (key.isAcceptable()) { // 处理 OP_ACCEPT 事件 ServerSocketChannel ssc = (ServerSocketChannel) key.channel(); SocketChannel sc = ssc.accept(); sc.configureBlocking(false); sc.register(selector, SelectionKey.OP_READ); } else if (key.isReadable()) { // 处理 OP_READ 事件 SocketChannel sc = (SocketChannel) key.channel(); ByteBuffer buffer = ByteBuffer.allocate(1024); int len = sc.read(buffer); if (len > ) { buffer.flip(); byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); String message = new String(bytes, "UTF-8"); System.out.println("Received message: " + message); } else if (len == -1) { // 客户端关闭连接 sc.close(); } } } } } } ``` 以上是一个简单的 NIO 服务端代码示例,可以实现基本的客户端连接和消息读取功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值