Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/FileChannel/directio/PreadDirect.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 positional 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 PreadDirect
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
/**
50
* Testing FileChannel's positional read method.
51
*/
52
53
public class PreadDirect {
54
55
private static PrintStream err = System.err;
56
57
private static Random generator = RandomFactory.getRandom();
58
59
private static int charsPerGroup = -1;
60
61
private static int alignment = -1;
62
63
public static void main(String[] args) throws Exception {
64
if (initTests()) {
65
genericTest();
66
testNotAlignedChannelPosition();
67
testNegativeChannelPosition();
68
}
69
}
70
71
private static boolean initTests() throws Exception {
72
Path p = DirectIOTest.createTempFile();
73
try {
74
FileStore fs = Files.getFileStore(p);
75
alignment = (int)fs.getBlockSize();
76
charsPerGroup = alignment;
77
} finally {
78
Files.delete(p);
79
}
80
return true;
81
}
82
83
private static void testNegativeChannelPosition() throws Exception {
84
Path p = DirectIOTest.createTempFile();
85
86
try (OutputStream fos = Files.newOutputStream(p)) {
87
fos.write(new byte[charsPerGroup]);
88
}
89
90
try (FileChannel fc = FileChannel.open(p,
91
StandardOpenOption.DELETE_ON_CLOSE, ExtendedOpenOption.DIRECT)) {
92
try {
93
fc.read(ByteBuffer.allocate(charsPerGroup), -1L);
94
throw new RuntimeException("Expected exception not thrown");
95
} catch(IllegalArgumentException e) {
96
// Correct result
97
}
98
}
99
}
100
101
private static void testNotAlignedChannelPosition() throws Exception {
102
Path p = DirectIOTest.createTempFile();
103
104
try (OutputStream fos = Files.newOutputStream(p)) {
105
fos.write(new byte[charsPerGroup]);
106
}
107
108
try (FileChannel fc = FileChannel.open(p,
109
StandardOpenOption.DELETE_ON_CLOSE, ExtendedOpenOption.DIRECT)) {
110
long pos = charsPerGroup - 1;
111
try {
112
fc.read(ByteBuffer.allocate(charsPerGroup), pos);
113
throw new RuntimeException("Expected exception not thrown");
114
} catch(IOException e) {
115
if (!e.getMessage().contains("Channel position (" + pos
116
+ ") is not a multiple of the block size (" + alignment + ")"))
117
throw new RuntimeException("Read test failed");
118
}
119
}
120
}
121
122
private static void genericTest() throws Exception {
123
StringBuffer sb = new StringBuffer();
124
sb.setLength(2);
125
126
Path p = DirectIOTest.createTempFile();
127
128
initTestFile(p);
129
130
try (FileChannel fc = FileChannel.open(p,
131
StandardOpenOption.DELETE_ON_CLOSE, ExtendedOpenOption.DIRECT)) {
132
ByteBuffer block =
133
ByteBuffer.allocateDirect(charsPerGroup + alignment - 1)
134
.alignedSlice(alignment);
135
for (int x = 0; x < 100; x++) {
136
block.clear();
137
long offset = generator.nextInt(100) * charsPerGroup;
138
long expectedResult = offset / charsPerGroup;
139
offset = expectedResult * charsPerGroup;
140
141
long originalPosition = fc.position();
142
143
int read = fc.read(block, offset);
144
if (read != charsPerGroup)
145
throw new Exception("Read failed");
146
147
long newPosition = fc.position();
148
149
for (int i = 0; i < 2; i++) {
150
byte aByte = block.get(i);
151
sb.setCharAt(i, (char)aByte);
152
}
153
int result = Integer.parseInt(sb.toString());
154
if (result != expectedResult) {
155
err.println("I expected "+ expectedResult);
156
err.println("I got "+ result);
157
throw new Exception("Read test failed");
158
}
159
160
// Ensure that file pointer position has not changed
161
if (originalPosition != newPosition)
162
throw new Exception("File position modified");
163
}
164
}
165
}
166
167
private static void initTestFile(Path p) throws Exception {
168
try (OutputStream fos = Files.newOutputStream(p)) {
169
try (BufferedWriter awriter
170
= new BufferedWriter(new OutputStreamWriter(fos, "8859_1"))) {
171
172
for (int i = 0; i < 100; i++) {
173
String number = new Integer(i).toString();
174
for (int h = 0; h < 2 - number.length(); h++)
175
awriter.write("0");
176
awriter.write(""+i);
177
for (int j = 0; j < (charsPerGroup - 2); j++)
178
awriter.write("0");
179
}
180
awriter.flush();
181
}
182
}
183
}
184
}
185
186