Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/FileChannel/directio/ReadDirect.java
41161 views
1
/*
2
* Copyright (c) 2017, 2021, 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 8164900
26
* @summary Test read method of FileChannel with DirectIO
27
* (use -Dseed=X to set PRNG seed)
28
* @library .. /test/lib
29
* @build jdk.test.lib.RandomFactory
30
* DirectIOTest
31
* @run main/othervm ReadDirect
32
* @key randomness
33
*/
34
35
import java.io.*;
36
import java.nio.ByteBuffer;
37
import java.nio.CharBuffer;
38
import java.nio.channels.*;
39
import java.nio.file.Files;
40
import java.nio.file.FileStore;
41
import java.nio.file.Path;
42
import java.nio.file.Paths;
43
import java.nio.file.StandardOpenOption;
44
import java.util.Random;
45
import com.sun.nio.file.ExtendedOpenOption;
46
47
import jdk.test.lib.RandomFactory;
48
49
public class ReadDirect {
50
51
private static PrintStream err = System.err;
52
53
private static Random generator = RandomFactory.getRandom();
54
55
private static int charsPerGroup = -1;
56
57
private static int alignment = -1;
58
59
private static boolean initTests() throws Exception {
60
Path p = DirectIOTest.createTempFile();
61
try {
62
FileStore fs = Files.getFileStore(p);
63
alignment = (int)fs.getBlockSize();
64
charsPerGroup = alignment;
65
} finally {
66
Files.delete(p);
67
}
68
return true;
69
}
70
71
private static void testWithSingleBuffer() throws Exception {
72
StringBuffer sb = new StringBuffer();
73
sb.setLength(2);
74
75
Path p = DirectIOTest.createTempFile();
76
77
initTestFile(p);
78
try (FileChannel fc = FileChannel.open(p,
79
StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE,
80
ExtendedOpenOption.DIRECT)) {
81
ByteBuffer block = ByteBuffer.allocateDirect(charsPerGroup
82
+ alignment - 1).alignedSlice(alignment);
83
for (int x = 0; x < 100; x++) {
84
block.clear();
85
long offset = x * charsPerGroup;
86
long expectedResult = offset / charsPerGroup;
87
fc.read(block);
88
89
for (int i = 0; i < 2; i++) {
90
byte aByte = block.get(i);
91
sb.setCharAt(i, (char)aByte);
92
}
93
int result = Integer.parseInt(sb.toString());
94
if (result != expectedResult) {
95
err.println("I expected " + expectedResult);
96
err.println("I got " + result);
97
throw new Exception("Read test failed");
98
}
99
}
100
}
101
}
102
103
private static void testWithNotAlignedBufferSize() throws Exception {
104
int bufferSize = charsPerGroup - 1;
105
Path p = DirectIOTest.createTempFile();
106
107
try (OutputStream fos = Files.newOutputStream(p)) {
108
fos.write(new byte[bufferSize]);
109
}
110
111
try (FileChannel fc = FileChannel.open(p,
112
StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE,
113
ExtendedOpenOption.DIRECT)) {
114
ByteBuffer block = ByteBuffer.allocate(bufferSize);
115
try {
116
fc.read(block);
117
throw new RuntimeException("Expected exception not thrown");
118
} catch (IOException e) {
119
if (!e.getMessage().contains("Number of remaining bytes ("
120
+ bufferSize + ") is not a multiple of the block size ("
121
+ alignment + ")"))
122
throw new Exception("Read test failed");
123
}
124
}
125
}
126
127
private static void testWithNotAlignedBufferOffset() throws Exception {
128
int bufferSize = charsPerGroup * 2;
129
int pos = alignment - 1;
130
131
Path p = DirectIOTest.createTempFile();
132
133
try (OutputStream fos = Files.newOutputStream(p)) {
134
fos.write(new byte[bufferSize]);
135
}
136
137
try (FileChannel fc = FileChannel.open(p,
138
StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE,
139
ExtendedOpenOption.DIRECT)) {
140
ByteBuffer block = ByteBuffer.allocateDirect(bufferSize);
141
block.position(pos);
142
block.limit(bufferSize - 1);
143
try {
144
fc.read(block);
145
throw new RuntimeException("Expected exception not thrown");
146
} catch (IOException e) {
147
if (!e.getMessage().contains("Current location of the bytebuffer "
148
+ "(" + pos + ") is not a multiple of the block size ("
149
+ alignment + ")"))
150
throw new Exception("Read test failed");
151
}
152
}
153
}
154
155
private static void testWithArrayOfBuffer() throws Exception {
156
StringBuffer sb = new StringBuffer();
157
sb.setLength(2);
158
ByteBuffer[] dests = new ByteBuffer[4];
159
Path p = DirectIOTest.createTempFile();
160
161
initTestFile(p);
162
163
try (FileChannel fc = FileChannel.open(p,
164
StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE,
165
ExtendedOpenOption.DIRECT)) {
166
int randomNumber = -1;
167
168
for (int i = 0; i < 4; i++) {
169
dests[i] = ByteBuffer.allocateDirect
170
(charsPerGroup + alignment - 1).alignedSlice(alignment);
171
for (int j = 0; j < charsPerGroup; j++) {
172
dests[i].put(j, (byte)'a');
173
}
174
}
175
176
// The size of the test FileChannel is 100*charsPerGroup.
177
// As the channel bytes will be scattered into two buffers
178
// each of size charsPerGroup, the offset cannot be greater
179
// than 98*charsPerGroup, so the value of randomNumber must
180
// be in the range [0,98], i.e., 0 <= randomNumber < 99.
181
randomNumber = generator.nextInt(99);
182
long offset = randomNumber * charsPerGroup;
183
fc.position(offset);
184
fc.read(dests, 1, 2);
185
186
for (int i = 0; i < 4; i++) {
187
if (i == 1 || i == 2) {
188
for (int j = 0; j < 2; j++) {
189
byte aByte = dests[i].get(j);
190
sb.setCharAt(j, (char)aByte);
191
}
192
int result = Integer.parseInt(sb.toString());
193
int expectedResult = randomNumber + i - 1;
194
if (result != expectedResult) {
195
err.println("I expected " + expectedResult);
196
err.println("I got " + result);
197
throw new Exception("Read test failed");
198
}
199
} else {
200
for (int k = 0; k < charsPerGroup; k++) {
201
if (dests[i].get(k) != (byte)'a')
202
throw new RuntimeException("Read test failed");
203
}
204
}
205
}
206
}
207
}
208
209
public static void testOnEOF() throws Exception {
210
int bufferSize = charsPerGroup / 2;
211
Path p = DirectIOTest.createTempFile();
212
213
try (OutputStream fos = Files.newOutputStream(p)) {
214
byte[] writeBlock = new byte[bufferSize];
215
for (int i = 0; i < bufferSize; i++) {
216
writeBlock[i] = ((byte)'a');
217
}
218
fos.write(writeBlock);
219
}
220
221
try (FileChannel fc = FileChannel.open(p,
222
StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE,
223
ExtendedOpenOption.DIRECT)) {
224
ByteBuffer block = ByteBuffer.allocateDirect(
225
(bufferSize / alignment + 1) * alignment + alignment - 1)
226
.alignedSlice(alignment);
227
int result = fc.read(block);
228
if (result != bufferSize) {
229
err.println("Number of bytes to read " + bufferSize);
230
err.println("I read " + result);
231
throw new Exception("Read test failed");
232
}
233
for (int j = 0; j < bufferSize; j++) {
234
if (block.get(j) != (byte)'a')
235
throw new RuntimeException("Read test failed");
236
}
237
}
238
}
239
240
public static void main(String[] args) throws Exception {
241
if (initTests()) {
242
testWithSingleBuffer();
243
testWithNotAlignedBufferSize();
244
testWithNotAlignedBufferOffset();
245
testWithArrayOfBuffer();
246
testOnEOF();
247
}
248
}
249
250
private static void initTestFile(Path p)
251
throws Exception {
252
try (OutputStream fos = Files.newOutputStream(p)) {
253
try (BufferedWriter awriter
254
= new BufferedWriter(new OutputStreamWriter(fos, "8859_1"))) {
255
for (int i = 0; i < 100; i++) {
256
String number = new Integer(i).toString();
257
for (int h = 0; h < 2 - number.length(); h++)
258
awriter.write("0");
259
awriter.write("" + i);
260
for (int j = 0; j < (charsPerGroup - 2); j++)
261
awriter.write("0");
262
}
263
awriter.flush();
264
}
265
}
266
}
267
}
268
269