nio 的使用方法 2

前面的可能还是不方便,再具体一点:
package zzzhc;


/**
 * @author <a href="mailto:zzzhc0508@hotmail.com">zzzhc</a>
 *
 */
public interface SocketHandler extends ConnectHandler, ReadWriteHandler {
    void onConnected();
   
    void onConnectFailed(String msg);
   
    /**
     * 在数据从channel中读出后被调用.
     *
     */
    void onRead();
   
    /**
     * 在要写的数所已写入channnel后被调用.
     *
     */
    void onWrite();
   
    void onClosed(String msg);

}

//抽象实现
package zzzhc;

import java.io.IOException;
import java.net.SocketAddress;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;

/**
 * @author <a href="mailto:zzzhc0508@hotmail.com">zzzhc </a>
 * 
 */
public abstract class AbstractSocketHandler implements SocketHandler {
    public final static int DEFAULT_BUFFER_SIZE = 2 * 1024;

    protected final SelectorProcessor processor;

    protected final SocketChannel sc;

    protected final ByteBuffer readBuf;

    protected final ByteBuffer writeBuf;
   
    protected SocketAddress localAddress;

    protected SocketAddress remoteAddress;

    protected boolean connected = false;

    protected boolean closed = false;

    public AbstractSocketHandler(SelectorProcessor processor,
            SocketAddress remoteAddress) throws IOException {
        this.processor = processor;
        this.remoteAddress = remoteAddress;
        this.sc = SocketChannel.open();
        this.sc.configureBlocking(false);
        this.sc.connect(remoteAddress);
        int readSize = DEFAULT_BUFFER_SIZE;
        int writeSize = DEFAULT_BUFFER_SIZE;
        try {
            readSize = sc.socket().getReceiveBufferSize();
            writeSize = sc.socket().getSendBufferSize();
        } catch (SocketException e1) {
        }
        readBuf = ByteBuffer.allocate(readSize);
        writeBuf = ByteBuffer.allocate(writeSize);
        processor.register(sc, this, SelectionKey.OP_CONNECT);
    }

    public AbstractSocketHandler(SelectorProcessor processor,
            SocketAddress remoteAddress, ByteBuffer readBuf, ByteBuffer writeBuf)
            throws IOException {
        this.processor = processor;
        this.remoteAddress = remoteAddress;
        this.sc = SocketChannel.open();
        this.sc.configureBlocking(false);
        this.sc.connect(remoteAddress);
        readBuf.clear();
        writeBuf.clear();
        this.readBuf = readBuf;
        this.writeBuf = writeBuf;
        processor.register(sc, this, SelectionKey.OP_CONNECT);
    }

    public AbstractSocketHandler(SelectorProcessor processor, SocketChannel sc) {
        this.processor = processor;
        this.sc = sc;
        this.connected = true;
        if (this.sc.isBlocking()) {
            try {
                this.sc.configureBlocking(false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        int readSize = DEFAULT_BUFFER_SIZE;
        int writeSize = DEFAULT_BUFFER_SIZE;
        try {
            readSize = sc.socket().getReceiveBufferSize();
            writeSize = sc.socket().getSendBufferSize();
        } catch (SocketException e1) {
        }
        readBuf = ByteBuffer.allocateDirect(readSize);
        writeBuf = ByteBuffer.allocateDirect(writeSize);
        processor.register(sc, this, SelectionKey.OP_READ);
    }

    public AbstractSocketHandler(SelectorProcessor processor, SocketChannel sc,
            ByteBuffer readBuf, ByteBuffer writeBuf) {
        this.processor = processor;
        this.sc = sc;
        this.connected = true;
        if (this.sc.isBlocking()) {
            try {
                this.sc.configureBlocking(false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        readBuf.clear();
        writeBuf.clear();
        this.readBuf = readBuf;
        this.writeBuf = writeBuf;
        processor.register(sc, this, SelectionKey.OP_READ);
    }

    public void onConnected() {
        System.out.println("connect to "+this.getRemoteAddress()+" ok");
    }

    public void onConnectFailed(String msg) {
        System.out.println("connect to "+this.getRemoteAddress()+" failed:"+msg);
    }

    /**
     * 如果一次没读完,最后须调用readBuf.compact().
     * 如果已读完,须调用readBuf.clear().
     */
    public void onRead() {
        readBuf.flip();
        int len = readBuf.limit();
        byte[] buf = new byte[len];
        readBuf.get(buf);
        readBuf.clear();
        System.out.print(new String(buf));
        writeBuf.put(buf);
        writeBuf.flip();
        enableWrite();
    }

    public void onWrite() {
       
    }

    public void onClosed(String msg) {
        System.out.println("channel closed:"+msg);
    }

    public void handleConnect() {
        try {
            if (sc.finishConnect()) {
                onConnected();
                connected = true;
            } else {
                onConnectFailed("");
            }
            processor.addInterestOps(sc, SelectionKey.OP_READ);
        } catch (IOException e) {
            e.printStackTrace();
            onConnectFailed(e.getMessage());
        }
    }

    public void handleRead() {
        try {
            //System.out.println("read");
            int len = sc.read(readBuf);
            if (len == -1) {//closed
                dispose("channel been closed correct");
            } else {
                //readBuf.flip();
                onRead();
                processor.addInterestOps(sc,SelectionKey.OP_READ);
            }
        } catch (IOException e) {
            e.printStackTrace();
            dispose(e.getMessage());
        }
    }

    public void handleWrite() {
        if (write()==true) {
            onWrite();
        }
    }
   
    /**
     * 将writeBuf中的数据写入socketchannel中,如写完清空writeBuf返回true,否则返加false.
     * 在使用该方法前应先对writeBuf调用flip()方法.
     * @return
     */
    protected boolean  write() {
        if (writeBuf.hasRemaining()) {
            try {
                sc.write(writeBuf);
            } catch (IOException e) {
                e.printStackTrace();
                dispose(e.getMessage());
                return false;
            }
        }
        if (writeBuf.hasRemaining()) {
            enableWrite();
            return false;
        }
        else {
            writeBuf.clear();
            return true;
        }
    }

    public SocketAddress getLocalAddress() {
        if (localAddress == null) {
            localAddress = this.sc.socket().getLocalSocketAddress();
        }
        return localAddress;
    }

    public SocketAddress getRemoteAddress() {
        if (remoteAddress == null) {
            remoteAddress = this.sc.socket().getRemoteSocketAddress();
        }
        return remoteAddress;
    }

    public void dispose(String msg) {
        if (!closed) {
            closed = true;
            processor.closeChannel(sc);
            onClosed(msg);
        }
    }

    public boolean isConnected() {
        return connected;
    }

    public boolean isClosed() {
        return closed;
    }
   
    public void close() {
        processor.closeChannel(sc);
    }

    public void enableRead() {
        processor.addInterestOps(sc, SelectionKey.OP_READ);
    }

    public void enableWrite() {
        processor.addInterestOps(sc, SelectionKey.OP_WRITE);
    }

}

//使用方法,先在console运行nc -l -p 1234再运行EchoClient
package zzzhc;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;

/**
 * @author <a href="mailto:zzzhc0508@hotmail.com">zzzhc</a>
 *
 */
public class EchoClient extends AbstractSocketHandler {
   
    public EchoClient(SelectorProcessor processor,SocketAddress remote) throws IOException {
        super(processor,remote);
    }

    public static void main(String[] args) throws IOException{
        EchoClient client = new EchoClient(SelectorProcessor.getDefaultInstance(),new InetSocketAddress("localhost",1234));
    }
}

//复杂一点的
package zzzhc;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.URL;
import java.nio.channels.FileChannel;

/**
 * @author <a href="mailto:zzzhc0508@hotmail.com">zzzhc </a>
 * 
 */
public class SimpleHttpClient extends AbstractSocketHandler {

    public final static int START_CONNECT = 0;

    public final static int CONNECT_OK = 1;

    public final static int CONNECT_FAILED = 2;

    public final static int TRY_SEND_REQUEST = 10;

    public final static int REQUEST_SENT = 11;

    //public final static int RECV_RESPONSE_HEADER = 20;
    public final static int RESPONSE_HEADER_END = 21;

    public final static int START_DATA_TRANSFER = 30;

    public final static int END_DATA_TRANSFER = 31;

    protected int status = START_CONNECT;

    protected URL url;

    protected FileChannel fc;

    protected FileOutputStream out;
   
    private StringBuffer header = new StringBuffer();

    public SimpleHttpClient(SelectorProcessor processor, SocketAddress remote,
            URL url) throws IOException {
        super(processor, remote);
        this.url = url;
    }

    public void onConnected() {
        super.onConnected();
        try {
            Thread.sleep(10);
        }catch (Exception e){}
        status = CONNECT_OK;
        fillHeader(url);
        enableWrite();
        status = TRY_SEND_REQUEST;
    }

    protected void fillHeader(URL url) {
        writeBuf.clear();
        writeBuf.put("GET ".getBytes()).put(url.getPath().getBytes()).put(
                " HTTP/1.1".getBytes()).put((byte) '/r').put((byte) '/n');
        writeBuf.put("Host: ".getBytes()).put(url.getHost().getBytes()).put((byte) '/r')
                .put((byte) '/n');
        writeBuf.put((byte) '/r').put((byte) '/n');
        writeBuf.flip();
    }

    public void onConnectFailed(String msg) {
        super.onConnectFailed(msg);
        status = CONNECT_FAILED;
        System.exit(0);
    }

    public void onRead() {
        readBuf.flip();
        switch (status) {
        case REQUEST_SENT:
            int pos = readBuf.limit();
            byte b;
            while (readBuf.hasRemaining()) {
                b = readBuf.get();
                if (b == '/r') {
                    if (pos - readBuf.position() >= 3) {
                        b = readBuf.get();///n
                        b = readBuf.get();
                        if (b == '/r') {
                            b = readBuf.get();///n
                            readBuf.compact();
                            status = RESPONSE_HEADER_END;
                            return;
                        }
                    }
                }
            }
            readBuf.position(pos);
            readBuf.limit(readBuf.capacity());
            break;
        case RESPONSE_HEADER_END:
            status = START_DATA_TRANSFER;
        case START_DATA_TRANSFER:
            System.out.println("start data transfer.");
            if (out == null) {
                String file = url.getFile().trim();
                if ("".equals(file)) {
                    file = "index.html";
                }
                int idx = file.lastIndexOf('/');
                if (idx != -1) {
                    file = file.substring(idx + 1);
                }
                if ("".equals(file)) {
                    file = "index.html";
                }
                try {
                    System.out.println("open "+file+" to write.");
                    out = new FileOutputStream(file);
                    fc = out.getChannel();
                } catch (FileNotFoundException e) {
                    //this should not happend.
                    e.printStackTrace();
                    close();
                }
            }
            try {
                while (readBuf.hasRemaining()) {
                    fc.write(readBuf);
                }
            } catch (IOException e) {
                e.printStackTrace();
                close();
            }
            readBuf.clear();
            break;
        default:
            System.out.println("status:"+status);
        }
    }

    public void onClosed(String msg) {
        super.onClosed(msg);
        try {
            if (out != null) {
                out.close();
                fc.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.exit(0);
    }

    public void onWrite() {
        status = REQUEST_SENT;
        System.out.println("request sent.");
    }

    public static void main(String[] args) throws IOException{
        URL url = new URL("http://www.sohu.com/");
        new SimpleHttpClient(SelectorProcessor.getDefaultInstance(),new InetSocketAddress("www.sohu.com",80),url);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java NIO中的Channel是用于在Java程序中进行I/O操作的基本组件之一。Channel提供了一种高效的、可扩展的方式来进行数据传输,同时也支持非阻塞式I/O操作。 下面是一些常用的Channel方法: 1. `open()`:打开一个新的Channel对象。 2. `close()`:关闭当前的Channel对象。 3. `read(ByteBuffer dst)`:从Channel中读取数据到指定的ByteBuffer中。 4. `write(ByteBuffer src)`:将数据从指定的ByteBuffer写入到Channel中。 5. `configureBlocking(boolean block)`:设置当前Channel的阻塞模式,如果为true则为阻塞模式,如果为false则为非阻塞模式。 6. `register(Selector sel, int ops)`:将当前的Channel对象注册到指定的Selector中,并且指定关注的事件类型。 7. `isOpen()`:判断当前的Channel对象是否处于打开状态。 8. `isConnected()`:判断当前的Channel对象是否已经连接到远程服务器。 9. `finishConnect()`:完成Channel对象的连接操作,如果连接成功则返回true,否则返回false。 10. `bind(SocketAddress local)`:将当前的Channel对象绑定到指定的本地地址。 11. `getRemoteAddress()`:获取当前Channel对象连接的远程服务器地址。 12. `getLocalAddress()`:获取当前Channel对象绑定的本地服务器地址。 这些方法提供了基本的Channel操作,可以根据具体的需求进行使用。需要注意的是,Channel对象在使用完毕后需要调用close()方法进行关闭,否则可能会出现资源泄漏等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值