下面的例子,说明了,nio中的三大核心类的基本使用。buffer,channel,selector

 package com.shengsiyuan.nio;

 import org.junit.Test;

 import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.*;
import java.nio.channels.spi.SelectorProvider;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Set; public class NioTest1 { public static void main(String[] args) {
IntBuffer intBuffer = IntBuffer.allocate(10); for (int i = 0; i < intBuffer.capacity(); i++) {
int randomNumber = new SecureRandom().nextInt(20);
intBuffer.put(randomNumber);
} intBuffer.flip();//翻转,从写转到读状态 while (intBuffer.hasRemaining()) {
System.out.println(intBuffer.get());
} } public void testBuffer() {
IntBuffer intBuffer = IntBuffer.allocate(10); for (int i = 0; i < intBuffer.capacity(); i++) {
int randomNumber = new SecureRandom().nextInt(20);
intBuffer.put(randomNumber);
} intBuffer.flip();//翻转,从写转到读状态 while (intBuffer.hasRemaining()) {
System.out.println(intBuffer.get());
}
} /**
* @ClassName: NioTest1
* @Description: 描述 读文件
* @Author:
* @CreateDate: 2019/8/27 21:26
* @Version: 1.0
*/
@Test
public void testChannelRead() {
try {
FileInputStream fileInputStream = new FileInputStream("NioTest1.txt"); FileChannel fileChannel = fileInputStream.getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocate(512); fileChannel.read(byteBuffer); byteBuffer.flip(); while(byteBuffer.remaining() > 0) { byte b = byteBuffer.get();
System.out.println("char is : " + (char) b); if((char)b == 'e') {
byteBuffer.mark();
}
} System.out.println(byteBuffer.position());
System.out.println(byteBuffer.limit());
System.out.println(byteBuffer.capacity()); byteBuffer.reset(); // System.out.println(byteBuffer.mark());
System.out.println(byteBuffer.position());
System.out.println(byteBuffer.limit());
System.out.println(byteBuffer.capacity()); fileChannel.close();
fileInputStream.close(); } catch (Exception e) {
e.printStackTrace();
}
} /**
* @ClassName: NioTest1
* @Description: 描述 写文件
* @Author:
* @CreateDate: 2019/8/27 21:25
* @Version: 1.0
*/
@Test
public void testChannelWrite() {
try{
FileInputStream fileInputStream = new FileInputStream("NioTest1.txt");
FileChannel fileChannel = fileInputStream.getChannel(); FileChannel randowFileChannel = new RandomAccessFile(new File("NioTest1.txt"),"rw").getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocate(512); byte[] byteArr = "hello nio nihao!".getBytes(); byteBuffer.put(byteArr); byteBuffer.flip(); // fileChannel.write(byteBuffer); randowFileChannel.write(byteBuffer);
}catch (Exception e) {
e.printStackTrace();
}
} /**
* @ClassName: NioTest1
* @Description: 描述 读写文件
* @Author:
* @CreateDate: 2019/8/27 21:25
* @Version: 1.0
*/
@Test
public void testChannelReadAndWrite() {
try {
FileInputStream fileInputStream = new FileInputStream("input.txt");
FileOutputStream fileOutputStream = new FileOutputStream("out.txt"); FileChannel inputFileChannel = fileInputStream.getChannel();
FileChannel outputFileChannel = fileOutputStream.getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocate(512); while(inputFileChannel.read(byteBuffer) != -1) { byteBuffer.flip(); outputFileChannel.write(byteBuffer); // byteBuffer.clear();
} }catch (Exception e) {
e.printStackTrace();
} } /**
* @ClassName: NioTest1
* @Description: 描述 切下部分的byteBuffer 作为一个快照,可以实现同步
* @Author:
* @CreateDate: 2019/8/27 21:24
* @Version: 1.0
*/
@Test
public void testSlice() {
try{
ByteBuffer byteBuffer = ByteBuffer.allocate(512); for (int i = 0; i < 20; i++) {
byteBuffer.put((byte)i);
}
// byteBuffer.flip(); ByteBuffer newByteBuffer = byteBuffer.slice(); byteBuffer.put((byte)'a');
byteBuffer.put((byte)'b'); // newByteBuffer.flip();
System.out.println(newByteBuffer.get(0)); // byteBuffer.put(0, (byte)9); newByteBuffer.put(0, (byte)9); System.out.println(byteBuffer.get(20)); System.out.println(newByteBuffer.get(0)); }catch (Exception e) {
e.printStackTrace();
}
} /**
* @ClassName: NioTest1
* @Description: 描述 只读的byteBuffer
* @Author:
* @CreateDate: 2019/8/27 21:24
* @Version: 1.0
*/
@Test
public void testReadonlyBuffer() { ByteBuffer byteBuffer = ByteBuffer.allocate(512);
for (int i = 0; i < 10; i++) {
byteBuffer.put((byte)i);
} ByteBuffer readOnlyBuffer = byteBuffer.asReadOnlyBuffer(); readOnlyBuffer.position(0); readOnlyBuffer.put((byte)1); } /**
* @ClassName: NioTest1
* @Description: 描述 通过直接内存,零拷贝来处理文件
* @Author:
* @CreateDate: 2019/8/27 21:23
* @Version: 1.0
*/
@Test
public void testChannelReadAndWriteDirect() {
try {
FileInputStream fileInputStream = new FileInputStream("input.txt");
FileOutputStream fileOutputStream = new FileOutputStream("out.txt"); FileChannel inputFileChannel = fileInputStream.getChannel();
FileChannel outputFileChannel = fileOutputStream.getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocateDirect(512); while(inputFileChannel.read(byteBuffer) != -1) { byteBuffer.flip(); outputFileChannel.write(byteBuffer); byteBuffer.clear();
} }catch (Exception e) {
e.printStackTrace();
} } /**
* @ClassName: NioTest1 内存映射,来修改文件 而不操作buffer
* @Description: 描述
* @Author:
* @CreateDate: 2019/8/27 21:23
* @Version: 1.0
*/
@Test
public void testMapBuffer() throws Exception {
RandomAccessFile randomAccessFile = new RandomAccessFile("NioTest1.txt", "rw");
FileChannel fileChannel = randomAccessFile.getChannel(); MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 5); mappedByteBuffer.put(0, (byte)'a');
mappedByteBuffer.put(3, (byte)'b'); fileChannel.close();
randomAccessFile.close();
} /**
* @ClassName: NioTest1
* @Description: 描述 测试 filelock
* @Author:
* @CreateDate: 2019/8/27 21:30
* @Version: 1.0
*/
@Test
public void testFilelock() throws Exception {
FileChannel fileChannel = new RandomAccessFile("NioTest1.txt", "rw").getChannel(); FileLock fileLock = fileChannel.lock(3, 6, false); System.out.println(fileLock.isValid());
System.out.println(fileLock.isShared());
} /**
* @ClassName: NioTest1
* @Description: 描述
* @Author:
* @CreateDate: 2019/8/27 22:36
* @Version: 1.0
*/
@Test
public void testScattering() throws Exception {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
SocketAddress address = new InetSocketAddress(8899);
serverSocketChannel.bind(address); SocketChannel socketChannel = serverSocketChannel.accept(); int messageLength = 2 + 3 +4;
ByteBuffer[] byteBuffers = new ByteBuffer[3]; byteBuffers[0] = ByteBuffer.allocate(2);
byteBuffers[1] = ByteBuffer.allocate(3);
byteBuffers[2] = ByteBuffer.allocate(4); while(true) {
long readLength = 0; //最大读取 messageLength 长度的字符
while(readLength < messageLength ) {
long r = socketChannel.read(byteBuffers);
readLength += r; System.out.println("byteBuffer length r is " + r);
System.out.println("readLength length r is " + readLength); Arrays.asList(byteBuffers).forEach(byteBuffer -> {
System.out.println("p:" + byteBuffer.position() + ", l: " +byteBuffer.limit());
});
} Arrays.asList(byteBuffers).forEach(byteBuffer -> {
byteBuffer.flip();
}); long writeLength = 0;
//写会去
while(writeLength < messageLength) {
long w = socketChannel.write(byteBuffers);
writeLength += w;
System.out.println("byteBuffer length w is " + w);
System.out.println("writeLength length w is " + writeLength); Arrays.asList(byteBuffers).forEach(byteBuffer -> {
System.out.println("p:" + byteBuffer.position() + ", l: " +byteBuffer.limit());
});
} Arrays.asList(byteBuffers).forEach(byteBuffer -> byteBuffer.clear()); }
} @Test
public void testSelector() throws Exception{
Selector selector = Selector.open(); int[] ports = {5000, 5001, 5002, 5003, 5004}; for (int i = 0; i < ports.length; i++) {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false); //变成非阻塞的模式 很重要 // serverSocketChannel.bind(new InetSocketAddress(ports[i])); ServerSocket serverSocket = serverSocketChannel.socket();
//这个bind 直接绑在 serverSocketChannel 可以不 尝试了下,好像区别不大,都可以走通 但是不能重复绑
serverSocket.bind(new InetSocketAddress(ports[i])); serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); System.out.println("监听:" + ports[i]);
} while(true) { int number = selector.select();
System.out.println("number:" + number); Set<SelectionKey> selectionKeys = selector.selectedKeys(); Iterator<SelectionKey> keyIterable = selectionKeys.iterator(); System.out.println("selectionKeys : " + selectionKeys); while(keyIterable.hasNext()) {
SelectionKey selectionKey = keyIterable.next();
if(selectionKey.isAcceptable()) {//可接受的
ServerSocketChannel serverSocketChannel = (ServerSocketChannel) selectionKey.channel();
//这里为什么还要调accept 因为上面放进去的就是 ServerSocketChannel ,而且没有调accept 接收请求
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.configureBlocking(false); System.out.println("获得链接:" + socketChannel); socketChannel.register(selector, SelectionKey.OP_READ);
// socketChannel.register(selector, SelectionKey.OP_READ); 重复注册好像不影响 keyIterable.remove();//如果不remove selectionKeys中会一直存在事件对象
} else if(selectionKey.isReadable()) {//可读取的
//这里就可以直接用SocketChannel来接收,因为在accept中,是用socketChannel来注册的
SocketChannel socketChannel = (SocketChannel) selectionKey.channel(); ByteBuffer byteBuffer = ByteBuffer.allocate(512);
while(socketChannel.read(byteBuffer) > 0) {
byteBuffer.flip(); System.out.println("读取到客户端的消息:" + byteBuffer);
System.out.println("读取到客户端:" + socketChannel); socketChannel.write(byteBuffer); byteBuffer.clear();
} keyIterable.remove(); } } } }
}

2、nio的例子实践的更多相关文章

  1. NIO模式例子

    NIO模式主要优势是体现在对多连接的管理,对众多连接各种事件的转发让处理变得更加高效,所以一般是服务器端才会使用NIO模式,而对于客户端为了方便及习惯使用阻塞模式的Socket进行通信.所以NIO模式 ...

  2. IO模型之NIO代码及其实践详解

    一.简介 NIO我们一般认为是New I/O(也是官方的叫法),因为它是相对于老的I/O类库新增的( JDK 1.4中的java.nio.*包中引入新的Java I/O库).但现在都称之为Non-bl ...

  3. docker-1-环境安装及例子实践

    1.安装go 先新建一个Go的工作空间文件夹,文件夹路径建议放在$HOME下: userdeMacBook-Pro:~ user$ cd $HOME userdeMacBook-Pro:~ user$ ...

  4. python argparse例子实践

    python 解析命令行读取参数,在多个文件或者不同语言协同的项目中,python脚本经常需要从命令行直接读取参数. 万能的python自带了sys.arg.argparse.optparse模块等, ...

  5. NIO的学习

    参考 http://wenku.baidu.com/link?url=rq-BEp3Et4JRrE62f2Lv9hq8nT_Gq0XPb65h8OBqTAt-ILfqKmdjIhVEp8bctIdm0 ...

  6. 漫谈Java IO之 NIO那些事儿

    前面一篇中已经介绍了基本IO的使用以及最简单的阻塞服务器的例子,本篇就来介绍下NIO的相关内容,前面的分享可以参考目录: 网络IO的基本知识与概念 普通IO以及BIO服务器 NIO的使用与服务器Hel ...

  7. MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example1.2 Static Map with Two Layers

    MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example1.2 Static Map with Two Layers 一.前言 上一篇博客< ...

  8. NIO基本操作

    NIO是Java 4里面提供的新的API,目的是用来解决传统IO的问题 NIO主要有三大核心部分:Channel(通道),Buffer(缓冲区), Selector(选择器) Channel(通道) ...

  9. JAVA NIO non-blocking模式实现高并发服务器(转)

    原文链接:JAVA NIO non-blocking模式实现高并发服务器 Java自1.4以后,加入了新IO特性,NIO. 号称new IO. NIO带来了non-blocking特性. 这篇文章主要 ...

随机推荐

  1. Word表格斜线怎么弄?这里有三种方法很实用

    有些时候我们也会在Word文档中插入表格,因为这样可以准确的表达出文档中的内容.大家应该都知道Excel表格斜线怎么弄,那么Word表格斜线怎么弄吗?今天呢小编就帮大家总结了三种方法哦,有需要的小伙伴 ...

  2. python 中 and or

    在Python 中,and 和 or 执行布尔逻辑演算,如你所期待的一样,但是它们并不返回布尔值:而是,返回它们实际进行比较的值之一. 一.and: 在布尔上下文中从左到右演算表达式的值,如果布尔上下 ...

  3. webpack学习3.1由浅入深-打包JS

    打包JS Step1:在新建的文件夹下新建一个sum.js文件,一个app.js文件 sum.js //es module export default function(a,b){ return a ...

  4. Winform DataGridView控件数据导出EXcel

    /// <summary> /// 导出Excel /// </summary> /// <param name="dgv"></para ...

  5. SVN重新定位操作流程

    因服务器其他系统需要SVN原来使用的443端口无法继续使用需要更换,服务器SVN端口已更新为8443,现将个人电脑SVN文件夹路径重新定位流程展示如下: 确定即修改成功!

  6. Django-Model 大全

      ORM 映射关系: 表名 <-------> 类名 字段 <-------> 属性 表记录 <-------> 类实例对象 创建表(建立模型) 实例:我们来假定 ...

  7. SSM项目整合纪实

    一 前 言 本来是为了探究一些功能性问题,需要一套完整的项目架构,本以为SSM用过那么多了,轻松搭建不在话下,但是过程中还是遇到一些问题,踩到一些未曾料想的坑.博文以搭建极简架构为目的,附带一些关键阐 ...

  8. 使用react-app-rewired和customize-cra对默认webpack自定义配置

    最近在学习react框架,之前一直都是用vue 开发,知道在vue 中知道如何配置一下相关的webpack 有助于开发,学react 过程中,我也在想这些该怎么配置啊,所以就有这篇文章. 这篇文章主要 ...

  9. 拖动条(SeekBar)的功能与用法

    拖动条和进度条非常相似,只是进度条采用颜色填充来表明进度完成的程度,而拖动条则通过滑块的位置来标识数值——而且拖动条允许用户拖动滑块来改变值,因此拖动条通常用于对系统的某种数值进行调节,比如调节音量等 ...

  10. SpringCloud断路器(Hystrix)

    一.为什么需要 Hystrix? 在微服务架构中,我们将业务拆分成一个个的服务,服务与服务之间可以相互调用(RPC).为了保证其高可用,单个服务又必须集群部署.由于网络原因或者自身的原因,服务并不能保 ...