Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/SocketChannel/Write.java
38828 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.*;34import sun.misc.*;353637public class Write {3839static Random generator = new Random();4041static int testSize = 15;4243public static void main(String[] args) throws Exception {44WriteServer sv = new WriteServer();45sv.start();46bufferTest(sv.port());47if (sv.finish(8000) == 0)48throw new Exception("Failed" );49}5051static void bufferTest(int port) throws Exception {52ByteBuffer[] bufs = new ByteBuffer[testSize];53for(int i=0; i<testSize; i++) {54String source =55"a muchmuchmuchmuchmuchmuchmuchmuch larger buffer numbered " +56i;57bufs[i] = ByteBuffer.allocateDirect(source.length());58}5960// Get a connection to the server61InetAddress lh = InetAddress.getLocalHost();62InetSocketAddress isa = new InetSocketAddress(lh, port);63SocketChannel sc = SocketChannel.open();64sc.connect(isa);65sc.configureBlocking(false);6667// Try to overflow the socket buffer68long total = 0;69for (int i=0; i<100; i++) {70long bytesWritten = sc.write(bufs);71if (bytesWritten > 0)72total += bytesWritten;73for(int j=0; j<testSize; j++)74bufs[j].rewind();75}7677// Clean up78sc.close();79}8081}828384class WriteServer extends TestThread {8586static Random generator = new Random();878889final ServerSocketChannel ssc;9091WriteServer() throws IOException {92super("WriteServer");93this.ssc = ServerSocketChannel.open().bind(new InetSocketAddress(0));94}9596int port() {97return ssc.socket().getLocalPort();98}99100void go() throws Exception {101bufferTest();102}103104void bufferTest() throws Exception {105ByteBuffer buf = ByteBuffer.allocateDirect(5);106107// Get a connection from client108SocketChannel sc = null;109110try {111ssc.configureBlocking(false);112113for (;;) {114sc = ssc.accept();115if (sc != null)116break;117Thread.sleep(50);118}119120// I'm a slow reader...121Thread.sleep(3000);122123} finally {124// Clean up125ssc.close();126if (sc != null)127sc.close();128}129130}131132}133134135