use of java.nio.channels.ServerSocketChannel in project robovm by robovm.
the class SocketChannelTest method test_socketChannel_write_close.
/**
* @tests SocketChannel#write(ByteBuffer) after close
*/
public void test_socketChannel_write_close() throws Exception {
// regression 4 for HARMONY-549
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(localAddr2);
SocketChannel sc = SocketChannel.open();
sc.connect(localAddr2);
SocketChannel sock = ssc.accept();
ByteBuffer buf = null;
ssc.close();
sc.close();
try {
sc.write(buf);
fail("should throw NPE");
} catch (NullPointerException e) {
// expected
}
sock.close();
}
use of java.nio.channels.ServerSocketChannel in project robovm by robovm.
the class SinkChannelTest method test_socketChannel_read_close.
public void test_socketChannel_read_close() throws Exception {
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(InetAddress.getLocalHost(), 49999));
SocketChannel sc = SocketChannel.open();
ByteBuffer buf = null;
try {
sc.write(buf);
fail("should throw NPE");
} catch (NullPointerException e) {
// expected
}
sc.connect(new InetSocketAddress(InetAddress.getLocalHost(), 49999));
SocketChannel sock = ssc.accept();
ssc.close();
sc.close();
try {
sc.write(buf);
fail("should throw NPE");
} catch (NullPointerException e) {
// expected
}
sock.close();
}
use of java.nio.channels.ServerSocketChannel in project robovm by robovm.
the class SocketChannelTest method testReadByteBuffer_Direct2.
public void testReadByteBuffer_Direct2() throws IOException {
byte[] request = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
ByteBuffer buffer = ByteBuffer.allocateDirect(128);
ServerSocketChannel server = ServerSocketChannel.open();
server.socket().bind(new InetSocketAddress(InetAddress.getLocalHost(), 0), 5);
Socket client = new Socket(InetAddress.getLocalHost(), server.socket().getLocalPort());
client.setTcpNoDelay(false);
Socket worker = server.socket().accept();
SocketChannel workerChannel = worker.getChannel();
OutputStream out = client.getOutputStream();
out.write(request);
out.close();
buffer.limit(5);
int bytesRead = workerChannel.read(buffer);
assertEquals(5, bytesRead);
assertEquals(5, buffer.position());
buffer.limit(request.length);
bytesRead = workerChannel.read(buffer);
assertEquals(6, bytesRead);
buffer.flip();
assertEquals(request.length, buffer.limit());
assertEquals(ByteBuffer.wrap(request), buffer);
client.close();
worker.close();
server.close();
}
use of java.nio.channels.ServerSocketChannel in project robovm by robovm.
the class ServerSocketChannelTest method test_accept_SOTIMEOUT.
/**
* @tests ServerSocketChannel#socket().getSoTimeout()
*/
public void test_accept_SOTIMEOUT() throws IOException {
// regression test for Harmony-707
final int SO_TIMEOUT = 10;
ServerSocketChannel sc = ServerSocketChannel.open();
try {
ServerSocket ss = sc.socket();
ss.bind(localAddr1);
sc.configureBlocking(false);
ss.setSoTimeout(SO_TIMEOUT);
SocketChannel client = sc.accept();
// non blocking mode, returns null since there are no pending connections.
assertNull(client);
int soTimeout = ss.getSoTimeout();
// Harmony fails here.
assertEquals(SO_TIMEOUT, soTimeout);
} finally {
sc.close();
}
}
use of java.nio.channels.ServerSocketChannel in project robovm by robovm.
the class SocketChannelTest method test_write$LByteBuffer_buffers.
/**
* @tests java.nio.channels.SocketChannel#write(ByteBuffer[])
*/
public void test_write$LByteBuffer_buffers() throws IOException {
// Set-up
ServerSocketChannel server = ServerSocketChannel.open();
server.socket().bind(null);
SocketChannel client = SocketChannel.open();
client.connect(server.socket().getLocalSocketAddress());
SocketChannel worker = server.accept();
// A variety of buffer types to write
byte[] data = "Hello world!".getBytes("UTF-8");
ByteBuffer[] buffers = new ByteBuffer[3];
buffers[0] = ByteBuffer.wrap(data, 0, 2);
assertFalse(buffers[0].isDirect());
assertTrue(buffers[0].hasArray());
buffers[1] = ByteBuffer.wrap(data, 2, 4).asReadOnlyBuffer();
assertFalse(buffers[1].isDirect());
assertFalse(buffers[1].hasArray());
buffers[2] = ByteBuffer.allocateDirect(42);
buffers[2].put(data, 6, data.length - 6);
buffers[2].flip();
assertTrue(buffers[2].isDirect());
// Android's direct buffers do have a backing array.
assertTrue(buffers[2].hasArray());
// Write them out, read what we wrote and check it
client.write(buffers);
client.close();
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
while (EOF != worker.read(readBuffer)) {
}
;
readBuffer.flip();
assertEquals(ByteBuffer.wrap(data), readBuffer);
// Tidy-up
worker.close();
server.close();
}
Aggregations