Java NIO Channel
来源:互联网 发布:linux 下安装jdk 编辑:程序博客网 时间:2024/06/07 05:12
Java NIO的通道类似流,但又有些不同:
- 既可以从通道中读取数据,又可以写数据到通道。但流的读写通常是单向的。
- 通道可以异步地读写。
- 通道中的数据总是要先读到一个Buffer,或者总是要从一个Buffer中写入。
正如上面所说,从通道读取数据到缓冲区,从缓冲区写入数据到通道。如下图所示:
Channel的实现
这些是Java NIO中最重要的通道的实现:
- FileChannel
- DatagramChannel
- SocketChannel
- ServerSocketChannel
FileChannel 从文件中读写数据。
DatagramChannel 能通过UDP读写网络中的数据。
SocketChannel 能通过TCP读写网络中的数据。
ServerSocketChannel可以监听新进来的TCP连接,像Web服务器那样。对每一个新进来的连接都会创建一个SocketChannel。
使用FileChannel读取数据到Buffer中的示例:
package com.hutonm.nio;import org.junit.Before;import org.junit.Test;import java.io.FileNotFoundException;import java.io.IOException;import java.io.RandomAccessFile;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;import java.util.RandomAccess;public class Niotest { @Test public void method1(){ RandomAccessFile aFile = null; try { aFile = new RandomAccessFile("src/nio.text","rw"); FileChannel fileChannel = aFile.getChannel(); //create buffer with capacity of 1024 bytes ByteBuffer buf = ByteBuffer.allocate(1024); //read into buffer int byteRead = fileChannel.read(buf); // print byteRead //System.out.println(byteRead); while (byteRead != -1){ //make buffer read for read buf.flip(); while (buf.hasRemaining()){ System.out.print((char)buf.get()); } //清除已读数据 buf.compact(); byteRead = fileChannel.read(buf); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}
创建一个SocketChannel:
package com.hutonm.nio;import org.junit.Test;import java.io.IOException;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.SocketChannel;import java.util.concurrent.TimeUnit;public class NioClient { @Test public void client(){ ByteBuffer buffer = ByteBuffer.allocate(1024); SocketChannel socketChannel = null; try { socketChannel = SocketChannel.open(); socketChannel.configureBlocking(false); socketChannel.connect(new InetSocketAddress("10.0.2.41",8080)); if(socketChannel.finishConnect()){ int i = 0; while (true){ TimeUnit.SECONDS.sleep(1); String info = "i'm " + i++ + "th information from client"; buffer.clear(); buffer.put(info.getBytes()); buffer.flip(); while (buffer.hasRemaining()){ System.out.println(buffer); socketChannel.write(buffer); } } } } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }}
//
运行演示图:
client
java.nio.HeapByteBuffer[pos=0 lim=31 cap=1024]java.nio.HeapByteBuffer[pos=0 lim=31 cap=1024]java.nio.HeapByteBuffer[pos=0 lim=31 cap=1024]java.nio.HeapByteBuffer[pos=0 lim=31 cap=1024]java.nio.HeapByteBuffer[pos=0 lim=31 cap=1024]java.nio.HeapByteBuffer[pos=0 lim=31 cap=1024]java.nio.HeapByteBuffer[pos=0 lim=31 cap=1024]java.nio.HeapByteBuffer[pos=0 lim=31 cap=1024]
server:
======i'm 0th information from clienti'm 1th information from clienti'm 2th information from clienti'm 3th information from clienti'm 4th information from clienti'm 5th information from clienti'm 6th information from client
参考:
http://ifeve.com/channels/
阅读全文
0 0
- Java NIO Channel
- Java NIO Socket Channel
- Java NIO(3-Channel)
- Java NIO Channel
- Java NIO笔记 Channel
- Java NIO Channel
- Java NIO--Channel
- java NIO Channel
- Java NIO (二) Channel
- Java NIO Channel
- Java NIO Channel
- java NIO-Channel
- Java NIO Channel
- Java NIO之Channel
- java nio channel
- Java NIO Channel tranfer
- Java NIO Channel
- java NIO Channel详解
- java在SSM下使用支付宝扫码支付
- MyBatis日志
- (springmvc)页面找不到静态资源文件Failed to load resource: the server responded with a status of 404 (Not Found)
- mt6735 [Audio App]拖动歌曲计时条过程中,如何显示当前快进的时间
- TraceView 创建文件.trace文件出现 read-only file system问题
- Java NIO Channel
- Testbench基本入门
- 用简单的代码实现简易的rxjs的filter功能,帮助rxjs新手了解rxjs的操作符的工作原理。
- csdn积分获取攻略
- 抗混叠滤波器的概念
- 修改 Linux下面文件夹的颜色
- java 数据结构--顺序表
- Android app调用第三方地图(百度地图,高德地图)导航
- win7怎么打开注册表