Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/SocketChannel/Write.java
38828 views
1
/*
2
* Copyright (c) 2003, 2010, 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 4854354
26
* @summary Test vector write faster than can be read
27
* @library ..
28
*/
29
30
import java.io.*;
31
import java.net.*;
32
import java.nio.*;
33
import java.nio.channels.*;
34
import java.util.*;
35
import sun.misc.*;
36
37
38
public class Write {
39
40
static Random generator = new Random();
41
42
static int testSize = 15;
43
44
public static void main(String[] args) throws Exception {
45
WriteServer sv = new WriteServer();
46
sv.start();
47
bufferTest(sv.port());
48
if (sv.finish(8000) == 0)
49
throw new Exception("Failed" );
50
}
51
52
static void bufferTest(int port) throws Exception {
53
ByteBuffer[] bufs = new ByteBuffer[testSize];
54
for(int i=0; i<testSize; i++) {
55
String source =
56
"a muchmuchmuchmuchmuchmuchmuchmuch larger buffer numbered " +
57
i;
58
bufs[i] = ByteBuffer.allocateDirect(source.length());
59
}
60
61
// Get a connection to the server
62
InetAddress lh = InetAddress.getLocalHost();
63
InetSocketAddress isa = new InetSocketAddress(lh, port);
64
SocketChannel sc = SocketChannel.open();
65
sc.connect(isa);
66
sc.configureBlocking(false);
67
68
// Try to overflow the socket buffer
69
long total = 0;
70
for (int i=0; i<100; i++) {
71
long bytesWritten = sc.write(bufs);
72
if (bytesWritten > 0)
73
total += bytesWritten;
74
for(int j=0; j<testSize; j++)
75
bufs[j].rewind();
76
}
77
78
// Clean up
79
sc.close();
80
}
81
82
}
83
84
85
class WriteServer extends TestThread {
86
87
static Random generator = new Random();
88
89
90
final ServerSocketChannel ssc;
91
92
WriteServer() throws IOException {
93
super("WriteServer");
94
this.ssc = ServerSocketChannel.open().bind(new InetSocketAddress(0));
95
}
96
97
int port() {
98
return ssc.socket().getLocalPort();
99
}
100
101
void go() throws Exception {
102
bufferTest();
103
}
104
105
void bufferTest() throws Exception {
106
ByteBuffer buf = ByteBuffer.allocateDirect(5);
107
108
// Get a connection from client
109
SocketChannel sc = null;
110
111
try {
112
ssc.configureBlocking(false);
113
114
for (;;) {
115
sc = ssc.accept();
116
if (sc != null)
117
break;
118
Thread.sleep(50);
119
}
120
121
// I'm a slow reader...
122
Thread.sleep(3000);
123
124
} finally {
125
// Clean up
126
ssc.close();
127
if (sc != null)
128
sc.close();
129
}
130
131
}
132
133
}
134
135