Search in sources :

Example 1 with IllegalBlockingModeException

use of java.nio.channels.IllegalBlockingModeException in project robovm by robovm.

the class AbstractSelectableChannel method register.

/**
     * Registers this channel with the specified selector for the specified
     * interest set. If the channel is already registered with the selector, the
     * {@link SelectionKey interest set} is updated to {@code interestSet} and
     * the corresponding selection key is returned. If the channel is not yet
     * registered, this method calls the {@code register} method of
     * {@code selector} and adds the selection key to this channel's key set.
     *
     * @param selector
     *            the selector with which to register this channel.
     * @param interestSet
     *            this channel's {@link SelectionKey interest set}.
     * @param attachment
     *            the object to attach, can be {@code null}.
     * @return the selection key for this registration.
     * @throws CancelledKeyException
     *             if this channel is registered but its key has been canceled.
     * @throws ClosedChannelException
     *             if this channel is closed.
     * @throws IllegalArgumentException
     *             if {@code interestSet} is not supported by this channel.
     * @throws IllegalBlockingModeException
     *             if this channel is in blocking mode.
     * @throws IllegalSelectorException
     *             if this channel does not have the same provider as the given
     *             selector.
     */
@Override
public final SelectionKey register(Selector selector, int interestSet, Object attachment) throws ClosedChannelException {
    if (!isOpen()) {
        throw new ClosedChannelException();
    }
    if (!((interestSet & ~validOps()) == 0)) {
        throw new IllegalArgumentException("no valid ops in interest set: " + interestSet);
    }
    synchronized (blockingLock) {
        if (isBlocking) {
            throw new IllegalBlockingModeException();
        }
        if (!selector.isOpen()) {
            if (interestSet == 0) {
                // throw ISE exactly to keep consistency
                throw new IllegalSelectorException();
            }
            // throw NPE exactly to keep consistency
            throw new NullPointerException("selector not open");
        }
        SelectionKey key = keyFor(selector);
        if (key == null) {
            key = ((AbstractSelector) selector).register(this, interestSet, attachment);
            keyList.add(key);
        } else {
            if (!key.isValid()) {
                throw new CancelledKeyException();
            }
            key.interestOps(interestSet);
            key.attach(attachment);
        }
        return key;
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) SelectionKey(java.nio.channels.SelectionKey) CancelledKeyException(java.nio.channels.CancelledKeyException) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) IllegalSelectorException(java.nio.channels.IllegalSelectorException)

Example 2 with IllegalBlockingModeException

use of java.nio.channels.IllegalBlockingModeException in project robovm by robovm.

the class OldDatagramSocketTest method test_sendLjava_net_DatagramPacket.

public void test_sendLjava_net_DatagramPacket() throws Exception {
    // Test for method void
    // java.net.DatagramSocket.send(java.net.DatagramPacket)
    int[] ports = Support_PortManager.getNextPortsForUDP(2);
    final int portNumber = ports[0];
    class TestDGSend implements Runnable {

        Thread pThread;

        public TestDGSend(Thread t) {
            pThread = t;
        }

        public void run() {
            try {
                byte[] rbuf = new byte[1000];
                sds = new DatagramSocket(portNumber);
                DatagramPacket sdp = new DatagramPacket(rbuf, rbuf.length);
                sds.setSoTimeout(6000);
                sds.receive(sdp);
                retval = new String(rbuf, 0, testString.length());
                pThread.interrupt();
            } catch (java.io.InterruptedIOException e) {
                System.out.println("Recv operation timed out");
                pThread.interrupt();
                ds.close();
            } catch (Exception e) {
                System.out.println("Failed to establish Dgram server: " + e);
            }
        }
    }
    try {
        new Thread(new TestDGSend(Thread.currentThread()), "DGServer").start();
        ds = new java.net.DatagramSocket(ports[1]);
        dp = new DatagramPacket(testString.getBytes(), testString.length(), InetAddress.getLocalHost(), portNumber);
        // Wait to allow send to occur
        try {
            Thread.sleep(500);
            ds.send(dp);
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            ds.close();
            assertTrue("Incorrect data sent: " + retval, retval.equals(testString));
        }
    } catch (Exception e) {
        fail("Exception during send test : " + e.getMessage());
    } finally {
        ds.close();
    }
    /*
        SecurityManager sm = new SecurityManager() {

            public void checkPermission(Permission perm) {
            }

            public void checkMulticast(InetAddress maddr) {
                throw new SecurityException();
            }

            public void checkConnect(String host,
                    int port) {
                throw new SecurityException();
            }
        };
        try {

            ds = new java.net.DatagramSocket(ports[1]);
            dp = new DatagramPacket(testString.getBytes(), testString.length(),
                    InetAddress.getLocalHost(), portNumber);

            SecurityManager oldSm = System.getSecurityManager();
            System.setSecurityManager(sm);
            try {
                ds.send(dp);
                fail("SecurityException should be thrown.");
            } catch (SecurityException e) {
                // expected
            } catch (SocketException e) {
                fail("SocketException was thrown.");
            } finally {
                System.setSecurityManager(oldSm);
            }
        } catch(Exception e) {
            fail("Unexpected exception was thrown: " + e.getMessage());
        }
        */
    DatagramSocket socket = null;
    try {
        byte[] rbuf = new byte[1000];
        DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length);
        SocketAddress address = new InetSocketAddress(portNumber);
        DatagramChannel channel = DatagramChannel.open();
        channel.configureBlocking(false);
        socket = channel.socket();
        socket.send(rdp);
        fail("IllegalBlockingModeException was not thrown.");
    } catch (IllegalBlockingModeException ibme) {
    //expected
    } catch (IOException ioe) {
        fail("IOException was thrown: " + ioe.getMessage());
    } finally {
        socket.close();
    }
    //Regression for HARMONY-1118
    class testDatagramSocket extends DatagramSocket {

        public testDatagramSocket(DatagramSocketImpl impl) {
            super(impl);
        }
    }
    class testDatagramSocketImpl extends DatagramSocketImpl {

        protected void create() throws SocketException {
        }

        protected void bind(int arg0, InetAddress arg1) throws SocketException {
        }

        protected void send(DatagramPacket arg0) throws IOException {
        }

        protected int peek(InetAddress arg0) throws IOException {
            return 0;
        }

        protected int peekData(DatagramPacket arg0) throws IOException {
            return 0;
        }

        protected void receive(DatagramPacket arg0) throws IOException {
        }

        protected void setTTL(byte arg0) throws IOException {
        }

        protected byte getTTL() throws IOException {
            return 0;
        }

        protected void setTimeToLive(int arg0) throws IOException {
        }

        protected int getTimeToLive() throws IOException {
            return 0;
        }

        protected void join(InetAddress arg0) throws IOException {
        }

        protected void leave(InetAddress arg0) throws IOException {
        }

        protected void joinGroup(SocketAddress arg0, NetworkInterface arg1) throws IOException {
        }

        protected void leaveGroup(SocketAddress arg0, NetworkInterface arg1) throws IOException {
        }

        protected void close() {
        }

        public void setOption(int arg0, Object arg1) throws SocketException {
        }

        public Object getOption(int arg0) throws SocketException {
            return null;
        }
    }
    InetSocketAddress sa = new InetSocketAddress(InetAddress.getLocalHost(), 0);
    //no exception expected for next line
    new testDatagramSocket(new testDatagramSocketImpl()).send(new DatagramPacket(new byte[272], 3, sa));
    // Regression test for Harmony-2938
    InetAddress i = InetAddress.getByName("127.0.0.1");
    DatagramSocket d = new DatagramSocket(0, i);
    try {
        d.send(new DatagramPacket(new byte[] { 1 }, 1));
        fail("should throw NPE.");
    } catch (NullPointerException e) {
    // expected;
    } finally {
        d.close();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InterruptedIOException(java.io.InterruptedIOException) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) NetworkInterface(java.net.NetworkInterface) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) BindException(java.net.BindException) InterruptedIOException(java.io.InterruptedIOException) UnknownHostException(java.net.UnknownHostException) SocketException(java.net.SocketException) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) SocketTimeoutException(java.net.SocketTimeoutException) PortUnreachableException(java.net.PortUnreachableException) DatagramSocketImpl(java.net.DatagramSocketImpl) DatagramSocket(java.net.DatagramSocket) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) InetAddress(java.net.InetAddress)

Example 3 with IllegalBlockingModeException

use of java.nio.channels.IllegalBlockingModeException in project robovm by robovm.

the class OldDatagramSocketTest method test_receiveLjava_net_DatagramPacket.

public void test_receiveLjava_net_DatagramPacket() throws Exception {
    // Test for method void
    // java.net.DatagramSocket.receive(java.net.DatagramPacket)
    receive_oversize_java_net_DatagramPacket();
    final int[] ports = Support_PortManager.getNextPortsForUDP(2);
    final int portNumber = ports[0];
    class TestDGRcv implements Runnable {

        public void run() {
            try {
                InetAddress localHost = InetAddress.getLocalHost();
                Thread.sleep(1000);
                DatagramSocket sds = new DatagramSocket(ports[1]);
                sds.send(new DatagramPacket("Test".getBytes("UTF-8"), "Test".length(), localHost, portNumber));
                sds.send(new DatagramPacket("Longer test".getBytes("UTF-8"), "Longer test".length(), localHost, portNumber));
                sds.send(new DatagramPacket("3 Test".getBytes("UTF-8"), "3 Test".length(), localHost, portNumber));
                sds.send(new DatagramPacket("4 Test".getBytes("UTF-8"), "4 Test".length(), localHost, portNumber));
                sds.send(new DatagramPacket("5".getBytes("UTF-8"), "5".length(), localHost, portNumber));
                sds.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
    try {
        new Thread(new TestDGRcv(), "datagram receiver").start();
        ds = new java.net.DatagramSocket(portNumber);
        ds.setSoTimeout(6000);
        byte[] rbuf = new byte[1000];
        DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length);
        // Receive the first packet.
        ds.receive(rdp);
        assertEquals("Test", new String(rbuf, 0, rdp.getLength()));
        // Check that we can still receive a longer packet (http://code.google.com/p/android/issues/detail?id=24748).
        ds.receive(rdp);
        assertEquals("Longer test", new String(rbuf, 0, rdp.getLength()));
        // See what happens if we manually call DatagramPacket.setLength.
        rdp.setLength(4);
        ds.receive(rdp);
        assertEquals("3 Te", new String(rbuf, 0, rdp.getLength()));
        // And then another.
        ds.receive(rdp);
        assertEquals("4 Te", new String(rbuf, 0, rdp.getLength()));
        // And then a packet shorter than the user-supplied length.
        ds.receive(rdp);
        assertEquals("5", new String(rbuf, 0, rdp.getLength()));
        ds.close();
    } finally {
        ds.close();
    }
    DatagramSocket socket = null;
    try {
        byte[] rbuf = new byte[1000];
        DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length);
        DatagramChannel channel = DatagramChannel.open();
        channel.configureBlocking(false);
        socket = channel.socket();
        socket.receive(rdp);
        fail("IllegalBlockingModeException was not thrown.");
    } catch (IllegalBlockingModeException expected) {
    } finally {
        socket.close();
    }
    try {
        ds = new java.net.DatagramSocket(portNumber);
        ds.setSoTimeout(1000);
        byte[] rbuf = new byte[1000];
        DatagramPacket rdp = new DatagramPacket(rbuf, rbuf.length);
        ds.receive(rdp);
        fail("SocketTimeoutException was not thrown.");
    } catch (SocketTimeoutException expected) {
    } finally {
        ds.close();
    }
    interrupted = false;
    final DatagramSocket ds = new DatagramSocket();
    ds.setSoTimeout(12000);
    Runnable runnable = new Runnable() {

        public void run() {
            try {
                ds.receive(new DatagramPacket(new byte[1], 1));
            } catch (InterruptedIOException e) {
                interrupted = true;
            } catch (IOException ignored) {
            }
        }
    };
    Thread thread = new Thread(runnable, "DatagramSocket.receive1");
    thread.start();
    do {
        Thread.sleep(500);
    } while (!thread.isAlive());
    ds.close();
    int c = 0;
    do {
        Thread.sleep(500);
        if (interrupted) {
            fail("received interrupt");
        }
        if (++c > 4) {
            fail("read call did not exit");
        }
    } while (thread.isAlive());
    interrupted = false;
    final int portNum = ports[0];
    final DatagramSocket ds2 = new DatagramSocket(ports[1]);
    ds2.setSoTimeout(12000);
    Runnable runnable2 = new Runnable() {

        public void run() {
            try {
                ds2.receive(new DatagramPacket(new byte[1], 1, InetAddress.getLocalHost(), portNum));
            } catch (InterruptedIOException e) {
                interrupted = true;
            } catch (IOException ignored) {
            }
        }
    };
    Thread thread2 = new Thread(runnable2, "DatagramSocket.receive2");
    thread2.start();
    try {
        do {
            Thread.sleep(500);
        } while (!thread2.isAlive());
    } catch (InterruptedException ignored) {
    }
    ds2.close();
    int c2 = 0;
    do {
        Thread.sleep(500);
        if (interrupted) {
            fail("receive2 was interrupted");
        }
        if (++c2 > 4) {
            fail("read2 call did not exit");
        }
    } while (thread2.isAlive());
    interrupted = false;
    DatagramSocket ds3 = new DatagramSocket();
    ds3.setSoTimeout(500);
    Date start = new Date();
    try {
        ds3.receive(new DatagramPacket(new byte[1], 1));
    } catch (InterruptedIOException e) {
        interrupted = true;
    }
    ds3.close();
    assertTrue("receive not interrupted", interrupted);
    int delay = (int) (new Date().getTime() - start.getTime());
    assertTrue("timeout too soon: " + delay, delay >= 490);
}
Also used : InterruptedIOException(java.io.InterruptedIOException) DatagramChannel(java.nio.channels.DatagramChannel) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) BindException(java.net.BindException) InterruptedIOException(java.io.InterruptedIOException) UnknownHostException(java.net.UnknownHostException) SocketException(java.net.SocketException) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) SocketTimeoutException(java.net.SocketTimeoutException) PortUnreachableException(java.net.PortUnreachableException) Date(java.util.Date) DatagramSocket(java.net.DatagramSocket) SocketTimeoutException(java.net.SocketTimeoutException) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) InetAddress(java.net.InetAddress)

Example 4 with IllegalBlockingModeException

use of java.nio.channels.IllegalBlockingModeException in project robovm by robovm.

the class OldSocketTest method test_getOutputStream_shutdownOutput.

public void test_getOutputStream_shutdownOutput() throws Exception {
    // regression test for Harmony-873
    ServerSocket ss = new ServerSocket(0);
    Socket s = new Socket("127.0.0.1", ss.getLocalPort());
    ss.accept();
    s.shutdownOutput();
    try {
        s.getOutputStream();
        fail("should throw SocketException");
    } catch (IOException e) {
    // expected
    } finally {
        s.close();
    }
    SocketChannel channel = SocketChannel.open(new InetSocketAddress(ss.getInetAddress(), ss.getLocalPort()));
    channel.configureBlocking(false);
    ss.accept();
    Socket socket = channel.socket();
    OutputStream out = null;
    try {
        out = socket.getOutputStream();
        out.write(1);
        fail("IllegalBlockingModeException was not thrown.");
    } catch (IllegalBlockingModeException ibme) {
    //expected
    } finally {
        if (out != null)
            out.close();
        socket.close();
        channel.close();
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) OutputStream(java.io.OutputStream) ServerSocket(java.net.ServerSocket) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) IOException(java.io.IOException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 5 with IllegalBlockingModeException

use of java.nio.channels.IllegalBlockingModeException in project robovm by robovm.

the class SocketChannelTest method assertSocketAction_NonBlock_BeforeConnect.

private void assertSocketAction_NonBlock_BeforeConnect(Socket s) throws IOException {
    assertFalse(this.channel1.isConnected());
    this.server2 = new ServerSocket(localAddr2.getPort());
    try {
        s.connect(localAddr2);
        fail("Should throw IllegalBlockingModeException");
    } catch (IllegalBlockingModeException e1) {
    // OK.
    }
    if (this.channel1.isConnectionPending()) {
        try {
            s.bind(localAddr2);
            fail("Should throw ConnectionPendingException");
        } catch (ConnectionPendingException e1) {
        // OK.
        }
    } else {
        try {
            s.bind(localAddr2);
            fail("Should throw BindException");
        } catch (BindException e1) {
        // OK.
        }
    }
    assertFalse(this.channel1.isConnected());
    assertFalse(s.isConnected());
    s.close();
    assertTrue(s.isClosed());
    assertFalse(this.channel1.isOpen());
}
Also used : ConnectionPendingException(java.nio.channels.ConnectionPendingException) NoConnectionPendingException(java.nio.channels.NoConnectionPendingException) BindException(java.net.BindException) ServerSocket(java.net.ServerSocket) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException)

Aggregations

IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)36 ServerSocket (java.net.ServerSocket)14 SocketChannel (java.nio.channels.SocketChannel)10 IOException (java.io.IOException)7 OutputStream (java.io.OutputStream)7 Socket (java.net.Socket)7 ClosedChannelException (java.nio.channels.ClosedChannelException)7 SelectionKey (java.nio.channels.SelectionKey)7 Selector (java.nio.channels.Selector)6 ServerSocketChannel (java.nio.channels.ServerSocketChannel)6 BindException (java.net.BindException)5 InetSocketAddress (java.net.InetSocketAddress)5 SocketTimeoutException (java.net.SocketTimeoutException)5 IllegalSelectorException (java.nio.channels.IllegalSelectorException)5 InputStream (java.io.InputStream)4 DatagramSocket (java.net.DatagramSocket)4 SocketException (java.net.SocketException)4 UnknownHostException (java.net.UnknownHostException)4 DatagramChannel (java.nio.channels.DatagramChannel)4 SocketAddress (java.net.SocketAddress)3