893N/A/*
3261N/A * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
893N/A * published by the Free Software Foundation.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/A/* @test
893N/A * @bug 4607272
893N/A * @summary Test Channels methods for interoperability between streams and
893N/A * asynchronous byte channels
893N/A */
893N/A
893N/Aimport java.net.*;
893N/Aimport java.io.*;
893N/Aimport java.nio.channels.*;
893N/Aimport java.util.Random;
893N/A
893N/Apublic class Basic2 {
893N/A
893N/A static final Random rand = new Random();
893N/A
893N/A public static void main(String[] args) throws Exception {
893N/A // establish loopback connection
893N/A AsynchronousServerSocketChannel listener =
893N/A AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(0));
893N/A int port = ((InetSocketAddress)(listener.getLocalAddress())).getPort();
893N/A InetSocketAddress isa =
893N/A new InetSocketAddress(InetAddress.getLocalHost(), port);
893N/A AsynchronousSocketChannel ch1 = AsynchronousSocketChannel.open();
893N/A ch1.connect(isa).get();
893N/A AsynchronousSocketChannel ch2 = listener.accept().get();
893N/A
893N/A // start thread to write to stream
893N/A Writer writer = new Writer(Channels.newOutputStream(ch1));
893N/A Thread writerThread = new Thread(writer);
893N/A writerThread.start();
893N/A
893N/A // start thread to read from stream
893N/A Reader reader = new Reader(Channels.newInputStream(ch2));
893N/A Thread readerThread = new Thread(reader);
893N/A readerThread.start();
893N/A
893N/A // wait for threads to complete
893N/A writerThread.join();
893N/A readerThread.join();
893N/A
2546N/A // shutdown listener
2546N/A listener.close();
2546N/A
893N/A // check that reader received what we expected
893N/A if (reader.total() != writer.total())
893N/A throw new RuntimeException("Unexpected number of bytes read");
893N/A if (reader.hash() != writer.hash())
893N/A throw new RuntimeException("Hash incorrect for bytes read");
893N/A
893N/A // channels should be closed
893N/A if (ch1.isOpen() || ch2.isOpen())
893N/A throw new RuntimeException("Channels should be closed");
893N/A }
893N/A
893N/A static class Reader implements Runnable {
893N/A private final InputStream in;
893N/A private volatile int total;
893N/A private volatile int hash;
893N/A
893N/A Reader(InputStream in) {
893N/A this.in = in;
893N/A }
893N/A
893N/A public void run() {
893N/A try {
893N/A int n;
893N/A do {
893N/A // random offset/len
893N/A byte[] buf = new byte[128 + rand.nextInt(128)];
893N/A int len, off;
893N/A if (rand.nextBoolean()) {
893N/A len = buf.length;
893N/A off = 0;
893N/A n = in.read(buf);
893N/A } else {
893N/A len = 1 + rand.nextInt(64);
893N/A off = rand.nextInt(64);
893N/A n = in.read(buf, off, len);
893N/A }
893N/A if (n > len)
893N/A throw new RuntimeException("Too many bytes read");
893N/A if (n > 0) {
893N/A total += n;
893N/A for (int i=0; i<n; i++) {
893N/A int value = buf[off + i];
893N/A hash = hash ^ value;
893N/A }
893N/A }
893N/A } while (n > 0);
893N/A in.close();
893N/A
893N/A } catch (IOException x) {
893N/A x.printStackTrace();
893N/A }
893N/A }
893N/A
893N/A int total() { return total; }
893N/A int hash() { return hash; }
893N/A }
893N/A
893N/A static class Writer implements Runnable {
893N/A private final OutputStream out;
893N/A private final int total;
893N/A private volatile int hash;
893N/A
893N/A Writer(OutputStream out) {
893N/A this.out = out;
893N/A this.total = 50*1000 + rand.nextInt(50*1000);
893N/A }
893N/A
893N/A public void run() {
893N/A hash = 0;
893N/A int rem = total;
893N/A try {
893N/A do {
893N/A byte[] buf = new byte[1 + rand.nextInt(rem)];
893N/A int off, len;
893N/A
893N/A // write random bytes
893N/A if (rand.nextBoolean()) {
893N/A off = 0;
893N/A len = buf.length;
893N/A } else {
893N/A off = rand.nextInt(buf.length);
893N/A int r = buf.length - off;
893N/A len = (r <= 1) ? 1 : (1 + rand.nextInt(r));
893N/A }
893N/A for (int i=0; i<len; i++) {
893N/A byte value = (byte)rand.nextInt(256);
893N/A buf[off + i] = value;
893N/A hash = hash ^ value;
893N/A }
893N/A if ((off == 0) && (len == buf.length)) {
893N/A out.write(buf);
893N/A } else {
893N/A out.write(buf, off, len);
893N/A }
893N/A rem -= len;
893N/A } while (rem > 0);
893N/A
893N/A // close stream when done
893N/A out.close();
893N/A
893N/A } catch (IOException x) {
893N/A x.printStackTrace();
893N/A }
893N/A }
893N/A
893N/A int total() { return total; }
893N/A int hash() { return hash; }
893N/A }
893N/A}