java nio之 channel通道(二)

來源:互聯網
上載者:User

標籤:java nio


java nio 通道上一篇文章裡就講述過,channel總是寫資料的時候,要先把資料寫入到bytebuffer,讀資料的時候總是要先從channel中讀入到bytebuffer。如,這個圖是好多知名部落格常用的圖,很好理解這個channel。

650) this.width=650;" src="https://s4.51cto.com/wyfs02/M01/9E/4A/wKioL1mPKOfiRUviAAAlAoB14ME679.png-wh_500x0-wm_3-wmp_4-s_3716001730.png" title="QQ20170813001014.png" alt="wKioL1mPKOfiRUviAAAlAoB14ME679.png-wh_50" />


channel分為一下幾種:

  1. FileChannel

  2. SocketChannel

  3. ServerSocketChannel

  4. DatagramChannel


FileChannel:

經常說的FileChannel都是拿下面的例子說事

代碼如下:

package com.nio.basic;import java.io.IOException;import java.io.RandomAccessFile;/** * Created by sdc on 2017/8/13. */public class RandomAccessFileTest {    public static void main(String[] args) {        readFile();    }    /**     * 讀取檔案     * @throws Exception     */    public static void readFile(){        String fileName = "C:\\Users\\sdc\\Desktop\\gc (2).log";        RandomAccessFile randomAccessFile = null;        try{            randomAccessFile = new RandomAccessFile(fileName, "r");            long fileLength = randomAccessFile.length();            System.out.print("length" + fileLength);            int start = 100;            randomAccessFile.seek(start);            byte[] bytes = new byte[20];            int read = 0;            while ((read = randomAccessFile.read(bytes)) != -1) {                System.out.println(new String(bytes, "UTF-8"));            }            System.out.println(bytes.length);            System.out.println(new String(bytes, "UTF-8"));        }catch (Exception e) {            e.printStackTrace();        }finally {            if (randomAccessFile != null) {                try {                    randomAccessFile.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}


還有這樣的例子:

FileInputStream is = new FileInputStream(new File(src));FileChannel channelFrom = is.getChannel();

其實這兩個是用到了nio的channel,不妨自己寫一個例子試試。


SocketChannel和ServerSocketChannel一般是兩個集合起來說的,一個用於用戶端串連,一個用於服務端串連。

package com.nio.basic;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;import java.util.Set;/** * nio 服務端 * Created by sdc on 2017/8/13. */public class NIoServer {    ByteBuffer buffer = ByteBuffer.allocate(1024);    public static void main(String[] args) throws IOException {        System.out.println("server started...");        try {            new NIoServer().run();        } catch (Exception e) {            e.printStackTrace();        }    }    public void run () throws  Exception {        //開啟伺服器端的通訊端通道        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();        //伺服器端設定為非阻塞        serverSocketChannel.configureBlocking(false);        //服務端進行綁定        serverSocketChannel.bind(new InetSocketAddress("localhost", 8000));        //註冊感興趣的事件        Selector selector = Selector.open();        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);        while (true) {            int selectCount = selector.select();            if( selectCount ==0 ) {                continue;            }            selector.select();            Set<SelectionKey> keys = selector.selectedKeys();            //擷取迭代器            Iterator<SelectionKey> keyIterator = keys.iterator();            while (keyIterator.hasNext()) {                SelectionKey key = keyIterator.next();                if (!key.isValid()) {                    continue;                }                if (key.isAcceptable()) {                    ServerSocketChannel  sscTemp = (ServerSocketChannel) key.channel();                    //得到一個串連好的SocketChannel,並把它註冊到Selector上,興趣操作為READ                    SocketChannel socketChannel = sscTemp.accept();                    socketChannel.configureBlocking(false);                    socketChannel.register(selector, SelectionKey.OP_READ);                    System.out.println("REGISTER CHANNEL , CHANNEL NUMBER IS:" + selector.keys().size());                } else if (key.isReadable()) {                    //讀取通道中的資料                    SocketChannel channel = (SocketChannel) key.channel();                    read(channel);                }                keyIterator.remove(); //該事件已經處理,可以丟棄            }        }    }    private void read(SocketChannel channel) throws IOException {        int count ;        buffer.clear();        try {            while ((count = channel.read(buffer)) > 0) {                buffer.flip();                byte[] bytes = new byte[buffer.remaining()];                buffer.get(bytes);                System.out.println("READ FROM CLIENT:" + new String(bytes));            }            if (count < 0) {                channel.close();            }        } catch (IOException e) {            e.printStackTrace();        }    }}
package com.nio.basic;import java.io.IOException;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.SocketChannel;import java.util.Random;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;/** * nio 用戶端 * Created by sdc on 2017/8/13. */public class NioClient {    public static void main(String[] args) {        ExecutorService executorService = Executors.newCachedThreadPool();        executorService.submit(new Client("nio-client-1"));        executorService.submit(new Client("nio-client-2"));        executorService.submit(new Client("nio-client-3"));        executorService.shutdown();    }    static class Client extends Thread {        private String clientThreadName;        ByteBuffer buffer = ByteBuffer.allocate(1024);        Random random = new Random(20);        Client(String clientThreadName) {            this.clientThreadName = clientThreadName;        }        @Override        public void run() {            SocketChannel channel = null;            try {                channel = SocketChannel.open();                channel.configureBlocking(false);                channel.connect(new InetSocketAddress("localhost", 8000));                while (!channel.finishConnect()) {                    TimeUnit.MICROSECONDS.sleep(100);                }                for (int i=0; i<5; i++) {                    TimeUnit.MICROSECONDS.sleep(100 * random.nextInt());                    String message = "send message " + i + " from" + clientThreadName;                    buffer.put(message.getBytes());                    buffer.flip();                    //buffer先把資料讀入到buffer,然後channel先把buffer中的資料寫入到channel,                    channel.write(buffer);                    buffer.clear();                }            } catch (IOException e) {                e.printStackTrace();            } catch (InterruptedException e) {                e.printStackTrace();            }finally {                try {                    channel.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}




本文出自 “不積跬步無以至千裡” 部落格,請務必保留此出處http://shangdc.blog.51cto.com/10093778/1955874

java nio之 channel通道(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.