Basic2.java revision 2546:eb84b89ef3ff
1/*
2 * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/* @test
25 * @bug 4607272
26 * @summary Test Channels methods for interoperability between streams and
27 *     asynchronous byte channels
28 */
29
30import java.net.*;
31import java.io.*;
32import java.nio.channels.*;
33import java.util.Random;
34
35public class Basic2 {
36
37    static final Random rand = new Random();
38
39    public static void main(String[] args) throws Exception {
40        // establish loopback connection
41        AsynchronousServerSocketChannel listener =
42            AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(0));
43        int port = ((InetSocketAddress)(listener.getLocalAddress())).getPort();
44        InetSocketAddress isa =
45            new InetSocketAddress(InetAddress.getLocalHost(), port);
46        AsynchronousSocketChannel ch1 = AsynchronousSocketChannel.open();
47        ch1.connect(isa).get();
48        AsynchronousSocketChannel ch2 = listener.accept().get();
49
50        // start thread to write to stream
51        Writer writer = new Writer(Channels.newOutputStream(ch1));
52        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