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