import dalvik.annotation.TestTargetClass;
import dalvik.annotation.TestLevel;
import dalvik.annotation.AndroidOnly;
+ import dalvik.annotation.BrokenTest;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.BindException;
import java.net.ConnectException;
+import java.net.Inet4Address;
+import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
method = "write",
args = {java.nio.ByteBuffer.class}
)
+ @BrokenTest("Occasionally fail in CTS, but works in CoreTestRunner")
public void test_writeLjava_nio_ByteBuffer_Nonblocking_HugeData() throws IOException {
// initialize write content
ByteBuffer writeContent = ByteBuffer.allocate(CAPACITY_HUGE);
}
}
+ /**
+ * @throws IOException
+ * @tests java.nio.channels.SocketChannel#read(ByteBuffer)
+ */
+ @TestTargetNew(
+ level = TestLevel.PARTIAL_COMPLETE,
+ notes = "",
+ method = "read",
+ args = {java.nio.ByteBuffer[].class}
+ )
+ public void test_socketChannel_read_DirectByteBuffer() throws InterruptedException, IOException {
+
+ ServerThread server = new ServerThread();
+ server.start();
+ Thread.currentThread().sleep(1000);
+
+ InetSocketAddress address = new InetSocketAddress(InetAddress
+ .getByName("localhost"), port);
+
+ // First test with array based byte buffer
+ SocketChannel sc = SocketChannel.open();
+ sc.connect(address);
+
+ ByteBuffer buf = ByteBuffer.allocate(data.length);
+ buf.limit(data.length / 2);
+ sc.read(buf);
+
+ buf.limit(buf.capacity());
+ sc.read(buf);
+ sc.close();
+
+ // Make sure the buffer is filled correctly
+ buf.rewind();
+ assertSameContent(data, buf);
+
+ // Now test with direct byte buffer
+ sc = SocketChannel.open();
+ sc.connect(address);
+
+ buf = ByteBuffer.allocateDirect(data.length);
+ buf.limit(data.length / 2);
+ sc.read(buf);
+
+ buf.limit(buf.capacity());
+ sc.read(buf);
+ sc.close();
+
+ // Make sure the buffer is filled correctly
+ buf.rewind();
+ assertSameContent(data, buf);
+ }
+
+ private void assertSameContent(byte[] data, ByteBuffer buf) {
+ for (byte b : data) {
+ if (b != buf.get()) {
+ int pos = buf.position() - 1;
+ fail("Content not equal. Buffer position: " +
+ (pos) + " expected: " + b + " was: " + buf.get(pos));
+ }
+ }
+ }
+
+ public static boolean done = false;
+ public static int port = Support_PortManager.getNextPort();
+ public static byte[] data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+
+ static class ServerThread extends Thread {
+ @Override
+ public void run() {
+ try {
+ ServerSocketChannel ssc = ServerSocketChannel.open();
+ InetSocketAddress addr = new InetSocketAddress(InetAddress
+ .getByAddress(new byte[] {0, 0, 0, 0}), port);
+ ssc.socket().bind(addr, 0);
+
+ ByteBuffer buf = ByteBuffer.allocate(10);
+ buf.put(data);
+
+ while (!done) {
+ SocketChannel sc = ssc.accept();
+ buf.rewind();
+ sc.write(buf);
+ }
+ } catch (Exception e) {
+ // ignore
+ }
+ }
+ }
+
class MockSocketChannel extends SocketChannel {
private boolean isWriteCalled = false;