Path: blob/master/test/jdk/java/nio/channels/SocketChannel/VectorIO.java
41154 views
/*1* Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 819102525* @summary Test socketchannel vector IO (use -Dseed=X to set PRNG seed)26* @library .. /test/lib27* @build jdk.test.lib.RandomFactory28* @run main VectorIO29* @key randomness30*/3132import java.io.*;33import java.net.*;34import java.nio.*;35import java.nio.channels.*;36import java.util.*;37import jdk.test.lib.RandomFactory;3839public class VectorIO {4041private static Random generator = RandomFactory.getRandom();4243static int testSize;4445// whether to use the write/read variant with a length parameter46static boolean setLength;4748public static void main(String[] args) throws Exception {49testSize = 1;50setLength = false;51runTest();52for(int i=15; i<18; i++) {53testSize = i;54setLength = !setLength;55runTest();56}57}5859static void runTest() throws Exception {60System.err.println("Length " + testSize);61Server sv = new Server(testSize);62sv.start();63bufferTest(sv.port());64if (sv.finish(8000) == 0)65throw new Exception("Failed: Length = " + testSize);66}6768static void bufferTest(int port) throws Exception {69ByteBuffer[] bufs = new ByteBuffer[testSize];70long total = 0L;71for(int i=0; i<testSize; i++) {72String source = "buffer" + i;73if (generator.nextBoolean())74bufs[i] = ByteBuffer.allocateDirect(source.length());75else76bufs[i] = ByteBuffer.allocate(source.length());7778bufs[i].put(source.getBytes("8859_1"));79bufs[i].flip();80total += bufs[i].remaining();81}8283ByteBuffer[] bufsPlus1 = new ByteBuffer[bufs.length + 1];84System.arraycopy(bufs, 0, bufsPlus1, 0, bufs.length);8586// Get a connection to the server87InetAddress lh = InetAddress.getLocalHost();88InetSocketAddress isa = new InetSocketAddress(lh, port);89SocketChannel sc = SocketChannel.open();90sc.connect(isa);91sc.configureBlocking(generator.nextBoolean());9293// Write the data out94long rem = total;95while (rem > 0L) {96long bytesWritten;97if (setLength) {98bytesWritten = sc.write(bufsPlus1, 0, bufs.length);99} else {100bytesWritten = sc.write(bufs);101}102if (bytesWritten == 0) {103if (sc.isBlocking()) {104throw new RuntimeException("write did not block");105} else {106System.err.println("Non-blocking write() wrote zero bytes");107}108Thread.sleep(50);109} else {110rem -= bytesWritten;111}112}113114// Clean up115sc.close();116}117118static class Server119extends TestThread120{121final int testSize;122final ServerSocketChannel ssc;123124Server(int testSize) throws IOException {125super("Server " + testSize);126this.testSize = testSize;127this.ssc = ServerSocketChannel.open().bind(new InetSocketAddress(0));128}129130int port() {131return ssc.socket().getLocalPort();132}133134void go() throws Exception {135bufferTest();136}137138void bufferTest() throws Exception {139long total = 0L;140ByteBuffer[] bufs = new ByteBuffer[testSize];141for(int i=0; i<testSize; i++) {142String source = "buffer" + i;143if (generator.nextBoolean())144bufs[i] = ByteBuffer.allocateDirect(source.length());145else146bufs[i] = ByteBuffer.allocate(source.length());147total += bufs[i].capacity();148}149150ByteBuffer[] bufsPlus1 = new ByteBuffer[bufs.length + 1];151System.arraycopy(bufs, 0, bufsPlus1, 0, bufs.length);152153// Get a connection from client154SocketChannel sc = null;155156try {157158ssc.configureBlocking(false);159160for (;;) {161sc = ssc.accept();162if (sc != null) {163System.err.println("accept() succeeded");164break;165}166Thread.sleep(50);167}168169sc.configureBlocking(generator.nextBoolean());170171// Read data into multiple buffers172long avail = total;173while (avail > 0) {174long bytesRead;175if (setLength) {176bytesRead = sc.read(bufsPlus1, 0, bufs.length);177} else {178bytesRead = sc.read(bufs);179}180if (bytesRead < 0)181break;182if (bytesRead == 0) {183if (sc.isBlocking()) {184throw new RuntimeException("read did not block");185} else {186System.err.println187("Non-blocking read() read zero bytes");188}189Thread.sleep(50);190}191avail -= bytesRead;192}193194// Check results195for(int i=0; i<testSize; i++) {196String expected = "buffer" + i;197bufs[i].flip();198int size = bufs[i].capacity();199byte[] data = new byte[size];200for(int j=0; j<size; j++)201data[j] = bufs[i].get();202String message = new String(data, "8859_1");203if (!message.equals(expected))204throw new Exception("Wrong data: Got "205+ message + ", expected "206+ expected);207}208209} finally {210// Clean up211ssc.close();212if (sc != null)213sc.close();214}215216}217218}219220}221222223