Code example for DatagramChannel

Methods: connect, isBlocking, isConnected, isOpen, send

0
    // ------------------------------------------------------------------- 
 
    private void sendDataBlocking(InetSocketAddress addr, ByteBuffer writeBuf)
            throws IOException { 
        InetSocketAddress ipAddr = addr;
        assertEquals(CAPACITY_NORMAL, this.channel1.send(writeBuf, ipAddr));
        assertTrue(this.channel1.isOpen());
        assertTrue(this.channel1.isBlocking());
        this.channel1.connect(ipAddr);
        assertTrue(this.channel1.isConnected());
    } 
 
    private void sendDataNonBlocking(InetSocketAddress addr, ByteBuffer writeBuf)
            throws IOException { 
        InetSocketAddress ipAddr = addr;
        this.channel1.configureBlocking(false);
        assertEquals(CAPACITY_NORMAL, this.channel1.send(writeBuf, ipAddr));
        assertTrue(this.channel1.isOpen());
        assertFalse(this.channel1.isBlocking());
        this.channel1.connect(ipAddr);
        assertTrue(this.channel1.isConnected());
    } 
 
    /* 
     * Test method for 'DatagramChannelImpl.send(ByteBuffer, SocketAddress)' 
     */ 
    public void testSend_NoServerBlockingCommon() throws IOException { 
        ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
        sendDataBlocking(localAddr1, writeBuf);
    } 
 
    public void testSend_NoServerNonblockingCommon() throws IOException { 
        ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
        sendDataNonBlocking(localAddr1, writeBuf);
    } 
 
    public void testSend_NoServerTwice() throws IOException { 
        ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
        sendDataBlocking(localAddr1, writeBuf);
        // can not buffer twice! 
        assertEquals(0, this.channel1.send(writeBuf, localAddr1));
        try { 
            channel1.send(writeBuf, localAddr2);
            fail("Should throw IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // correct 
        } 
    } 
 
    public void testSend_NoServerNonBlockingTwice() throws IOException { 
        ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
        sendDataNonBlocking(localAddr1, writeBuf);
        // can not buffer twice! 
        assertEquals(0, this.channel1.send(writeBuf, localAddr1));
        try { 
            channel1.send(writeBuf, localAddr2);
            fail("Should throw IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // correct 
        } 
    } 
 
    public void testSend_NoServerBufNull() throws IOException { 
        try { 
            sendDataBlocking(localAddr1, null);
            fail("Should throw a NPE here.");
        } catch (NullPointerException e) {
            // correct 
        } 
    } 
 
    public void testSend_NoServerBufNullTwice() throws IOException { 
        ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
        try { 
            sendDataBlocking(localAddr1, null);
            fail("Should throw a NPE here.");
        } catch (NullPointerException e) {
            // correct 
        } 
        sendDataBlocking(localAddr1, writeBuf);
        try { 
            channel1.send(null, localAddr2);
            fail("Should throw NPE");
        } catch (NullPointerException e) {
            // correct 
        } 
    }