Basic2.java revision 3261:a06412e13bf7
11590Srgrimes/* 21590Srgrimes * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 31590Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 41590Srgrimes * 51590Srgrimes * This code is free software; you can redistribute it and/or modify it 61590Srgrimes * under the terms of the GNU General Public License version 2 only, as 71590Srgrimes * published by the Free Software Foundation. 81590Srgrimes * 91590Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT 101590Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 111590Srgrimes * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12263142Seadler * version 2 for more details (a copy is included in the LICENSE file that 131590Srgrimes * accompanied this code). 141590Srgrimes * 151590Srgrimes * You should have received a copy of the GNU General Public License version 161590Srgrimes * 2 along with this work; if not, write to the Free Software Foundation, 171590Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 181590Srgrimes * 191590Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 201590Srgrimes * or visit www.oracle.com if you need additional information or have any 211590Srgrimes * questions. 221590Srgrimes */ 231590Srgrimes 241590Srgrimes/* @test 251590Srgrimes * @bug 4607272 261590Srgrimes * @summary Test Channels methods for interoperability between streams and 271590Srgrimes * asynchronous byte channels 281590Srgrimes */ 291590Srgrimes 301590Srgrimesimport java.net.*; 311590Srgrimesimport java.io.*; 321590Srgrimesimport java.nio.channels.*; 331590Srgrimesimport java.util.Random; 341590Srgrimes 351590Srgrimespublic class Basic2 { 361590Srgrimes 371590Srgrimes static final Random rand = new Random(); 381590Srgrimes 391590Srgrimes public static void main(String[] args) throws Exception { 401590Srgrimes // establish loopback connection 411590Srgrimes AsynchronousServerSocketChannel listener = 421590Srgrimes AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(0)); 431590Srgrimes int port = ((InetSocketAddress)(listener.getLocalAddress())).getPort(); 441590Srgrimes InetSocketAddress isa = 451590Srgrimes new InetSocketAddress(InetAddress.getLocalHost(), port); 461590Srgrimes AsynchronousSocketChannel ch1 = AsynchronousSocketChannel.open(); 471590Srgrimes ch1.connect(isa).get(); 481590Srgrimes AsynchronousSocketChannel ch2 = listener.accept().get(); 491590Srgrimes 501590Srgrimes // start thread to write to stream 511590Srgrimes Writer writer = new Writer(Channels.newOutputStream(ch1)); 521590Srgrimes Thread writerThread = new Thread(writer); 53 writerThread.start(); 54 55 // start thread to read from stream 56 Reader reader = new Reader(Channels.newInputStream(ch2)); 57 Thread readerThread = new Thread(reader); 58 readerThread.start(); 59 60 // wait for threads to complete 61 writerThread.join(); 62 readerThread.join(); 63 64 // shutdown listener 65 listener.close(); 66 67 // check that reader received what we expected 68 if (reader.total() != writer.total()) 69 throw new RuntimeException("Unexpected number of bytes read"); 70 if (reader.hash() != writer.hash()) 71 throw new RuntimeException("Hash incorrect for bytes read"); 72 73 // channels should be closed 74 if (ch1.isOpen() || ch2.isOpen()) 75 throw new RuntimeException("Channels should be closed"); 76 } 77 78 static class Reader implements Runnable { 79 private final InputStream in; 80 private volatile int total; 81 private volatile int hash; 82 83 Reader(InputStream in) { 84 this.in = in; 85 } 86 87 public void run() { 88 try { 89 int n; 90 do { 91 // random offset/len 92 byte[] buf = new byte[128 + rand.nextInt(128)]; 93 int len, off; 94 if (rand.nextBoolean()) { 95 len = buf.length; 96 off = 0; 97 n = in.read(buf); 98 } else { 99 len = 1 + rand.nextInt(64); 100 off = rand.nextInt(64); 101 n = in.read(buf, off, len); 102 } 103 if (n > len) 104 throw new RuntimeException("Too many bytes read"); 105 if (n > 0) { 106 total += n; 107 for (int i=0; i<n; i++) { 108 int value = buf[off + i]; 109 hash = hash ^ value; 110 } 111 } 112 } while (n > 0); 113 in.close(); 114 115 } catch (IOException x) { 116 x.printStackTrace(); 117 } 118 } 119 120 int total() { return total; } 121 int hash() { return hash; } 122 } 123 124 static class Writer implements Runnable { 125 private final OutputStream out; 126 private final int total; 127 private volatile int hash; 128 129 Writer(OutputStream out) { 130 this.out = out; 131 this.total = 50*1000 + rand.nextInt(50*1000); 132 } 133 134 public void run() { 135 hash = 0; 136 int rem = total; 137 try { 138 do { 139 byte[] buf = new byte[1 + rand.nextInt(rem)]; 140 int off, len; 141 142 // write random bytes 143 if (rand.nextBoolean()) { 144 off = 0; 145 len = buf.length; 146 } else { 147 off = rand.nextInt(buf.length); 148 int r = buf.length - off; 149 len = (r <= 1) ? 1 : (1 + rand.nextInt(r)); 150 } 151 for (int i=0; i<len; i++) { 152 byte value = (byte)rand.nextInt(256); 153 buf[off + i] = value; 154 hash = hash ^ value; 155 } 156 if ((off == 0) && (len == buf.length)) { 157 out.write(buf); 158 } else { 159 out.write(buf, off, len); 160 } 161 rem -= len; 162 } while (rem > 0); 163 164 // close stream when done 165 out.close(); 166 167 } catch (IOException x) { 168 x.printStackTrace(); 169 } 170 } 171 172 int total() { return total; } 173 int hash() { return hash; } 174 } 175} 176