Code example for DatagramChannel

Methods: close, connect, isConnected, write

0
        assertEquals(0, this.channel1.write(buf));
    } 
 
    private void noconnectWrite(ByteBuffer buf) throws IOException {
        try { 
            this.channel1.write(buf);
            fail("should throw NotYetConnectedException");
        } catch (NotYetConnectedException e) {
            // correct 
        } 
    } 
 
    /* 
     * Test method for 'DatagramChannelImpl.write(ByteBuffer)' 
     */ 
    public void testWriteByteBuffer_Block() throws IOException { 
        ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
        connectWriteBuf(localAddr1, writeBuf);
    } 
 
    public void testWriteByteBuffer_NonBlock() throws IOException { 
        ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
        this.channel1.configureBlocking(false);
        connectWriteBuf(localAddr1, writeBuf);
    } 
 
    public void testWriteByteBuffer_Block_closed() throws IOException { 
        ByteBuffer writeBuf = ByteBuffer.allocateDirect(CAPACITY_NORMAL);
        InetSocketAddress ipAddr = localAddr1;
        noconnectWrite(writeBuf);
        this.channel1.connect(ipAddr);
        assertTrue(this.channel1.isConnected());
        this.channel1.close();
        try { 
            channel1.write(writeBuf);
            fail("should throw ClosedChannelException");
        } catch (ClosedChannelException e) {
            // correct 
        } 
    }