Code example for DatagramChannel

Methods: configureBlocking, register

0
    protected DatagramChannelWithTimeouts() throws IOException { 
        super(null); 
        log.debug("Creating DatagramChannel");
        selector = Selector.open();
        channel = DatagramChannel.open();
        channel.configureBlocking(false);
        channelKey = channel.register(selector, SelectionKey.OP_READ);
    } 
 
    public static DatagramChannel open() throws IOException {
        return new DatagramChannelWithTimeouts(); 
    } 
 
    public int read(ByteBuffer dst) throws IOException {
        int bytesRead = 0;
        while (selector.select(readTimeout) > 0) {
            if (log.isDebugEnabled()) {
                log.debug("Loop " + bytesRead);
            } 
            // damn NPE in unit tests... 
            if (selector.selectedKeys() != null) {