Search in sources :

Example 16 with IllegalBlockingModeException

use of java.nio.channels.IllegalBlockingModeException in project j2objc by google.

the class SocketChannelTest method test_socket_getOutputStream_nonBlocking_read_Exception.

public void test_socket_getOutputStream_nonBlocking_read_Exception() throws IOException {
    byte[] buf = new byte[1];
    channel1.connect(this.localAddr1);
    InputStream is = channel1.socket().getInputStream();
    channel1.configureBlocking(false);
    try {
        is.read();
        fail();
    } catch (IllegalBlockingModeException expected) {
    }
    try {
        is.read(null);
        fail();
    } catch (NullPointerException expected) {
    // Any of these exceptions are possible.
    } catch (IllegalBlockingModeException expected) {
    // Any of these exceptions are possible.
    }
    try {
        is.read(buf, -1, 1);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    // Any of these exceptions are possible.
    } catch (IllegalBlockingModeException expected) {
    // Any of these exceptions are possible.
    }
    try {
        is.read(buf, 0, -1);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    // Any of these exceptions are possible.
    } catch (IllegalBlockingModeException expected) {
    // Any of these exceptions are possible.
    }
    try {
        is.read(buf, 0, 2);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    // Any of these exceptions are possible.
    } catch (IllegalBlockingModeException expected) {
    // Any of these exceptions are possible.
    }
    try {
        is.read(buf, 2, 0);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    // Any of these exceptions are possible.
    } catch (IllegalBlockingModeException expected) {
    // Any of these exceptions are possible.
    }
    try {
        is.read(null, 0, 0);
        fail();
    } catch (NullPointerException expected) {
    // Any of these exceptions are possible.
    } catch (IllegalBlockingModeException expected) {
    // Any of these exceptions are possible.
    }
    is.close();
    try {
        is.read();
        fail();
    } catch (IllegalBlockingModeException expected) {
    // Any of these exceptions are possible.
    } catch (IOException expected) {
    // Any of these exceptions are possible.
    }
    try {
        is.read(null);
        fail();
    } catch (NullPointerException expected) {
    // Any of these exceptions are possible.
    } catch (IllegalBlockingModeException expected) {
    // Any of these exceptions are possible.
    } catch (IOException expected) {
    // Any of these exceptions are possible.
    }
    try {
        is.read(buf, -1, 1);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    // Any of these exceptions are possible.
    } catch (IllegalBlockingModeException expected) {
    // Any of these exceptions are possible.
    } catch (IOException expected) {
    // Any of these exceptions are possible.
    }
    try {
        is.read(buf, 0, -1);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    // Any of these exceptions are possible.
    } catch (IllegalBlockingModeException expected) {
    // Any of these exceptions are possible.
    } catch (IOException expected) {
    // Any of these exceptions are possible.
    }
    try {
        is.read(buf, 0, 2);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    // Any of these exceptions are possible.
    } catch (IllegalBlockingModeException expected) {
    // Any of these exceptions are possible.
    } catch (IOException expected) {
    // Any of these exceptions are possible.
    }
    try {
        is.read(buf, 2, 0);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    // Any of these exceptions are possible.
    } catch (IllegalBlockingModeException expected) {
    // Any of these exceptions are possible.
    } catch (IOException expected) {
    // Any of these exceptions are possible.
    }
    try {
        is.read(null, 0, 0);
        fail();
    } catch (NullPointerException expected) {
    // Any of these exceptions are possible.
    } catch (IllegalBlockingModeException expected) {
    // Any of these exceptions are possible.
    } catch (IOException expected) {
    // Any of these exceptions are possible.
    }
}
Also used : InputStream(java.io.InputStream) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) IOException(java.io.IOException)

Example 17 with IllegalBlockingModeException

use of java.nio.channels.IllegalBlockingModeException in project j2objc by google.

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 18 with IllegalBlockingModeException

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

the class ScannerTest method test_Constructor_LReadableByteChannel.

public void test_Constructor_LReadableByteChannel() throws IOException {
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(null);
    SocketChannel sc = SocketChannel.open();
    sc.connect(ssc.socket().getLocalSocketAddress());
    sc.configureBlocking(false);
    assertFalse(sc.isBlocking());
    ssc.accept().close();
    ssc.close();
    assertFalse(sc.isBlocking());
    Scanner s = new Scanner(sc);
    try {
        s.hasNextInt();
        fail();
    } catch (IllegalBlockingModeException expected) {
    }
    sc.close();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Scanner(java.util.Scanner) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 19 with IllegalBlockingModeException

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

the class AbstractSelectableChannelTest method test_configureBlocking_Z_IllegalBlockingMode.

/**
     * @tests AbstractSelectableChannel#configureBlocking(boolean)
     */
public void test_configureBlocking_Z_IllegalBlockingMode() throws Exception {
    SocketChannel sc = SocketChannel.open();
    sc.configureBlocking(false);
    Selector acceptSelector = SelectorProvider.provider().openSelector();
    SelectionKey acceptKey = sc.register(acceptSelector, SelectionKey.OP_READ, null);
    assertEquals(sc.keyFor(acceptSelector), acceptKey);
    SelectableChannel getChannel = sc.configureBlocking(false);
    assertEquals(getChannel, sc);
    try {
        sc.configureBlocking(true);
        fail("Should throw IllegalBlockingModeException");
    } catch (IllegalBlockingModeException e) {
    // expected
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) SelectableChannel(java.nio.channels.SelectableChannel) AbstractSelectableChannel(java.nio.channels.spi.AbstractSelectableChannel) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) Selector(java.nio.channels.Selector)

Example 20 with IllegalBlockingModeException

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

the class AbstractSelectableChannelTest method test_register_LSelectorILObject_IllegalArgument.

/**
     * @tests AbstractSelectableChannel#register(Selector, int, Object)
     */
public void test_register_LSelectorILObject_IllegalArgument() throws IOException {
    Selector acceptSelector = SelectorProvider.provider().openSelector();
    assertTrue(acceptSelector.isOpen());
    MockSelectableChannel msc = new MockSelectableChannel(SelectorProvider.provider());
    msc.configureBlocking(false);
    // in nonblocking mode
    try {
        //different SelectionKey with validOps
        msc.register(acceptSelector, SelectionKey.OP_READ, null);
        fail("Should throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // expected
    }
    try {
        msc.register(null, 0, null);
        fail("Should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
    // in nonblocking mode, if selector closed
    acceptSelector.close();
    try {
        msc.register(acceptSelector, SelectionKey.OP_READ, null);
        fail("Should throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // expected
    }
    try {
        msc.register(null, 0, null);
        fail("Should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
    try {
        msc.register(acceptSelector, 0, null);
        fail("Should throw IllegalSelectorException");
    } catch (IllegalSelectorException e) {
    // expected
    }
    acceptSelector = SelectorProvider.provider().openSelector();
    // test in blocking mode
    msc.configureBlocking(true);
    try {
        msc.register(acceptSelector, SelectionKey.OP_READ, null);
        fail("Should throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // expected
    }
    try {
        msc.register(null, 0, null);
        fail("Should throw IllegalBlockingModeException");
    } catch (IllegalBlockingModeException e) {
    // expected
    }
    acceptSelector.close();
    // in blocking mode, if selector closed
    try {
        msc.register(acceptSelector, SelectionKey.OP_READ, null);
        fail("Should throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    // expected
    }
    try {
        msc.register(null, 0, null);
        fail("Should throw IllegalBlockingModeException");
    } catch (IllegalBlockingModeException e) {
    // expected
    }
    // register with an object
    Object argObj = new Object();
    SocketChannel sc = SocketChannel.open();
    sc.configureBlocking(false);
    try {
        sc.register(null, SelectionKey.OP_READ, argObj);
        fail("Should throw NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
    // if channel closed
    msc.close();
    try {
        msc.register(acceptSelector, SelectionKey.OP_READ, null);
        fail("Should throw ClosedChannelException");
    } catch (ClosedChannelException e) {
    // expected
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) IllegalSelectorException(java.nio.channels.IllegalSelectorException) Selector(java.nio.channels.Selector)

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