Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/MappedByteBuffer/ForceException.java
41149 views
1
/*
2
* Copyright (c) 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 6539707
26
* @summary Test behavior of force() with respect to throwing exceptions
27
* @run main ForceException
28
*/
29
30
import java.io.File;
31
import java.io.IOException;
32
import java.io.RandomAccessFile;
33
import java.io.UncheckedIOException;
34
import java.nio.MappedByteBuffer;
35
import java.nio.channels.FileChannel;
36
37
public class ForceException {
38
public static void main(String[] args) throws IOException {
39
int blockSize = 2048 * 1024;
40
int numberOfBlocks = 200;
41
int fileLength = numberOfBlocks * blockSize;
42
43
File file = new File(System.getProperty("test.src", "."), "test.dat");
44
file.deleteOnExit();
45
try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
46
raf.setLength(fileLength);
47
48
int pos = (numberOfBlocks - 1) * blockSize;
49
int size = (int)Math.min(blockSize, fileLength - pos);
50
MappedByteBuffer mbb =
51
raf.getChannel().map(FileChannel.MapMode.READ_WRITE, pos, size);
52
53
System.out.printf("Write region 0x%s..0x%s%n",
54
Long.toHexString(pos), Long.toHexString(size));
55
for (int k = 0; k < mbb.limit(); k++) {
56
mbb.put(k, (byte)65);
57
}
58
59
// Catch and process UncheckedIOException; other Throwables fail
60
try {
61
System.out.println("Force");
62
mbb.force();
63
} catch (UncheckedIOException legal) {
64
System.out.printf("Caught legal exception %s%n", legal);
65
IOException cause = legal.getCause(); // can't be null
66
// Throw the cause if flush failed (should be only on Windows)
67
if (cause.getMessage().startsWith("Flush failed")) {
68
throw cause;
69
}
70
}
71
72
System.out.println("OK");
73
}
74
}
75
}
76
77