Search in sources :

Example 31 with IllegalBlockingModeException

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

the class ChannelsTest method test_newReader_LReadableByteChannel_LString.

/**
	 * @tests java.nio.channels.Channels#newReader(ReadableByteChannel channel,
	 *        String charsetName)
	 */
public void test_newReader_LReadableByteChannel_LString() throws IOException {
    InetSocketAddress localAddr = new InetSocketAddress("127.0.0.1", Support_PortManager.getNextPort());
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(localAddr);
    SocketChannel sc = SocketChannel.open();
    sc.connect(localAddr);
    sc.configureBlocking(false);
    assertFalse(sc.isBlocking());
    ssc.accept().close();
    ssc.close();
    assertFalse(sc.isBlocking());
    Reader reader = Channels.newReader(sc, "UTF16");
    try {
        int i = reader.read();
        fail("should throw IllegalBlockingModeException");
    } catch (IllegalBlockingModeException expected) {
    }
    try {
        Channels.newInputStream(sc).read();
        fail("should throw IllegalBlockingModeException");
    } catch (IllegalBlockingModeException expected) {
    }
    sc.close();
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) Reader(java.io.Reader) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 32 with IllegalBlockingModeException

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

the class ChannelsTest method test_newReader_LReadableByteChannel_LString.

/**
 * @tests java.nio.channels.Channels#newReader(ReadableByteChannel channel,
 *        String charsetName)
 */
public void test_newReader_LReadableByteChannel_LString() 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());
    Reader reader = Channels.newReader(sc, "UTF16");
    try {
        int i = reader.read();
        fail("should throw IllegalBlockingModeException");
    } catch (IllegalBlockingModeException expected) {
    }
    try {
        Channels.newInputStream(sc).read();
        fail("should throw IllegalBlockingModeException");
    } catch (IllegalBlockingModeException expected) {
    }
    sc.close();
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) Reader(java.io.Reader) IllegalBlockingModeException(java.nio.channels.IllegalBlockingModeException) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 33 with IllegalBlockingModeException

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

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)

Example 34 with IllegalBlockingModeException

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

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

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

the class ServerSocketChannelTest method test_socket_accept_Blocking_NotBound.

/**
 * @tests ServerSocket#socket().accept()
 */
public void test_socket_accept_Blocking_NotBound() throws IOException {
    // regression test for Harmony-748
    ServerSocket gotSocket = serverChannel.socket();
    serverChannel.configureBlocking(true);
    try {
        gotSocket.accept();
        fail("Should throw an IllegalBlockingModeException");
    } catch (IllegalBlockingModeException expected) {
    }
    serverChannel.close();
    try {
        gotSocket.accept();
        fail("Should throw an IllegalBlockingModeException");
    } catch (IllegalBlockingModeException expected) {
    }
}
Also used : 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