BIO与NIO

BIO与NIO

1.传统BIO

(1)特点

  1. 面向数据流
  2. 阻塞式传输
  3. 一个客户端对应一个线程
  4. 在客户机增多的情况下,线程资源随之增多,会造成cpu资源枯竭

(2)需求

​ 客户机向服务器输出字符串,逐一在服务器器上打印显示。类似一个简陋的聊天室功能。

(3)代码示例

  1. 服务器程序TimeServer.java

    package com.xm.bio;
    
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class TimeServer {
    
     public static void main(String[] args) throws IOException {
         int port = 8080;
         if(args != null && args.length>0) {
             try {
                 port = Integer.parseInt(args[0]);
                 
             } catch (Exception e) {
                 
             }
         }
         
         ServerSocket server = null;
         try {
             server = new ServerSocket(port);
             System.out.println("开启服务器:"+server.getLocalSocketAddress());
             Socket socket = null;
             while(true) {
                 socket = server.accept();
                 new Thread(new TimeServerHandle(socket)).start();
             }
         } finally {
             if(server != null) {
                 System.out.println("服务器已关闭!");
                 server.close();
                 server = null;
             }
         }
    
     }
    
    }
    
    
  2. 服务器处理客户机进程TimeServerHandle.java

    package com.xm.bio;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.util.Date;
    
    public class TimeServerHandle implements Runnable {
     
     private Socket socket;
     
     public TimeServerHandle(Socket socket) {
         this.socket = socket;
     }
    
     @Override
     public void run() {
         System.out.println(socket.getInetAddress().toString()+"客户机已连接");
         BufferedReader in = null;
         PrintWriter out = null;
         BufferedReader wt = null;
         try {
             in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             out = new PrintWriter(socket.getOutputStream(), true);
             wt = new BufferedReader(new InputStreamReader(System.in));
             while(true) {
                     System.out.println(in.readLine());
             }
         } catch (Exception e) {
             
         } finally {
             if(in != null) {
                 try {
                     in.close();
                     in = null;
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
             if(out != null) {
                 out.close();
                 out = null;
             }
             if(socket != null) {
                 try {
                     socket.close();
                     socket = null;
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
                 
             }
         }
         
    
     }
    
    }
    
    
  3. 客户端程序TimeClient.java

    package com.xm.bio;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class TimeClient {
    
     public static void main(String[] args) {
         int port = 8080;
         String host = "127.0.0.1";
         Socket socket = null;
         BufferedReader in = null;
         BufferedReader wt = null;
         PrintWriter out = null;
         try {
             socket = new Socket(host, port);
             wt = new BufferedReader(new InputStreamReader(System.in));
             in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             out = new PrintWriter(socket.getOutputStream(), true);
             String body = null;
             while(true) {
                 String str = wt.readLine();
                 out.println(str);
             }
         } catch (UnknownHostException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }finally {
             try {
                 wt.close();
                 in.close();
                 out.close();
                 socket.close();
             } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }
             
         }
    
     }
    
    }
    
    

2.NIO

(1)NIO特点

1.面向缓冲区
2.传输方式为管道传输
3.非阻塞
4.支持大并发下的io处理

(2)NIO下的本地文件传输

  1. 内存映射下的缓冲通道

    package com.xm.nio;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    import java.nio.file.Paths;
    import java.nio.file.StandardOpenOption;
    import java.time.Duration;
    import java.time.Instant;
    
    import org.junit.jupiter.api.Test;
    
    public class NIOFileDemo {
     
     /**
      * 1.通过流获取通道
      */
     @Test
     public void test1() {
         Instant begin = Instant.now();
         //1.定义文件流
         FileInputStream fis = null;
         FileOutputStream fos = null;
         //2.获取通道
         FileChannel inChannel = null;
         FileChannel outChannel = null;
         try {
             fis = new FileInputStream("1.jpg");
             fos = new FileOutputStream("2.jpg");
             
             inChannel = fis.getChannel();
             outChannel = fos.getChannel();
             
             //3.定义缓冲区
             ByteBuffer buffer = ByteBuffer.allocate(1024);
             
             //4.读取数据到缓冲区,再从缓冲区写入到文件
             while(inChannel.read(buffer) != -1) {
                 //切换到读模式
                 buffer.flip();
                 //写操作到管道
                 outChannel.write(buffer);
                 //清空buffer
                 buffer.clear();
             }
         } catch (FileNotFoundException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         } finally {
             //5.关闭通道和流
             if(inChannel != null) {
                 try {
                     inChannel.close();
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
             }
             if(outChannel != null) {
                 try {
                     outChannel.close();
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
             }
             if(fis != null) {
                 
                 try {
                     fis.close();
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
                 
             }
             if(fos != null) {
                 try {
                     fos.close();
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
                 
             }
             
             }
         Instant end = begin.plus(Duration.ofSeconds(10));
         System.out.println("Difference in milliseconds : " + Duration.between(begin, end).toMillis());
     }
     
     /**
      * 通过文件获取管道
      */
     @Test
     public void test2() {
         Instant begin = Instant.now();
         FileChannel inChannel =null;
         FileChannel outChannel = null;
         try {
             inChannel =FileChannel.open(Paths.get("1.jpg"), StandardOpenOption.READ);
             /**
              * StandardOpenOption.CREATE与StandardOpenOption.CREATE_NEW的区别
              * 1.StandardOpenOption.CREATE:无则创建,有则覆盖
              * 2.StandardOpenOption.CREATE_NEW:无则创建,有则报错
              */
             outChannel =FileChannel.open(Paths.get("2.jpg"), StandardOpenOption.WRITE,StandardOpenOption.CREATE);
             //3.定义缓冲区
             ByteBuffer buffer = ByteBuffer.allocate(1024);
             
             //4.读取数据到缓冲区,再从缓冲区写入到文件
             while(inChannel.read(buffer) != -1) {
                 //切换到读模式
                 buffer.flip();
                 //写操作到管道
                 outChannel.write(buffer);
                 //清空buffer
                 buffer.clear();
             }
         } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         } finally {
             //5.关闭通道和流
             if(inChannel != null) {
                 try {
                     inChannel.close();
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
             }
             if(outChannel != null) {
                 try {
                     outChannel.close();
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
             }
         }
         Instant end = begin.plus(Duration.ofSeconds(10));
         System.out.println("Difference in milliseconds : " + Duration.between(begin, end).toMillis());
         
     }
    
    }
    
    
  2. 物理映射下的缓冲通道

    package com.xm.nio;
    
    import java.io.IOException;
    import java.nio.MappedByteBuffer;
    import java.nio.channels.FileChannel;
    import java.nio.channels.FileChannel.MapMode;
    import java.nio.file.Paths;
    import java.nio.file.StandardOpenOption;
    import java.time.Duration;
    import java.time.Instant;
    
    import org.junit.Test;
    
    public class NIOFileDemo2 {
     
     /**
      * 使用直接缓冲区传输
      */
     @Test
     public void test1() {
         Instant begin = Instant.now();
         FileChannel inChannel = null;
         FileChannel outChannel = null;
         
         try {
             //1.开启通道
             inChannel = FileChannel.open(Paths.get("1.jpg"), StandardOpenOption.READ);
             outChannel = FileChannel.open(Paths.get("2.jpg"), StandardOpenOption.WRITE,StandardOpenOption.CREATE,StandardOpenOption.READ);
             
             //2.定义物理缓冲区
             MappedByteBuffer inBuffer = inChannel.map(MapMode.READ_ONLY, 0, inChannel.size());
             MappedByteBuffer outBuffer = outChannel.map(MapMode.READ_WRITE, 0, inChannel.size());
             
             //3.缓冲区读写操作
             byte[]  dst = new byte[inBuffer.limit()];
             inBuffer.get(dst);
             outBuffer.put(dst);
             
             
         } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         } finally {
             //4.关闭通道
             if(null != inChannel) {
                 
                 try {
                     inChannel.close();
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
             }
             if(null != outChannel) {
                 
                 try {
                     outChannel.close();
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
             }
         }
         Instant end = begin.plus(Duration.ofSeconds(10));
         System.out.println("Difference in milliseconds : " + Duration.between(begin, end).toMillis());
     }
     
     /**
      * 通道之间的传输
      */
     @Test
     public void test2() {
         Instant begin = Instant.now();
         FileChannel inChannel = null;
         FileChannel outChannel = null;
         //获取通道
         try {
             inChannel = FileChannel.open(Paths.get("1.jpg"), StandardOpenOption.READ);
             outChannel = FileChannel.open(Paths.get("2.jpg"), StandardOpenOption.WRITE,StandardOpenOption.CREATE);
             
             //通道间传输
             //1.to操作
             //inChannel.transferTo(0, inChannel.size(), outChannel);
             //2.from操作
             outChannel.transferFrom(inChannel, 0, inChannel.size());
         } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }finally {
             //4.关闭通道
             if(null != inChannel) {
                 
                 try {
                     inChannel.close();
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
             }
             if(null != outChannel) {
                 
                 try {
                     outChannel.close();
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
             }
         }
         Instant end = begin.plus(Duration.ofSeconds(10));
         System.out.println("Difference in milliseconds : " + Duration.between(begin, end).toMillis());
     }
    
    }
    
    

(3)NIO下的网络传输

  1. 阻塞式

    • 服务端程序

      package com.xm.nio.block;
      
      import java.io.IOException;
      import java.net.InetSocketAddress;
      import java.nio.ByteBuffer;
      import java.nio.channels.FileChannel;
      import java.nio.channels.ServerSocketChannel;
      import java.nio.channels.SocketChannel;
      import java.nio.file.Paths;
      import java.nio.file.StandardOpenOption;
      import java.util.Scanner;
      
      public class NIOServer {
      
         public static void main(String[] args) throws IOException {
             
             int port = 8989;
                 
             //1.获取通道
             ServerSocketChannel serverChannel = ServerSocketChannel.open();
             
             //2.绑定端口号
             serverChannel.bind(new InetSocketAddress(port));
             
             //3.获取客户端连接
             SocketChannel socketChannel = serverChannel.accept();
             
             //定义文件传输通道
             FileChannel outChannel = FileChannel.open(Paths.get("3.jpg"), StandardOpenOption.WRITE,StandardOpenOption.CREATE);
             ByteBuffer buffer = ByteBuffer.allocate(1024);
             while(socketChannel.read(buffer)!=-1) {
                 buffer.flip();
                 outChannel.write(buffer);
                 buffer.clear();
             }
             
             outChannel.close();
             socketChannel.close();
      
         }
      
      }
      
      
    • 客户端程序

      package com.xm.nio.block;
      
      import java.io.IOException;
      import java.net.InetSocketAddress;
      import java.nio.ByteBuffer;
      import java.nio.channels.FileChannel;
      import java.nio.channels.SocketChannel;
      import java.nio.file.Paths;
      import java.nio.file.StandardOpenOption;
      
      public class NIOClient {
      
         public static void main(String[] args) throws IOException {
             int port = 8989;
             //1.获取通道
             SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", port));
             
             
             //2.获取文件通道
             FileChannel inChannel = FileChannel.open(Paths.get("1.jpg"), StandardOpenOption.READ);
             inChannel.transferTo(0, inChannel.size(), socketChannel);
             
             //3.关闭通道
             inChannel.close();
             socketChannel.close();
      
         }
      
      }
      
      
  2. 非阻塞式

    • 服务端程序

      package com.xm.nio.noblock;
      
      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.ArrayList;
      import java.util.Iterator;
      import java.util.List;
      
      public class NIOServer {
      
         public static void main(String[] args) throws IOException {
             
      
             int port = 8989;
                 
             //1.开启通道
             ServerSocketChannel serverChannel = ServerSocketChannel.open();
             
             //2.绑定端口号
             serverChannel.bind(new InetSocketAddress(port));
             
             //3.设置非阻塞
             serverChannel.configureBlocking(false);
             
             //4.开启选择器
             Selector selector = Selector.open();
             
             //5.注册连接监听
             serverChannel.register(selector, SelectionKey.OP_ACCEPT);
             
             List<SocketChannel> channels = new ArrayList<>();
             
             while(selector.select() > 0) {
                 Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
                 
                 while(keys.hasNext()) {
                     SelectionKey key = keys.next();
                     if(key.isAcceptable()) {
                         SocketChannel socketChannel = serverChannel.accept();
                         channels.add(socketChannel);
                         System.out.println("客户端连接成功:"+socketChannel.getLocalAddress()+" hashcode:"+socketChannel.hashCode());
                         socketChannel.configureBlocking(false);
                         socketChannel.register(selector, SelectionKey.OP_READ);
                     } else if(key.isReadable()) {
                         SocketChannel socketChannel = (SocketChannel) key.channel();
                         ByteBuffer dst = ByteBuffer.allocate(1024);
                         int len;
                         while(-1 != (len=socketChannel.read(dst))) {
                             dst.flip();
                             System.out.println(new String(dst.array(),0,len));
                             /*for(SocketChannel sChannel:channels) {
                                 if(sChannel != socketChannel) {
                                     dst.flip();
                                     sChannel.write(dst);
                                 }
                             }*/
                             dst.clear();
                         }
                     }       
                 }
                 
                 keys.remove();
             }
      
         }
      
      }
      
      
    • 客户端

      package com.xm.nio.noblock;
      
      import java.io.IOException;
      import java.net.InetSocketAddress;
      import java.nio.ByteBuffer;
      import java.nio.channels.FileChannel;
      import java.nio.channels.SelectionKey;
      import java.nio.channels.Selector;
      import java.nio.channels.SocketChannel;
      import java.nio.file.Paths;
      import java.nio.file.StandardOpenOption;
      import java.util.Iterator;
      import java.util.Scanner;
      
      public class NIOClient{
         
         SocketChannel socketChannel;
         
         
         public NIOClient() throws IOException {
             int port = 8989;
             //1.获取通道
             socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", port));
             //2.设置异步非阻塞
             socketChannel.configureBlocking(false);
             
             //2.获取文件通道
             FileChannel inChannel = FileChannel.open(Paths.get("1.md"), StandardOpenOption.READ);
             inChannel.transferTo(0, inChannel.size(), socketChannel);
                     
                     //3.关闭通道
                     inChannel.close();
                     socketChannel.close();
             
         }
         
      
         public static void main(String[] args)  {   
             try {
                 new NIOClient();
             } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }
             
         }
             
      
      
      }
      
      
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,539评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,911评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,337评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,723评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,795评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,762评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,742评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,508评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,954评论 1 308
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,247评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,404评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,104评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,736评论 3 324
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,352评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,557评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,371评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,292评论 2 352

推荐阅读更多精彩内容