Path: blob/master/test/jdk/java/nio/channels/SocketChannel/Write.java
41154 views
/*1* Copyright (c) 2003, 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* @bug 485435425* @summary Test vector write faster than can be read26* @library ..27*/2829import java.io.*;30import java.net.*;31import java.nio.*;32import java.nio.channels.*;33import java.util.*;343536public class Write {3738static Random generator = new Random();3940static int testSize = 15;4142public static void main(String[] args) throws Exception {43WriteServer sv = new WriteServer();44sv.start();45bufferTest(sv.port());46if (sv.finish(8000) == 0)47throw new Exception("Failed" );48}4950static void bufferTest(int port) throws Exception {51ByteBuffer[] bufs = new ByteBuffer[testSize];52for(int i=0; i<testSize; i++) {53String source =54"a muchmuchmuchmuchmuchmuchmuchmuch larger buffer numbered " +55i;56bufs[i] = ByteBuffer.allocateDirect(source.length());57}5859// Get a connection to the server60InetAddress lh = InetAddress.getLocalHost();61InetSocketAddress isa = new InetSocketAddress(lh, port);62SocketChannel sc = SocketChannel.open();63sc.connect(isa);64sc.configureBlocking(false);6566// Try to overflow the socket buffer67long total = 0;68for (int i=0; i<100; i++) {69long bytesWritten = sc.write(bufs);70if (bytesWritten > 0)71total += bytesWritten;72for(int j=0; j<testSize; j++)73bufs[j].rewind();74}7576// Clean up77sc.close();78}7980}818283class WriteServer extends TestThread {8485static Random generator = new Random();868788final ServerSocketChannel ssc;8990WriteServer() throws IOException {91super("WriteServer");92this.ssc = ServerSocketChannel.open().bind(new InetSocketAddress(0));93}9495int port() {96return ssc.socket().getLocalPort();97}9899void go() throws Exception {100bufferTest();101}102103void bufferTest() throws Exception {104ByteBuffer buf = ByteBuffer.allocateDirect(5);105106// Get a connection from client107SocketChannel sc = null;108109try {110ssc.configureBlocking(false);111112for (;;) {113sc = ssc.accept();114if (sc != null)115break;116Thread.sleep(50);117}118119// I'm a slow reader...120Thread.sleep(3000);121122} finally {123// Clean up124ssc.close();125if (sc != null)126sc.close();127}128129}130131}132133134