Path: blob/master/test/jdk/java/nio/channels/Channels/Basic2.java
41152 views
/*1* Copyright (c) 2008, 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 460727225* @summary Test Channels methods for interoperability between streams and26* asynchronous byte channels27* @key randomness28*/2930import java.net.*;31import java.io.*;32import java.nio.channels.*;33import java.util.Random;3435public class Basic2 {3637static final Random rand = new Random();3839public static void main(String[] args) throws Exception {40// establish loopback connection41AsynchronousServerSocketChannel listener =42AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(0));43int port = ((InetSocketAddress)(listener.getLocalAddress())).getPort();44InetSocketAddress isa =45new InetSocketAddress(InetAddress.getLocalHost(), port);46AsynchronousSocketChannel ch1 = AsynchronousSocketChannel.open();47ch1.connect(isa).get();48AsynchronousSocketChannel ch2 = listener.accept().get();4950// start thread to write to stream51Writer writer = new Writer(Channels.newOutputStream(ch1));52Thread writerThread = new Thread(writer);53writerThread.start();5455// start thread to read from stream56Reader reader = new Reader(Channels.newInputStream(ch2));57Thread readerThread = new Thread(reader);58readerThread.start();5960// wait for threads to complete61writerThread.join();62readerThread.join();6364// shutdown listener65listener.close();6667// check that reader received what we expected68if (reader.total() != writer.total())69throw new RuntimeException("Unexpected number of bytes read");70if (reader.hash() != writer.hash())71throw new RuntimeException("Hash incorrect for bytes read");7273// channels should be closed74if (ch1.isOpen() || ch2.isOpen())75throw new RuntimeException("Channels should be closed");76}7778static class Reader implements Runnable {79private final InputStream in;80private volatile int total;81private volatile int hash;8283Reader(InputStream in) {84this.in = in;85}8687public void run() {88try {89int n;90do {91// random offset/len92byte[] buf = new byte[128 + rand.nextInt(128)];93int len, off;94if (rand.nextBoolean()) {95len = buf.length;96off = 0;97n = in.read(buf);98} else {99len = 1 + rand.nextInt(64);100off = rand.nextInt(64);101n = in.read(buf, off, len);102}103if (n > len)104throw new RuntimeException("Too many bytes read");105if (n > 0) {106total += n;107for (int i=0; i<n; i++) {108int value = buf[off + i];109hash = hash ^ value;110}111}112} while (n > 0);113in.close();114115} catch (IOException x) {116x.printStackTrace();117}118}119120int total() { return total; }121int hash() { return hash; }122}123124static class Writer implements Runnable {125private final OutputStream out;126private final int total;127private volatile int hash;128129Writer(OutputStream out) {130this.out = out;131this.total = 50*1000 + rand.nextInt(50*1000);132}133134public void run() {135hash = 0;136int rem = total;137try {138do {139byte[] buf = new byte[1 + rand.nextInt(rem)];140int off, len;141142// write random bytes143if (rand.nextBoolean()) {144off = 0;145len = buf.length;146} else {147off = rand.nextInt(buf.length);148int r = buf.length - off;149len = (r <= 1) ? 1 : (1 + rand.nextInt(r));150}151for (int i=0; i<len; i++) {152byte value = (byte)rand.nextInt(256);153buf[off + i] = value;154hash = hash ^ value;155}156if ((off == 0) && (len == buf.length)) {157out.write(buf);158} else {159out.write(buf, off, len);160}161rem -= len;162} while (rem > 0);163164// close stream when done165out.close();166167} catch (IOException x) {168x.printStackTrace();169}170}171172int total() { return total; }173int hash() { return hash; }174}175}176177178