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