java NIO总结

Java NIO :
Channels
Buffers:缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存。这块内存被包装成 NIO Buffer 对象
Selectors:允许单线程处理多个 Channel。

Channel 的实现:
FileChannel 从文件中读写数据。
DatagramChannel 能通过 UDP 读写网络中的数据。
SocketChannel 能通过 TCP 读写网络中的数据。
ServerSocketChannel 可以监听新进来的 TCP 连接

Buffer 实现:
ByteBuffer
CharBuffer
DoubleBuffer
FloatBuffer
IntBuffer
LongBuffer
ShortBuffer
MappedByteBuffer

使用 Buffer 读写数据一般遵循以下四个步骤:
写入数据到 Buffer
调用flip()方法  // flip() 方法将 Buffer 从写模式切换到读模式。
从 Buffer 中读取数据
调用clear()方法或者compact()方法  //clear() 方法会清空整个缓冲区。compact() 方法只会清除已经读过的数据。

Buffer 的工作原理,需要熟悉它的三个属性:
capacity容量  作为一个内存块,Buffer 有一个固定的大小值
position  当你写数据到 Buffer 中时,position 表示当前的位置。 position 最大可为 capacity – 1
          Buffer.rewind() 将 position 设回 0
          mark() 与 reset() 方法
limit 写模式下,limit 等于 Buffer 的 capacity。

分配buf
ByteBuffer buf = ByteBuffer.allocate(48);分配

写数据到 Buffer 有两种方式:
从 Channel 写到 Buffer。int bytesRead = inChannel.read(buf)
通过 Buffer 的 put() 方法写到 Buffer 里。buf.put(127);

Buffer 中读取数据有两种方式:
从 Buffer 读取数据到 Channel。int bytesWritten = inChannel.write(buf);
使用 get() 方法从 Buffer 中读取数据。byte aByte = buf.get();

分散(scatter)从 Channel 中读取是指在读操作时将读取的数据写入多个 buffer 中。因此,Channel 将从 Channel 中读取的数据 “分散(scatter)” 到多个 Buffer 中。
  channel.read() 方法按照 buffer 在数组中的顺序将从 channel 中读取的数据写入到 buffer,当一个 buffer 被写满后,channel 紧接着向另一个 buffer 中写。

聚集(gather)写入 Channel 是指在写操作时将多个 buffer 的数据写入同一个 Channel,因此,Channel 将多个 Buffer 中的数据 “聚集(gather)” 后发送到 Channel。
    channel.write() 方法会按照 buffer 在数组中的顺序,将数据写入到 channel,注意只有 position 和 limit 之间的数据才会被写入。

FileChannel 的 transferFrom() 方法可以将数据从源通道传输到 FileChannel 中:
RandomAccessFile fromFile = new RandomAccessFile("fromFile.txt", "rw");
FileChannel      fromChannel = fromFile.getChannel();
RandomAccessFile toFile = new RandomAccessFile("toFile.txt", "rw");
FileChannel      toChannel = toFile.getChannel();
long position = 0;
long count = fromChannel.size();
toChannel.transferFrom(position, count, fromChannel);

transferTo() 方法将数据从 FileChannel 传输到其他的 channel 中:
RandomAccessFile fromFile = new RandomAccessFile("fromFile.txt", "rw");
FileChannel      fromChannel = fromFile.getChannel();
RandomAccessFile toFile = new RandomAccessFile("toFile.txt", "rw");
FileChannel      toChannel = toFile.getChannel();
long position = 0;
long count = fromChannel.size();
fromChannel.transferTo(position, count, toChannel);

Selector(选择器)是 Java NIO 中能够检测一到多个 NIO 通道,并能够知晓通道是否为诸如读写事件做好准备的组件。这样,一个单独的线程可以管理多个 channel,从而管理多个网络连接。
创建:
Selector selector = Selector.open();
注册:
channel.configureBlocking(false);
SelectionKey key = channel.register(selector,Selectionkey.OP_READ);
事件类型:
Connect SelectionKey.OP_CONNECT
Accept  SelectionKey.OP_ACCEPT
Read    SelectionKey.OP_READ
Write   SelectionKey.OP_WRITE

selectionKey.isAcceptable();
selectionKey.isConnectable();
selectionKey.isReadable();
selectionKey.isWritable();    
 
FileChannel方法:   
channel.truncate(1024);截取
channel.size();
channel.force(true);方法将通道里尚未写入磁盘的数据强制写到磁盘上。
    
    
Pipe有一个 source 通道和一个 sink 通道。数据会被写到 sink 通道,从 source 通道读取。
Pipe pipe = Pipe.open();

Pipe.SinkChannel sinkChannel = pipe.sink();


【原创】原创文章,更多关注敬请关注微信公众号。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值