Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/SocketChannel/VectorParams.java
41154 views
1
/*
2
* Copyright (c) 2003, 2018, 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 4865031
26
* @summary Test ScatteringByteChannel/GatheringByteChannel read/write
27
* @library .. /test/lib
28
* @build jdk.test.lib.Utils TestServers
29
* @run main VectorParams
30
*/
31
32
import java.io.*;
33
import java.net.*;
34
import java.nio.*;
35
import java.nio.channels.*;
36
37
public class VectorParams {
38
39
static java.io.PrintStream out = System.out;
40
41
static final int testSize = 10;
42
static ByteBuffer[] bufs = null;
43
static InetSocketAddress isa = null;
44
45
public static void main(String[] args) throws Exception {
46
try (TestServers.DayTimeServer daytimeServer
47
= TestServers.DayTimeServer.startNewServer(100)) {
48
initBufs(daytimeServer);
49
testSocketChannelVectorParams();
50
testDatagramChannelVectorParams();
51
testPipeVectorParams();
52
testFileVectorParams();
53
}
54
}
55
56
static void initBufs(TestServers.DayTimeServer daytimeServer) throws Exception {
57
bufs = new ByteBuffer[testSize];
58
for(int i=0; i<testSize; i++) {
59
String source = "buffer" + i;
60
bufs[i] = ByteBuffer.allocate(source.length());
61
bufs[i].put(source.getBytes("8859_1"));
62
bufs[i].flip();
63
}
64
isa = new InetSocketAddress(daytimeServer.getAddress(),
65
daytimeServer.getPort());
66
}
67
68
static void testSocketChannelVectorParams() throws Exception {
69
SocketChannel sc = SocketChannel.open(isa);
70
tryBadWrite(sc, bufs, 0, -1);
71
tryBadWrite(sc, bufs, -1, 0);
72
tryBadWrite(sc, bufs, 0, 1000);
73
tryBadWrite(sc, bufs, 1000, 1);
74
tryBadRead(sc, bufs, 0, -1);
75
tryBadRead(sc, bufs, -1, 0);
76
tryBadRead(sc, bufs, 0, 1000);
77
tryBadRead(sc, bufs, 1000, 1);
78
sc.close();
79
}
80
81
static void testDatagramChannelVectorParams() throws Exception {
82
DatagramChannel dc = DatagramChannel.open();
83
dc.connect(isa);
84
tryBadRead(dc, bufs, 0, -1);
85
tryBadRead(dc, bufs, -1, 0);
86
tryBadRead(dc, bufs, 0, 1000);
87
tryBadRead(dc, bufs, 1000, 1);
88
tryBadWrite(dc, bufs, 0, -1);
89
tryBadWrite(dc, bufs, -1, 0);
90
tryBadWrite(dc, bufs, 0, 1000);
91
tryBadWrite(dc, bufs, 1000, 1);
92
dc.close();
93
}
94
95
static void testPipeVectorParams() throws Exception {
96
Pipe p = Pipe.open();
97
Pipe.SinkChannel sink = p.sink();
98
Pipe.SourceChannel source = p.source();
99
tryBadWrite(sink, bufs, 0, -1);
100
tryBadWrite(sink, bufs, -1, 0);
101
tryBadWrite(sink, bufs, 0, 1000);
102
tryBadWrite(sink, bufs, 1000, 1);
103
tryBadRead(source, bufs, 0, -1);
104
tryBadRead(source, bufs, -1, 0);
105
tryBadRead(source, bufs, 0, 1000);
106
tryBadRead(source, bufs, 1000, 1);
107
sink.close();
108
source.close();
109
}
110
111
static void testFileVectorParams() throws Exception {
112
File testFile = File.createTempFile("filevec", null);
113
testFile.deleteOnExit();
114
RandomAccessFile raf = new RandomAccessFile(testFile, "rw");
115
FileChannel fc = raf.getChannel();
116
tryBadWrite(fc, bufs, 0, -1);
117
tryBadWrite(fc, bufs, -1, 0);
118
tryBadWrite(fc, bufs, 0, 1000);
119
tryBadWrite(fc, bufs, 1000, 1);
120
tryBadRead(fc, bufs, 0, -1);
121
tryBadRead(fc, bufs, -1, 0);
122
tryBadRead(fc, bufs, 0, 1000);
123
tryBadRead(fc, bufs, 1000, 1);
124
fc.close();
125
}
126
127
private static void tryBadWrite(GatheringByteChannel gbc,
128
ByteBuffer[] bufs, int offset, int len)
129
throws Exception
130
{
131
try {
132
gbc.write(bufs, offset, len);
133
throw new RuntimeException("Expected exception not thrown");
134
} catch (IndexOutOfBoundsException ioobe) {
135
// Correct result
136
}
137
}
138
139
private static void tryBadRead(ScatteringByteChannel sbc,
140
ByteBuffer[] bufs, int offset, int len)
141
throws Exception
142
{
143
try {
144
sbc.read(bufs, offset, len);
145
throw new RuntimeException("Expected exception not thrown");
146
} catch (IndexOutOfBoundsException ioobe) {
147
// Correct result
148
}
149
}
150
151
}
152
153