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;
}
}
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();
}
}
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);
}
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();
}
}
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());
}
Aggregations