Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/SocketChannel/VectorIO.java
38828 views
/*1* Copyright (c) 2000, 2010, 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* @summary Test socketchannel vector IO25* @library ..26* @key randomness27*/2829import java.io.*;30import java.net.*;31import java.nio.*;32import java.nio.channels.*;33import java.util.*;34import sun.misc.*;353637public class VectorIO {3839static Random generator = new Random();4041static int testSize;4243public static void main(String[] args) throws Exception {44testSize = 1;45runTest();46for(int i=15; i<18; i++) {47testSize = i;48runTest();49}50}5152static void runTest() throws Exception {53System.err.println("Length " + testSize);54Server sv = new Server(testSize);55sv.start();56bufferTest(sv.port());57if (sv.finish(8000) == 0)58throw new Exception("Failed: Length = " + testSize);59}6061static void bufferTest(int port) throws Exception {62ByteBuffer[] bufs = new ByteBuffer[testSize];63long total = 0L;64for(int i=0; i<testSize; i++) {65String source = "buffer" + i;66if (generator.nextBoolean())67bufs[i] = ByteBuffer.allocateDirect(source.length());68else69bufs[i] = ByteBuffer.allocate(source.length());7071bufs[i].put(source.getBytes("8859_1"));72bufs[i].flip();73total += bufs[i].remaining();74}7576// Get a connection to the server77InetAddress lh = InetAddress.getLocalHost();78InetSocketAddress isa = new InetSocketAddress(lh, port);79SocketChannel sc = SocketChannel.open();80sc.connect(isa);81sc.configureBlocking(generator.nextBoolean());8283// Write the data out84long rem = total;85while (rem > 0L) {86long bytesWritten = sc.write(bufs);87if (bytesWritten == 0) {88if (sc.isBlocking())89throw new RuntimeException("write did not block");90Thread.sleep(50);91} else {92rem -= bytesWritten;93}94}9596// Clean up97sc.close();98}99100static class Server101extends TestThread102{103static Random generator = new Random();104105final int testSize;106final ServerSocketChannel ssc;107108Server(int testSize) throws IOException {109super("Server " + testSize);110this.testSize = testSize;111this.ssc = ServerSocketChannel.open().bind(new InetSocketAddress(0));112}113114int port() {115return ssc.socket().getLocalPort();116}117118void go() throws Exception {119bufferTest();120}121122void bufferTest() throws Exception {123long total = 0L;124ByteBuffer[] bufs = new ByteBuffer[testSize];125for(int i=0; i<testSize; i++) {126String source = "buffer" + i;127if (generator.nextBoolean())128bufs[i] = ByteBuffer.allocateDirect(source.length());129else130bufs[i] = ByteBuffer.allocate(source.length());131total += bufs[i].capacity();132}133134// Get a connection from client135SocketChannel sc = null;136137try {138139ssc.configureBlocking(false);140141for (;;) {142sc = ssc.accept();143if (sc != null)144break;145Thread.sleep(50);146}147148sc.configureBlocking(generator.nextBoolean());149150// Read data into multiple buffers151long avail = total;152while (avail > 0) {153long bytesRead = sc.read(bufs);154if (bytesRead < 0)155break;156if (bytesRead == 0) {157if (sc.isBlocking())158throw new RuntimeException("read did not block");159Thread.sleep(50);160}161avail -= bytesRead;162}163164// Check results165for(int i=0; i<testSize; i++) {166String expected = "buffer" + i;167bufs[i].flip();168int size = bufs[i].capacity();169byte[] data = new byte[size];170for(int j=0; j<size; j++)171data[j] = bufs[i].get();172String message = new String(data, "8859_1");173if (!message.equals(expected))174throw new Exception("Wrong data: Got "175+ message + ", expected "176+ expected);177}178179} finally {180// Clean up181ssc.close();182if (sc != null)183sc.close();184}185186}187188}189190}191192193