@@ -0,0 +1,2391 @@
+package com.github.booknara.nioexample;%0A%0Aimport java.io.IOException;%0Aimport java.net.InetSocketAddress;%0Aimport java.nio.ByteBuffer;%0Aimport java.nio.channels.SocketChannel;%0A%0A/**%0A * Created by Daehee Han(@daniel_booknara) on 2/12/16.%0A */%0Apublic class SocketChannelExample %7B%0A    public static final String URL_ADDRESS = %22http://www.google.com%22;%0A    public static final int SIZE = 48;%0A    public static final int PORT = 80;%0A%0A    public static void main(String%5B%5D args) %7B%0A        try %7B%0A            // Default SocketChannel mode is blocking mode%0A            SocketChannel blockingChannel = createSocketChannel(true);%0A%0A            readSocketChannel(blockingChannel);%0A            writeSocketChannel(blockingChannel);%0A%0A            blockingChannel.close();%0A%0A%0A            SocketChannel nonBlockingChannel = createSocketChannel(false);%0A            while (!nonBlockingChannel.finishConnect()) %7B%0A                // Do something...%0A            %7D%0A%0A            readSocketChannel(nonBlockingChannel);%0A            writeSocketChannel(nonBlockingChannel);%0A%0A            nonBlockingChannel.close();%0A        %7D catch (IOException e) %7B%0A            e.getStackTrace();%0A        %7D%0A    %7D%0A%0A    // SocketChannel -%3E ByteBuffer%0A    public static void readSocketChannel(SocketChannel channel) %7B%0A        try %7B%0A            ByteBuffer buffer = ByteBuffer.allocate(SIZE);%0A            int bytesRead = channel.read(buffer);%0A%0A        %7D catch (IOException e) %7B%0A            e.getStackTrace();%0A        %7D%0A    %7D%0A%0A    // String -%3E ByteBuffer -%3E SocketChannel%0A    public static void writeSocketChannel(SocketChannel channel) %7B%0A        try %7B%0A            String data = %22Response data to a client%22;%0A%0A            ByteBuffer buffer = ByteBuffer.allocate(data.getBytes().length);%0A            buffer.clear();%0A            buffer.put(data.getBytes());%0A            buffer.flip();%0A%0A            while (buffer.hasRemaining()) %7B%0A                channel.write(buffer);%0A            %7D%0A%0A        %7D catch (Exception e) %7B%0A            e.getStackTrace();%0A        %7D%0A    %7D%0A%0A    public static SocketChannel createSocketChannel(boolean blockMode) throws IOException %7B%0A        // Default SocketChannel mode is blocking mode%0A        SocketChannel channel = SocketChannel.open();%0A        channel.configureBlocking(blockMode);%0A%0A        InetSocketAddress address = new InetSocketAddress(URL_ADDRESS, PORT);%0A        channel.connect(address);%0A%0A        return channel;%0A    %7D%0A%7D%0A

Add SocketChannel example