Basic2.java revision 893:f06f30b29f36
1/*
2 * Copyright 2008-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any 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        // check that reader received what we expected
65        if (reader.total() != writer.total())
66            throw new RuntimeException("Unexpected number of bytes read");
67        if (reader.hash() != writer.hash())
68            throw new RuntimeException("Hash incorrect for bytes read");
69
70        // channels should be closed
71        if (ch1.isOpen() || ch2.isOpen())
72            throw new RuntimeException("Channels should be closed");
73    }
74
75    static class Reader implements Runnable {
76        private final InputStream in;
77        private volatile int total;
78        private volatile int hash;
79
80        Reader(InputStream in) {
81            this.in = in;
82        }
83
84        public void run() {
85            try {
86                int n;
87                do {
88                    // random offset/len
89                    byte[] buf = new byte[128 + rand.nextInt(128)];
90                    int len, off;
91                    if (rand.nextBoolean()) {
92                        len = buf.length;
93                        off = 0;
94                        n = in.read(buf);
95                    } else {
96                        len = 1 + rand.nextInt(64);
97                        off = rand.nextInt(64);
98                        n = in.read(buf, off, len);
99                    }
100                    if (n > len)
101                        throw new RuntimeException("Too many bytes read");
102                    if (n > 0) {
103                        total += n;
104                        for (int i=0; i<n; i++) {
105                            int value = buf[off + i];
106                            hash = hash ^ value;
107                        }
108                    }
109                } while (n > 0);
110                in.close();
111
112            } catch (IOException x) {
113                x.printStackTrace();
114            }
115        }
116
117        int total() { return total; }
118        int hash() { return hash; }
119    }
120
121    static class Writer implements Runnable {
122        private final OutputStream out;
123        private final int total;
124        private volatile int hash;
125
126        Writer(OutputStream out) {
127            this.out = out;
128            this.total = 50*1000 + rand.nextInt(50*1000);
129        }
130
131        public void run() {
132            hash = 0;
133            int rem = total;
134            try {
135                do {
136                    byte[] buf = new byte[1 + rand.nextInt(rem)];
137                    int off, len;
138
139                    // write random bytes
140                    if (rand.nextBoolean()) {
141                        off = 0;
142                        len = buf.length;
143                    } else {
144                        off = rand.nextInt(buf.length);
145                        int r = buf.length - off;
146                        len = (r <= 1) ? 1 : (1 + rand.nextInt(r));
147                    }
148                    for (int i=0; i<len; i++) {
149                        byte value = (byte)rand.nextInt(256);
150                        buf[off + i] = value;
151                        hash = hash ^ value;
152                    }
153                    if ((off == 0) && (len == buf.length)) {
154                        out.write(buf);
155                    } else {
156                        out.write(buf, off, len);
157                    }
158                    rem -= len;
159                } while (rem > 0);
160
161                // close stream when done
162                out.close();
163
164            } catch (IOException x) {
165                x.printStackTrace();
166            }
167        }
168
169        int total() { return total; }
170        int hash() { return hash; }
171    }
172}
173