2
|
1 |
/*
|
|
2 |
* Copyright 2000-2003 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 |
* @summary Test socketchannel vector IO
|
|
26 |
* @library ..
|
|
27 |
*/
|
|
28 |
|
|
29 |
import java.io.*;
|
|
30 |
import java.net.*;
|
|
31 |
import java.nio.*;
|
|
32 |
import java.nio.channels.*;
|
|
33 |
import java.util.*;
|
|
34 |
import sun.misc.*;
|
|
35 |
|
|
36 |
|
|
37 |
public class VectorIO {
|
|
38 |
|
|
39 |
static int port = 40170;
|
|
40 |
|
|
41 |
static Random generator = new Random();
|
|
42 |
|
|
43 |
static int testSize;
|
|
44 |
|
|
45 |
public static void main(String[] args) throws Exception {
|
|
46 |
testSize = 1;
|
|
47 |
runTest();
|
|
48 |
for(int i=15; i<18; i++) {
|
|
49 |
testSize = i;
|
|
50 |
runTest();
|
|
51 |
}
|
|
52 |
}
|
|
53 |
|
|
54 |
static void runTest() throws Exception {
|
|
55 |
System.err.println("Length " + testSize);
|
|
56 |
Server sv = new Server(testSize);
|
|
57 |
sv.start();
|
|
58 |
do {
|
|
59 |
try {
|
|
60 |
Thread.currentThread().sleep(200);
|
|
61 |
} catch (InterruptedException x) {
|
|
62 |
if (sv.finish(8000) == 0)
|
|
63 |
throw new Exception("Failed: Error in server thread");
|
|
64 |
}
|
|
65 |
} while (!sv.ready);
|
|
66 |
bufferTest();
|
|
67 |
if (sv.finish(8000) == 0)
|
|
68 |
throw new Exception("Failed: Length = " + testSize);
|
|
69 |
}
|
|
70 |
|
|
71 |
static void bufferTest() throws Exception {
|
|
72 |
ByteBuffer[] bufs = new ByteBuffer[testSize];
|
|
73 |
for(int i=0; i<testSize; i++) {
|
|
74 |
String source = "buffer" + i;
|
|
75 |
if (generator.nextBoolean())
|
|
76 |
bufs[i] = ByteBuffer.allocateDirect(source.length());
|
|
77 |
else
|
|
78 |
bufs[i] = ByteBuffer.allocate(source.length());
|
|
79 |
|
|
80 |
bufs[i].put(source.getBytes("8859_1"));
|
|
81 |
bufs[i].flip();
|
|
82 |
}
|
|
83 |
|
|
84 |
// Get a connection to the server
|
|
85 |
InetAddress lh = InetAddress.getLocalHost();
|
|
86 |
InetSocketAddress isa = new InetSocketAddress(lh, port);
|
|
87 |
SocketChannel sc = SocketChannel.open();
|
|
88 |
sc.connect(isa);
|
|
89 |
sc.configureBlocking(false);
|
|
90 |
|
|
91 |
// Write the data out
|
|
92 |
long bytesWritten = 0;
|
|
93 |
do {
|
|
94 |
bytesWritten = sc.write(bufs);
|
|
95 |
} while (bytesWritten > 0);
|
|
96 |
|
|
97 |
try {
|
|
98 |
Thread.currentThread().sleep(500);
|
|
99 |
} catch (InterruptedException ie) { }
|
|
100 |
|
|
101 |
// Clean up
|
|
102 |
sc.close();
|
|
103 |
}
|
|
104 |
|
|
105 |
static class Server
|
|
106 |
extends TestThread
|
|
107 |
{
|
|
108 |
static int port = 40170;
|
|
109 |
|
|
110 |
static Random generator = new Random();
|
|
111 |
|
|
112 |
int testSize;
|
|
113 |
|
|
114 |
volatile boolean ready = false;
|
|
115 |
|
|
116 |
Server(int testSize) {
|
|
117 |
super("Server " + testSize);
|
|
118 |
this.testSize = testSize;
|
|
119 |
}
|
|
120 |
|
|
121 |
void go() throws Exception {
|
|
122 |
bufferTest();
|
|
123 |
}
|
|
124 |
|
|
125 |
void bufferTest() throws Exception {
|
|
126 |
ByteBuffer[] bufs = new ByteBuffer[testSize];
|
|
127 |
for(int i=0; i<testSize; i++) {
|
|
128 |
String source = "buffer" + i;
|
|
129 |
if (generator.nextBoolean())
|
|
130 |
bufs[i] = ByteBuffer.allocateDirect(source.length());
|
|
131 |
else
|
|
132 |
bufs[i] = ByteBuffer.allocate(source.length());
|
|
133 |
}
|
|
134 |
|
|
135 |
// Get a connection from client
|
|
136 |
ServerSocketChannel ssc = ServerSocketChannel.open();
|
|
137 |
SocketChannel sc = null;
|
|
138 |
|
|
139 |
try {
|
|
140 |
|
|
141 |
ssc.configureBlocking(false);
|
|
142 |
InetAddress lh = InetAddress.getLocalHost();
|
|
143 |
InetSocketAddress isa = new InetSocketAddress(lh, port);
|
|
144 |
ssc.socket().bind(isa);
|
|
145 |
ready = true;
|
|
146 |
|
|
147 |
for (;;) {
|
|
148 |
sc = ssc.accept();
|
|
149 |
if (sc != null)
|
|
150 |
break;
|
|
151 |
Thread.sleep(50);
|
|
152 |
}
|
|
153 |
|
|
154 |
// Read data into multiple buffers
|
|
155 |
long bytesRead = 0;
|
|
156 |
do {
|
|
157 |
bytesRead = sc.read(bufs);
|
|
158 |
} while (bytesRead > 0);
|
|
159 |
|
|
160 |
// Check results
|
|
161 |
for(int i=0; i<testSize; i++) {
|
|
162 |
String expected = "buffer" + i;
|
|
163 |
bufs[i].flip();
|
|
164 |
int size = bufs[i].capacity();
|
|
165 |
byte[] data = new byte[size];
|
|
166 |
for(int j=0; j<size; j++)
|
|
167 |
data[j] = bufs[i].get();
|
|
168 |
String message = new String(data, "8859_1");
|
|
169 |
if (!message.equals(expected))
|
|
170 |
throw new Exception("Wrong data: Got "
|
|
171 |
+ message + ", expected "
|
|
172 |
+ expected);
|
|
173 |
}
|
|
174 |
|
|
175 |
} finally {
|
|
176 |
// Clean up
|
|
177 |
ssc.close();
|
|
178 |
if (sc != null)
|
|
179 |
sc.close();
|
|
180 |
}
|
|
181 |
|
|
182 |
}
|
|
183 |
|
|
184 |
}
|
|
185 |
|
|
186 |
}
|